diff --git a/index.ts b/index.ts index 77746d5..d177b06 100644 --- a/index.ts +++ b/index.ts @@ -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' diff --git a/src/tests.ts b/src/tests.ts new file mode 100644 index 0000000..c705a24 --- /dev/null +++ b/src/tests.ts @@ -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 +): Promise { + 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 +): Promise { + 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 +): Promise { + 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] +} diff --git a/tests.ts b/tests.ts new file mode 100644 index 0000000..c4dfb7b --- /dev/null +++ b/tests.ts @@ -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) diff --git a/tsconfig.json b/tsconfig.json index c252843..6aa7a32 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,6 +10,7 @@ }, "files": [ "index.ts", + "tests.ts", "hardhat.config.ts" ] }