Skip to content

Commit

Permalink
🧸 Remove waffle from tests (#1134)
Browse files Browse the repository at this point in the history
* Remove Waffle from dependencies

* Make tests work in core

* Don't call usEthers twice in tests

* Fix uniswap package

* Fix example package

* Lint

* Add playwright setup to E2E tests

* Add @usedapp/testing to example dependencies

* Add @usedapp-testing dependency to example-next
  • Loading branch information
yivlad committed Aug 4, 2023
1 parent b4b7b94 commit 54f7e26
Show file tree
Hide file tree
Showing 39 changed files with 544 additions and 884 deletions.
3 changes: 1 addition & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@
"chai-as-promised": "^7.1.1",
"eslint": "7.22.0",
"eslint-plugin-react-hooks": "^4.3.0",
"ethereum-waffle": "4.0.4",
"ethers": "5.6.9",
"ganache": "7.0.3",
"ganache": "7.9.0",
"jsdom": "^16.4.0",
"jsdom-global": "^3.0.2",
"mocha": "^8.2.1",
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/helpers/logs.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Filter, FilterByBlockHash, Log } from '@ethersproject/abstract-provider'
import { constants } from 'ethers'
import { expect } from 'chai'
import { MockProvider } from 'ethereum-waffle'
import { BigNumber, Contract, ethers } from 'ethers'
import { TypedFilter } from '../hooks'
import { deployMockToken } from '../testing'
import { MockProvider, deployMockToken } from '../testing'
import { decodeLogs, encodeFilterData, LogsResult } from './logs'

const AddressZero = constants.AddressZero
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/hooks/estimateGasLimit.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { expect } from 'chai'
import { MockProvider } from 'ethereum-waffle'
import { Contract } from 'ethers'
import { deployMockToken } from '../testing'
import { MockProvider, deployMockToken } from '../testing'
import { estimateContractFunctionGasLimit, estimateTransactionGasLimit } from './usePromiseTransaction'

const BASE_TX_COST = 21000
Expand Down
11 changes: 4 additions & 7 deletions packages/core/src/hooks/threeChains.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
/* eslint react-hooks/rules-of-hooks: 0 */
import { MockProvider } from 'ethereum-waffle'
import { Contract, providers, Wallet } from 'ethers'
import { useCall, useCalls } from './useCall'
import { SECOND_TEST_CHAIN_ID, renderDAppHook, waitUntil } from '../testing'
import { MockProvider, SECOND_TEST_CHAIN_ID, renderDAppHook, waitUntil } from '../testing'
import { BigNumber, constants } from 'ethers'
import { deployContract, solidity } from 'ethereum-waffle'
import { doublerContractABI, MultiCall, timestampContractABI } from '../constants/abi'
import { expect, use } from 'chai'
import { expect } from 'chai'
import { randomInt } from 'crypto'

use(solidity)
import { deployContract } from '../testing/utils/deployContract'

