-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5389109
commit 46d9787
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
}, | ||
"files": [ | ||
"index.ts", | ||
"tests.ts", | ||
"hardhat.config.ts" | ||
] | ||
} |