Skip to content

Commit

Permalink
tests: add hardhat fork test tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
facuspagnuolo committed Sep 16, 2024
1 parent 5389109 commit 46d9787
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export * from './src/contracts'
export * from './src/mocks'
export * from './src/numbers'
export * from './src/signers'
export * from './src/tests'
export * from './src/time'
73 changes: 73 additions & 0 deletions src/tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { TASK_TEST_GET_TEST_FILES, TASK_TEST_RUN_MOCHA_TESTS } from 'hardhat/builtin-tasks/task-names'
import { HardhatNetworkConfig, HardhatRuntimeEnvironment, HttpNetworkConfig, RunSuperFunction } from 'hardhat/types'

/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */

export async function overrideTestTask(
args: any,
hre: HardhatRuntimeEnvironment,
run: RunSuperFunction<any>
): Promise<void> {
const files = await hre.run(TASK_TEST_GET_TEST_FILES, { testFiles: args.testFiles })
if (hre.network.name === 'hardhat' && args.fork) await runForkTests(args, files, hre, run)
else await runNormalTests(args, files, hre, run)
}

async function runNormalTests(
args: any,
files: string[],
hre: HardhatRuntimeEnvironment,
run: RunSuperFunction<any>
): Promise<void> {
console.log('Running normal tests...')
if (args.fork) throw Error('Cannot run normal tests with a forked network')
args.testFiles = files.filter((file: string) => file.endsWith('.test.ts'))
if (args.testFiles.length == 0) return hre.run(TASK_TEST_RUN_MOCHA_TESTS, { testFiles: [] })

await run(args)
}

async function runForkTests(
args: any,
files: string[],
hre: HardhatRuntimeEnvironment,
run: RunSuperFunction<any>
): Promise<void> {
console.log(`Running fork tests on ${args.fork}...`)
if (args.fork === 'hardhat') throw Error('Cannot fork local networks')

args.testFiles = files.filter((file: string) => file.endsWith(`.${args.fork}.ts`) || file.endsWith(`.fork.ts`))
if (args.testFiles.length == 0) return hre.run(TASK_TEST_RUN_MOCHA_TESTS, { testFiles: [] })

const forkingNetworkName = Object.keys(hre.config.networks).find((networkName) => networkName === args.fork)
if (!forkingNetworkName) throw Error(`Could not find a config for network ${args.fork} to be forked`)

const forkingNetworkConfig = hre.config.networks[forkingNetworkName] as HttpNetworkConfig
if (!forkingNetworkConfig.url) throw Error(`Could not find a RPC url in network config for ${forkingNetworkName}`)

if (args.chainId) hre.config.networks.hardhat.chainId = args.chainId

await hre.network.provider.request({
method: 'hardhat_reset',
params: [{ forking: { jsonRpcUrl: forkingNetworkConfig.url, blockNumber: args.blockNumber } }],
})

const config = hre.network.config as HardhatNetworkConfig
config.forking = { enabled: true, blockNumber: args.blockNumber, url: forkingNetworkConfig.url, httpHeaders: {} }

await run(args)
}

export function getForkedNetwork(hre: HardhatRuntimeEnvironment): string {
const config = hre.network.config as HardhatNetworkConfig
if (!config.forking || !config.forking.url) throw Error(`No forks found on network ${hre.network.name}`)

const network = Object.entries(hre.config.networks).find(([, networkConfig]) => {
const httpNetworkConfig = networkConfig as HttpNetworkConfig
return httpNetworkConfig.url && httpNetworkConfig.url === config?.forking?.url
})

if (!network) throw Error(`No network found matching fork from ${config.forking.url}`)
return network[0]
}
11 changes: 11 additions & 0 deletions tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { TASK_TEST } from 'hardhat/builtin-tasks/task-names'
import { task, types } from 'hardhat/config'

import { overrideTestTask } from './src/tests'

task(TASK_TEST)
.addOptionalParam('fork', 'Optional network name to be forked in case of running fork tests.')
.addOptionalParam('forkIgnoreUnknownTxType', 'Optional flag to ignore unknown tx types.', false, types.boolean)
.addOptionalParam('chainId', 'Optional chain ID to overwrite hardhat local network ID.', undefined, types.int)
.addOptionalParam('blockNumber', 'Optional block number to fork in case of running fork tests.', undefined, types.int)
.setAction(overrideTestTask)
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"files": [
"index.ts",
"tests.ts",
"hardhat.config.ts"
]
}

0 comments on commit 46d9787

Please sign in to comment.