const FIRST_TEST_CHAIN_ID = 1337
const THIRD_TEST_CHAIN_ID = 31338
Expand Down Expand Up @@ -43,7 +40,7 @@ describe('useCall - three chains', () => {
}

for (const chainId of chainIds) {
const provider = new MockProvider({ ganacheOptions: { chain: { chainId } } })
const provider = new MockProvider({ chainId })
const [deployer] = provider.getWallets()
chains[chainId] = {
provider,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/hooks/useBlockMeta.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('useBlockMeta', () => {
await sleep(1000)
await network1.mineBlock()
await waitForCurrent((val) => val.timestamp?.getTime() !== firstTimestamp?.getTime())
if (!firstTimestamp) throw new Error('firstTimestamp is undefined')
expect(result.current.timestamp).to.be.greaterThan(firstTimestamp)
})

Expand All @@ -45,6 +46,7 @@ describe('useBlockMeta', () => {
await network1.wallets[0].sendTransaction({ to: receiver, value: 100 })
await sleep(1000)
await waitForCurrent((val) => val.timestamp?.getTime() !== firstTimestamp?.getTime())
if (!firstTimestamp) throw new Error('firstTimestamp is undefined')
expect(result.current.timestamp).to.be.greaterThan(firstTimestamp)
})

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/hooks/useCall.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {
getResultPropertyError,
TestingNetwork,
} from '../testing'
import { deployContract } from 'ethereum-waffle'
import { BlockNumberContract, reverterContractABI, doublerContractABI, Config } from '../constants'
import waitForExpect from 'wait-for-expect'
import { errorsContractABI } from '../constants/abi/errors'
import { defaultMulticall1ErrorMessage } from '../abi/multicall/constants'
import { deployContract } from '../testing/utils/deployContract'

describe('useCall', () => {
for (const multicallVersion of [1, 2] as const) {
Expand Down
11 changes: 10 additions & 1 deletion packages/core/src/hooks/useCallResilency.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { expect } from 'chai'
import { defaultAccounts, deployContract } from 'ethereum-waffle'
import { Config, doublerContractABI, reverterContractABI } from '../constants'
import multicall2ABI from '../constants/abi/MultiCall2.json'
import { useBlockMeta, useCall, useEthers } from '../hooks'
import { renderDAppHook, setupTestingConfig, sleep } from '../testing'

import { constants, providers, Wallet } from 'ethers'
import Ganache, { Server } from 'ganache'
import { deployContract } from '../testing/utils/deployContract'

describe('useCall Resilency tests', () => {
for (const multicallVersion of [1, 2] as const) {
Expand Down Expand Up @@ -89,6 +89,15 @@ describe('useCall Resilency tests', () => {
let miners: Wallet[]
let config: Config

const balance = '0x1ED09BEAD87C0378D8E6400000000' // 10^34
const privateKeys = [
'0x29f3edee0ad3abf8e2699402e0e28cd6492c9be7eaab00d732a791c33552f797',
'0x5c8b9227cd5065c7e3f6b73826b8b42e198c4497f6688e3085d5ab3a6d520e74',
'0x50c8b3fc81e908501c8cd0a60911633acaca1a567d1be8e769c5ae7007b34b23',
'0x706618637b8ca922f6290ce1ecd4c31247e9ab75cf0530a0ac95c0332173d7c5',
]
const defaultAccounts = privateKeys.map((secretKey) => ({ balance, secretKey }))

beforeEach(async () => {
ganacheServers = [
Ganache.server({
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/hooks/useContractFunction.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('useContractFunction', () => {
await result.current.send(spender.address, 10)
await waitForCurrent((val) => val.state !== undefined)

expect(result.current.state.status).to.eq('Exception')
expect(result.current.state.status).to.eq('Fail')
expect(result.current.state.transactionName).to.eq('Approve')
expect(result.current.state.errorMessage).to.eq('transaction failed')
})
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/hooks/useEthers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { providers, Wallet } from 'ethers'
import { useEffect } from 'react'
import { Config } from '../constants'
import { Mainnet, Mumbai } from '../model'
import { renderDAppHook, setupTestingConfig, sleep, TestingNetwork } from '../testing'
import { MockProvider, renderDAppHook, setupTestingConfig, sleep, TestingNetwork } from '../testing'
import { useEthers } from './useEthers'
import Ganache, { Server } from 'ganache'

Expand Down Expand Up @@ -113,12 +113,12 @@ describe('useEthers', () => {
it('return signer if library is type of JsonRpcProvider', async () => {
const { result, waitForCurrent } = await renderDAppHook(
() => {
const { activate } = useEthers()
const { activate, library, error, isLoading } = useEthers()
useEffect(() => {
void activate(network1.provider)
}, [])

return useEthers()
return { library, error, isLoading }
},
{ config }
)
Expand All @@ -129,7 +129,7 @@ describe('useEthers', () => {
const signer = provider && 'getSigner' in provider ? provider.getSigner() : undefined

expect(result.current.error).to.be.undefined
expect(result.current.library).to.be.instanceOf(providers.JsonRpcProvider)
expect(result.current.library).to.be.instanceOf(MockProvider)
expect(signer).to.be.instanceOf(providers.JsonRpcSigner)
})

Expand Down
58 changes: 46 additions & 12 deletions packages/core/src/hooks/useSendTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,24 @@ describe('useSendTransaction', () => {

await waitForCurrent((val) => val.state !== undefined)
expect(result.current.state.status).to.eq('Success')
await expect(await network1.provider.getTransaction(receipt!.transactionHash)).to.changeEtherBalances(
[network1.deployer, wallet1],
['-10', '10']
const txReceipt = await network1.provider.getTransactionReceipt(receipt!.transactionHash)
const txFee = txReceipt?.cumulativeGasUsed?.mul(txReceipt?.effectiveGasPrice)
const deployerBalanceBeforeTransaction = await network1.provider.getBalance(
network1.deployer.address,
receipt!.blockNumber - 1
)
const wallet1BalanceBeforeTransaction = await network1.provider.getBalance(
wallet1.address,
receipt!.blockNumber - 1
)
const deployerBalanceAfterTransaction = await network1.provider.getBalance(
network1.deployer.address,
receipt!.blockNumber
)
const wallet1BalanceAfterTransaction = await network1.provider.getBalance(wallet1.address, receipt!.blockNumber)

expect(deployerBalanceAfterTransaction).to.eq(deployerBalanceBeforeTransaction.sub(10).sub(txFee ?? 0))
expect(wallet1BalanceAfterTransaction).to.eq(wallet1BalanceBeforeTransaction.add(10))
})

it('sends with different signer', async () => {
Expand All @@ -63,7 +77,6 @@ describe('useSendTransaction', () => {
it('Exception(invalid sender)', async () => {
const { result, waitForCurrent, waitForNextUpdate } = await renderDAppHook(useSendTransaction, { config })
await waitForNextUpdate()

await result.current.sendTransaction({ to: '0x1', value: utils.parseEther('1') })
await waitForCurrent((val) => val.state !== undefined)
expect(result.current.state.status).to.eq('Exception')
Expand Down Expand Up @@ -138,13 +151,10 @@ describe('useSendTransaction', () => {
)
await waitForNextUpdate()

const receipt = await result.current.sendTransaction({ to: wallet2.address, value: BigNumber.from(10) })
await result.current.sendTransaction({ to: wallet2.address, value: BigNumber.from(10) })

await waitForCurrent((val) => val.state !== undefined)
expect(result.current.state.status).to.eq('Success')
const tx = await network1.provider.getTransaction(receipt!.transactionHash)
await expect(tx).to.changeEtherBalances([wallet1, wallet2], ['-10', '10'])

expect(result.current.state.receipt).to.not.be.undefined
expect(result.current.state.receipt?.to).to.eq(wallet2.address)
expect(result.current.state.receipt?.from).to.eq(wallet1.address)
Expand All @@ -164,10 +174,22 @@ describe('useSendTransaction', () => {

const receipt = await result.current.sendTransaction({ to: wallet2.address, value: BigNumber.from(10) })

const txFee = receipt?.gasUsed.mul(receipt?.effectiveGasPrice ?? 0)
await waitForCurrent((val) => val.state !== undefined)
expect(result.current.state.status).to.eq('Success')
const tx = await network1.provider.getTransaction(receipt!.transactionHash)
await expect(tx).to.changeEtherBalances([wallet1, wallet2], ['-10', '10'])
const wallet1BalanceBeforeTransaction = await network1.provider.getBalance(
wallet1.address,
receipt!.blockNumber - 1
)
const wallet2BalanceBeforeTransaction = await network1.provider.getBalance(
wallet2.address,
receipt!.blockNumber - 1
)
const wallet1BalanceAfterTransaction = await network1.provider.getBalance(wallet1.address, receipt!.blockNumber)
const wallet2BalanceAfterTransaction = await network1.provider.getBalance(wallet2.address, receipt!.blockNumber)

expect(wallet1BalanceAfterTransaction).to.eq(wallet1BalanceBeforeTransaction.sub(10).sub(txFee ?? 0))
expect(wallet2BalanceAfterTransaction).to.eq(wallet2BalanceBeforeTransaction.add(10))

expect(result.current.state.receipt).to.not.be.undefined
expect(result.current.state.receipt?.to).to.eq(wallet2.address)
Expand All @@ -194,10 +216,22 @@ describe('useSendTransaction', () => {

const receipt = await result.current.sendTransaction({ to: wallet2.address, value: BigNumber.from(10) })

const txFee = receipt?.gasUsed.mul(receipt.effectiveGasPrice ?? 0)
await waitForCurrent((val) => val.state !== undefined)
expect(result.current.state.status).to.eq('Success')
const tx = await network1.provider.getTransaction(receipt!.transactionHash)
await expect(tx).to.changeEtherBalances([wallet1, wallet2], ['-10', '10'])
const wallet1BalanceBeforeTransaction = await network1.provider.getBalance(
wallet1.address,
receipt!.blockNumber - 1
)
const wallet2BalanceBeforeTransaction = await network1.provider.getBalance(
wallet2.address,
receipt!.blockNumber - 1
)
const wallet1BalanceAfterTransaction = await network1.provider.getBalance(wallet1.address, receipt!.blockNumber)
const wallet2BalanceAfterTransaction = await network1.provider.getBalance(wallet2.address, receipt!.blockNumber)

expect(wallet1BalanceAfterTransaction).to.eq(wallet1BalanceBeforeTransaction.sub(10).sub(txFee ?? 0))
expect(wallet2BalanceAfterTransaction).to.eq(wallet2BalanceBeforeTransaction.add(10))

expect(result.current.state.receipt).to.not.be.undefined
expect(result.current.state.receipt?.to).to.eq(wallet2.address)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { MockProvider } from 'ethereum-waffle'
import { utils } from 'ethers'
import { Contract } from 'ethers'
import chai, { expect } from 'chai'
import { deployContract, solidity } from 'ethereum-waffle'
import chaiAsPromised from 'chai-as-promised'
import { RawCall, ERC20Mock, MultiCall } from '../../..'
import { BigNumber } from 'ethers'
import { sendEmptyTx } from '../../../testing/utils/sendEmptyTx'
import { multicall1Factory } from './multicall'
import { MockProvider } from '../../../testing'
import { deployContract } from '../../../testing/utils/deployContract'

chai.use(solidity)
chai.use(chaiAsPromised)

const Interface = utils.Interface
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { MockProvider } from 'ethereum-waffle'
import { utils } from 'ethers'
import { Contract } from 'ethers'
import chai, { expect } from 'chai'
import { deployContract, solidity } from 'ethereum-waffle'
import chaiAsPromised from 'chai-as-promised'
import { BigNumber } from 'ethers'
import { ERC20Mock, MultiCall2 } from '../../../constants'
import { RawCall } from './callsReducer'
import { multicall2Factory } from './multicall2'
import { sendEmptyTx } from '../../../testing/utils/sendEmptyTx'
import { MockProvider } from '../../../testing'
import { deployContract } from '../../../testing/utils/deployContract'

chai.use(solidity)
chai.use(chaiAsPromised)

const Interface = utils.Interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from 'chai'
import { providers } from 'ethers'
import { getProvidersFromConfig } from './provider'
import { Kovan, Mainnet, Rinkeby, Localhost } from '../../../model/chain'
import { MockProvider } from 'ethereum-waffle'
import { MockProvider } from '../../../testing'

const JsonRpcProvider = providers.JsonRpcProvider

Expand Down
Loading

3 comments on commit 54f7e26

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.