Skip to content

Commit

Permalink
✅ Moving tests from Mocha to Jest (#2257)
Browse files Browse the repository at this point in the history
* ✅ Moving tests from Mocha to Jest

* Getting coverage (disable cache in pipeline)

* update README.md
  • Loading branch information
jakubswierczek authored Apr 27, 2023
1 parent 7d0cfd9 commit e19e6bc
Show file tree
Hide file tree
Showing 63 changed files with 1,584 additions and 2,882 deletions.
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# ENV For tests
49 changes: 11 additions & 38 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ jobs:
with:
node-version: 16

- name: Cache YARN dependencies
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.OS }}-yarn-cache-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.OS }}-yarn-cache-
# - name: Cache YARN dependencies
# uses: actions/cache@v3
# with:
# path: node_modules
# key: ${{ runner.OS }}-yarn-cache-${{ hashFiles('**/yarn.lock', '**/package.json') }}
# restore-keys: |
# ${{ runner.OS }}-yarn-cache-

- name: Install
run: yarn --no-progress --non-interactive --frozen-lockfile
Expand All @@ -31,39 +31,12 @@ jobs:
- name: Prettier
run: yarn format

# Disabled for now. Need to fix tests
# - name: Test
# run: CI=true yarn coverage

- name: Typecheck
run: yarn typecheck

# test-e2e:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v3
#
# - name: Cache YARN dependencies
# uses: actions/cache@v3
# with:
# path: node_modules
# key: ${{ runner.OS }}-yarn-cache-${{ hashFiles('**/yarn.lock') }}
# restore-keys: |
# ${{ runner.OS }}-yarn-cache-
#
# - name: Install
# run: yarn --no-progress --non-interactive --frozen-lockfile
#
# - name: Run deps
# run: |
# cd ./scripts
# docker-compose up -d
# sleep 10
#
# - name: Test-e2e
# run: yarn test:e2e
- name: Test
run: CI=true yarn test

# Few tips github actions
# - name: Setup SSH debug session
# uses: mxschmitt/action-tmate@v2
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2

50 changes: 0 additions & 50 deletions .mocharc.js

This file was deleted.

17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,23 @@ yarn test:fix

<br>

## Unit tests :construction_worker:
We are currently in the process of moving our tests from Mocha (with Chai, Enzyme, and Sinon) to Jest (with React Testing Library).
We are also in the process of adding more tests to increase our coverage.
We have some tests skipped for now, but we are working on fixing them.
**We needed to disable caching in the `build` action. Testing with collecting coverage was failing with caching enabled.**
Skipped tests (Marked with `TODO: [Mocha -> Jest] Rewrite in Jest compatible format.`):
- [Batch Manager](helpers/api/BatchManager.test.ts)
- [Manage Vault Validations](features/borrow/manage/tests/manageVaultValidations.test.ts)
- [Earn Calculations](features/earn/calculations.test.ts)
- [Manage Multiply Vault](features/multiply/manage/tests/manageMultiplyVault.test.ts)
- [Manage Multiply Vault - Adjust Validations](features/multiply/manage/tests/manageMultiplyVaultAdjustPositionValidations.test.ts)
- [Manage Multiply Vault - Other Action Validations](features/multiply/manage/tests/manageMultiplyVaultOtherActionsValidations.test.ts)
- [Open Multiply Vault](features/multiply/open/tests/openMultiplyVault.test.ts)
- [Maker Protocol Bonus Adapter](features/bonus/makerProtocolBonusAdapter.test.ts)
- [Blockchain Utils](blockchain/utils.test.ts)
- [Vault Math](blockchain/vault.maths.test.ts)

## License

