|
| 1 | +import type { HardhatConfig } from "@ignored/hardhat-vnext-core/types/config"; |
| 2 | +import type { NewTaskActionFunction } from "@ignored/hardhat-vnext-core/types/tasks"; |
| 3 | +import type { MochaOptions } from "mocha"; |
| 4 | + |
| 5 | +import { resolve as pathResolve } from "node:path"; |
| 6 | + |
| 7 | +import { HardhatError } from "@ignored/hardhat-vnext-errors"; |
| 8 | +import { findUp, getAllFilesMatching } from "@ignored/hardhat-vnext-utils/fs"; |
| 9 | +import { readClosestPackageJson } from "@ignored/hardhat-vnext-utils/package"; |
| 10 | + |
| 11 | +interface TestActionArguments { |
| 12 | + testFiles: string[]; |
| 13 | +} |
| 14 | + |
| 15 | +function isTypescriptFile(path: string): boolean { |
| 16 | + return /\.(ts|cts|mts)$/i.test(path); |
| 17 | +} |
| 18 | + |
| 19 | +function isJavascriptFile(path: string): boolean { |
| 20 | + return /\.(js|cjs|mjs)$/i.test(path); |
| 21 | +} |
| 22 | + |
| 23 | +async function getTestFiles( |
| 24 | + testFiles: string[], |
| 25 | + config: HardhatConfig, |
| 26 | +): Promise<string[]> { |
| 27 | + if (testFiles.length !== 0) { |
| 28 | + const testFilesAbsolutePaths = testFiles.map((x) => |
| 29 | + pathResolve(process.cwd(), x), |
| 30 | + ); |
| 31 | + |
| 32 | + return testFilesAbsolutePaths; |
| 33 | + } |
| 34 | + |
| 35 | + return getAllFilesMatching( |
| 36 | + config.paths.tests, |
| 37 | + (f) => isJavascriptFile(f) || isTypescriptFile(f), |
| 38 | + ); |
| 39 | +} |
| 40 | + |
| 41 | +async function hasTypescriptConfig(): Promise<boolean> { |
| 42 | + const hhConfig = await findUp("hardhat.config.ts"); |
| 43 | + |
| 44 | + return hhConfig !== undefined; |
| 45 | +} |
| 46 | + |
| 47 | +let testsAlreadyRun = false; |
| 48 | +const testWithHardhat: NewTaskActionFunction<TestActionArguments> = async ( |
| 49 | + { testFiles }, |
| 50 | + hre, |
| 51 | +) => { |
| 52 | + const files = await getTestFiles(testFiles, hre.config); |
| 53 | + |
| 54 | + // the second check is needed for the case of a user having a hardhat.config.ts file |
| 55 | + // but all their test files are js files. probably an edge case, but we should handle it |
| 56 | + if (files.some((f) => isTypescriptFile(f)) || (await hasTypescriptConfig())) { |
| 57 | + try { |
| 58 | + import.meta.resolve("typescript"); |
| 59 | + } catch { |
| 60 | + throw new HardhatError( |
| 61 | + HardhatError.ERRORS.GENERAL.TYPESCRIPT_NOT_INSTALLED, |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + try { |
| 66 | + import.meta.resolve("tsx"); |
| 67 | + } catch { |
| 68 | + throw new HardhatError(HardhatError.ERRORS.GENERAL.TSX_NOT_INSTALLED); |
| 69 | + } |
| 70 | + |
| 71 | + process.env.NODE_OPTIONS = "--import tsx"; |
| 72 | + } |
| 73 | + |
| 74 | + const { default: Mocha } = await import("mocha"); |
| 75 | + |
| 76 | + const mochaConfig: MochaOptions = { ...hre.config.mocha }; |
| 77 | + |
| 78 | + const mocha = new Mocha(mochaConfig); |
| 79 | + |
| 80 | + files.forEach((file) => mocha.addFile(file)); |
| 81 | + |
| 82 | + // if the project is of type "module" or if there's some ESM test file, |
| 83 | + // we call loadFilesAsync to enable Mocha's ESM support |
| 84 | + const projectPackageJson = await readClosestPackageJson(import.meta.url); |
| 85 | + const isTypeModule = projectPackageJson.type === "module"; |
| 86 | + const hasEsmTest = files.some((file) => file.endsWith(".mjs")); |
| 87 | + |
| 88 | + if (isTypeModule || hasEsmTest) { |
| 89 | + // Because of the way the ESM cache works, loadFilesAsync doesn't work |
| 90 | + // correctly if used twice within the same process, so we throw an error |
| 91 | + // in that case |
| 92 | + if (testsAlreadyRun) { |
| 93 | + throw new HardhatError( |
| 94 | + HardhatError.ERRORS.BUILTIN_TASKS.TEST_TASK_ESM_TESTS_RUN_TWICE, |
| 95 | + ); |
| 96 | + } |
| 97 | + testsAlreadyRun = true; |
| 98 | + |
| 99 | + // This instructs Mocha to use the more verbose file loading infrastructure |
| 100 | + // which supports both ESM and CJS |
| 101 | + await mocha.loadFilesAsync(); |
| 102 | + } |
| 103 | + |
| 104 | + await new Promise<number>((resolve) => { |
| 105 | + mocha.run(resolve); |
| 106 | + }); |
| 107 | +}; |
| 108 | + |
| 109 | +export default testWithHardhat; |
0 commit comments