Copyright (C) 2021 Oazo Apps Limited, Licensed under the Apache License, Version 2.0 (the
Expand Down
15 changes: 7 additions & 8 deletions blockchain/addresses/addressesUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { getCollaterals, getCollateralTokens } from 'blockchain/addresses/addressesUtils'
import mainnet from 'blockchain/addresses/mainnet.json'
import { supportedIlks } from 'blockchain/tokens/mainnet'
import { expect } from 'chai'

describe('adressesUtils', () => {
it('should filter collaterals correctly', async function () {
it('should filter collaterals correctly', async () => {
const actual = getCollaterals({ PIP_ETH: '', PIP_XYZ: '', XYZ_WBTC: '' }, supportedIlks)
expect(actual).to.be.not.undefined
expect(actual.length).to.be.equal(1)
expect(actual[0]).to.be.equal('ETH')
expect(actual).toBeDefined()
expect(actual.length).toBe(1)
expect(actual[0]).toBe('ETH')
})
it('should filter collateral tokens correctly', async function () {
it('should filter collateral tokens correctly', async () => {
const collaterals = getCollaterals(mainnet, supportedIlks)
const tokens = getCollateralTokens(mainnet, supportedIlks)
expect(Object.keys(tokens).length).to.be.equal(collaterals.length)
expect(Object.keys(tokens).length).toBe(collaterals.length)
Object.keys(tokens).forEach((element, idx) => {
expect(element).to.be.equal(collaterals[idx])
expect(element).toBe(collaterals[idx])
})
})
})
13 changes: 6 additions & 7 deletions blockchain/calls/proxyActions/proxyActionsResolver.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { MakerVaultType } from 'blockchain/calls/vaultResolver'
import { expect } from 'chai'
import { getStateUnpacker } from 'helpers/testHelpers'

import { ProxyActionsAdapterType } from './adapters/ProxyActionsSmartContractAdapterInterface'
Expand All @@ -12,21 +11,21 @@ describe('proxyActionsAdapterResolver', () => {

const state = getStateUnpacker(adapter$)

expect(state().AdapterType).to.eq(ProxyActionsAdapterType.CROPJOIN)
expect(state().AdapterType).toBe(ProxyActionsAdapterType.CROPJOIN)
})
it('returns default proxy actions for other ilks', () => {
const adapter$ = proxyActionsAdapterResolver$({ ilk: 'ETH-A' })

const state = getStateUnpacker(adapter$)

expect(state().AdapterType).to.eq(ProxyActionsAdapterType.STANDARD)
expect(state().AdapterType).toBe(ProxyActionsAdapterType.STANDARD)
})
it('throws when trying to open a charter vault', () => {
const adapter$ = proxyActionsAdapterResolver$({ ilk: 'INST-ETH-A' })

const state = getStateUnpacker(adapter$)

expect(state).to.throw(
expect(state).toThrow(
'can not create a proxy actions adapter from an INST-ETH-A ilk - adapter is not tested for opening vaults',
)
})
Expand All @@ -38,21 +37,21 @@ describe('proxyActionsAdapterResolver', () => {

const state = getStateUnpacker(adapter$)

expect(state().AdapterType).to.eq(ProxyActionsAdapterType.CROPJOIN)
expect(state().AdapterType).toBe(ProxyActionsAdapterType.CROPJOIN)
})
it('returns CHARTER proxy actions for CHARTER vault type', () => {
const adapter$ = proxyActionsAdapterResolver$({ makerVaultType: MakerVaultType.CHARTER })

const state = getStateUnpacker(adapter$)

expect(state().AdapterType).to.eq(ProxyActionsAdapterType.CHARTER)
expect(state().AdapterType).toBe(ProxyActionsAdapterType.CHARTER)
})
it('returns standard proxy actions in all other cases', () => {
const adapter$ = proxyActionsAdapterResolver$({ makerVaultType: MakerVaultType.STANDARD })

const state = getStateUnpacker(adapter$)

expect(state().AdapterType).to.eq(ProxyActionsAdapterType.STANDARD)
expect(state().AdapterType).toBe(ProxyActionsAdapterType.STANDARD)
})
})
})
14 changes: 6 additions & 8 deletions blockchain/calls/proxyActions/vaultActionsLogic.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { BigNumber } from 'bignumber.js'
import { TxMetaKind } from 'blockchain/calls/txMeta'
import { expect } from 'chai'
import { mockContextConnected } from 'helpers/mocks/context.mock'
import { one } from 'helpers/zero'
import { describe } from 'mocha'

import { MockProxyActionsSmartContractAdapter } from './adapters/mockProxyActionsSmartContractAdapter'
import { vaultActionsLogic } from './vaultActionsLogic'
Expand Down Expand Up @@ -52,8 +50,8 @@ describe('vaultActionsLogic', () => {
const actualMethodName = JSON.parse(methodCalled).method

it(testName, () => {
expect(actualMethodName).to.eq(expectedMethodName)
expect(proxyActionAddress).to.eq('0x-mock-dss-proxy-action-address')
expect(actualMethodName).toBe(expectedMethodName)
expect(proxyActionAddress).toBe('0x-mock-dss-proxy-action-address')
})
}

Expand Down Expand Up @@ -201,8 +199,8 @@ describe('vaultActionsLogic', () => {
const actualMethodName = JSON.parse(methodCalled).method

it(testName, () => {
expect(actualMethodName).to.eq(expectedMethodCalled)
expect(proxyActionAddress).to.eq('0x-mock-dss-proxy-action-address')
expect(actualMethodName).toBe(expectedMethodCalled)
expect(proxyActionAddress).toBe('0x-mock-dss-proxy-action-address')
})
}

Expand Down Expand Up @@ -298,8 +296,8 @@ describe('vaultActionsLogic', () => {
const actualMethodName = JSON.parse(methodCalled).method

it(testName, () => {
expect(actualMethodName).to.eq(expectedMethod)
expect(proxyActionAddress).to.eq('0x-mock-dss-proxy-action-address')
expect(actualMethodName).toBe(expectedMethod)
expect(proxyActionAddress).toBe('0x-mock-dss-proxy-action-address')
})
}

Expand Down
3 changes: 1 addition & 2 deletions blockchain/collateral.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import BigNumber from 'bignumber.js'
import { expect } from 'chai'
import { getStateUnpacker } from 'helpers/testHelpers'
import { Observable, of, throwError } from 'rxjs'

Expand Down Expand Up @@ -31,7 +30,7 @@ describe('getTotalValueLocked$', () => {

const state = getStateUnpacker(result)()

expect(state).to.eql({
expect(state).toEqual({
value: new BigNumber(6),
})
})
Expand Down
11 changes: 5 additions & 6 deletions blockchain/ilks.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { expect } from 'chai'
import {
debtScalingFactor$,
DEFAULT_DEBT_SCALING_FACTOR,
Expand All @@ -14,18 +13,18 @@ describe('ilkData$', () => {
it('should produce IlkData state', () => {
const state = mockIlkData()

expect(state()).to.not.be.undefined
expect(state()).toBeDefined()
})

it('should account for accrued debt', () => {
const state = mockIlkData()

expect(state().ilkDebt).to.deep.equal(defaultIlkDebt)
expect(state().ilkDebt).toEqual(defaultIlkDebt)
const expectedIlkDebtAvailable = defaultIlkDebt.times(2.5).minus(defaultIlkDebt)
expect(state().ilkDebtAvailable).to.deep.equal(expectedIlkDebtAvailable)
expect(state().ilkDebtAvailable).toEqual(expectedIlkDebtAvailable)

debtScalingFactor$.next(RANDOM_DEBT_SCALING_FACTOR)
expect(state().ilkDebt.gt(defaultIlkDebt)).to.be.true
expect(state().ilkDebtAvailable.lt(expectedIlkDebtAvailable)).to.be.true
expect(state().ilkDebt.gt(defaultIlkDebt)).toBe(true)
expect(state().ilkDebtAvailable.lt(expectedIlkDebtAvailable)).toBe(true)
})
})
7 changes: 3 additions & 4 deletions blockchain/instiVault.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BigNumber } from 'bignumber.js'
import { expect } from 'chai'
import { mockVault$ } from 'helpers/mocks/vaults.mock'
import { getStateUnpacker } from 'helpers/testHelpers'
import { zero } from 'helpers/zero'
Expand Down Expand Up @@ -30,8 +29,8 @@ describe('instiVault$', () => {
charterPeace$.next(new BigNumber(6))
charterUline$.next(new BigNumber(11))

expect(state().originationFeePercent.toString()).to.eq('2')
expect(state().activeCollRatio.toString()).to.eq('6')
expect(state().originationFeePercent.toString()).toBe('2')
expect(state().activeCollRatio.toString()).toBe('6')
})

it('takes the debt ceiling/available ilk debt from the charter contract', () => {
Expand All @@ -47,6 +46,6 @@ describe('instiVault$', () => {
})

const state = getStateUnpacker(instiVault$)
expect(state().daiYieldFromLockedCollateral.toString()).to.eq('800000000')
expect(state().daiYieldFromLockedCollateral.toString()).toBe('800000000')
})
})
Loading

0 comments on commit e19e6bc

Please sign in to comment.