From fadfc05c6cc112ea133620184a510f7973ad01f5 Mon Sep 17 00:00:00 2001 From: Luis Schaab Date: Thu, 9 Oct 2025 17:49:00 +0000 Subject: [PATCH] feat: replace direct usage of markTestRunStart with onTestRunStart hook --- v-next/hardhat-mocha/src/task-action.ts | 9 +++------ v-next/hardhat-node-test-runner/src/task-action.ts | 5 +++-- .../builtin-plugins/coverage/hook-handlers/test.ts | 14 ++++++++++++++ .../src/internal/builtin-plugins/coverage/index.ts | 1 + .../builtin-plugins/solidity-test/task-action.ts | 9 +++------ .../builtin-plugins/test/type-extensions.ts | 2 ++ 6 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 v-next/hardhat/src/internal/builtin-plugins/coverage/hook-handlers/test.ts diff --git a/v-next/hardhat-mocha/src/task-action.ts b/v-next/hardhat-mocha/src/task-action.ts index d1f8ef27e27..1abc6722ea5 100644 --- a/v-next/hardhat-mocha/src/task-action.ts +++ b/v-next/hardhat-mocha/src/task-action.ts @@ -7,11 +7,7 @@ import { resolve as pathResolve } from "node:path"; import { HardhatError } from "@nomicfoundation/hardhat-errors"; import { setGlobalOptionsAsEnvVariables } from "@nomicfoundation/hardhat-utils/env"; import { getAllFilesMatching } from "@nomicfoundation/hardhat-utils/fs"; -import { - markTestRunDone, - markTestRunStart, - markTestWorkerDone, -} from "hardhat/internal/coverage"; +import { markTestRunDone, markTestWorkerDone } from "hardhat/internal/coverage"; interface TestActionArguments { testFiles: string[]; @@ -120,7 +116,8 @@ const testWithHardhat: NewTaskActionFunction = async ( // which supports both ESM and CJS await mocha.loadFilesAsync(); - await markTestRunStart("mocha"); + // await markTestRunStart("mocha"); + await hre.hooks.runParallelHandlers("test", "onTestRunStart", ["mocha"]); const testFailures = await new Promise((resolve) => { mocha.run(resolve); diff --git a/v-next/hardhat-node-test-runner/src/task-action.ts b/v-next/hardhat-node-test-runner/src/task-action.ts index e9ffbe3e915..15a78884cef 100644 --- a/v-next/hardhat-node-test-runner/src/task-action.ts +++ b/v-next/hardhat-node-test-runner/src/task-action.ts @@ -10,7 +10,7 @@ import { hardhatTestReporter } from "@nomicfoundation/hardhat-node-test-reporter import { setGlobalOptionsAsEnvVariables } from "@nomicfoundation/hardhat-utils/env"; import { getAllFilesMatching } from "@nomicfoundation/hardhat-utils/fs"; import { createNonClosingWriter } from "@nomicfoundation/hardhat-utils/stream"; -import { markTestRunStart, markTestRunDone } from "hardhat/internal/coverage"; +import { markTestRunDone } from "hardhat/internal/coverage"; interface TestActionArguments { testFiles: string[]; @@ -130,7 +130,8 @@ const testWithHardhat: NewTaskActionFunction = async ( return failures; } - await markTestRunStart("nodejs"); + // await markTestRunStart("nodejs"); + await hre.hooks.runParallelHandlers("test", "onTestRunStart", ["nodejs"]); const testFailures = await runTests(); diff --git a/v-next/hardhat/src/internal/builtin-plugins/coverage/hook-handlers/test.ts b/v-next/hardhat/src/internal/builtin-plugins/coverage/hook-handlers/test.ts new file mode 100644 index 00000000000..f7402ae3f81 --- /dev/null +++ b/v-next/hardhat/src/internal/builtin-plugins/coverage/hook-handlers/test.ts @@ -0,0 +1,14 @@ +import type { TestHooks } from "hardhat/types/hooks"; + +import { markTestRunStart } from "../helpers.js"; + +export default async (): Promise> => { + const handlers: Partial = { + onTestRunStart: async (_context, id) => { + console.log(`onTestRunStart coverage hook handler called with id: ${id}`); + await markTestRunStart(id); + }, + }; + + return handlers; +}; diff --git a/v-next/hardhat/src/internal/builtin-plugins/coverage/index.ts b/v-next/hardhat/src/internal/builtin-plugins/coverage/index.ts index c902a8e7d96..7ff67358d34 100644 --- a/v-next/hardhat/src/internal/builtin-plugins/coverage/index.ts +++ b/v-next/hardhat/src/internal/builtin-plugins/coverage/index.ts @@ -17,6 +17,7 @@ const hardhatPlugin: HardhatPlugin = { clean: () => import("./hook-handlers/clean.js"), hre: () => import("./hook-handlers/hre.js"), solidity: () => import("./hook-handlers/solidity.js"), + test: () => import("./hook-handlers/test.js"), }, npmPackage: "hardhat", }; diff --git a/v-next/hardhat/src/internal/builtin-plugins/solidity-test/task-action.ts b/v-next/hardhat/src/internal/builtin-plugins/solidity-test/task-action.ts index b92c2df2d61..63d67157c71 100644 --- a/v-next/hardhat/src/internal/builtin-plugins/solidity-test/task-action.ts +++ b/v-next/hardhat/src/internal/builtin-plugins/solidity-test/task-action.ts @@ -19,11 +19,7 @@ import { createNonClosingWriter } from "@nomicfoundation/hardhat-utils/stream"; import { HardhatRuntimeEnvironmentImplementation } from "../../core/hre.js"; import { isSupportedChainType } from "../../edr/chain-type.js"; import { ArtifactManagerImplementation } from "../artifacts/artifact-manager.js"; -import { - markTestRunDone, - markTestRunStart, - markTestWorkerDone, -} from "../coverage/helpers.js"; +import { markTestRunDone, markTestWorkerDone } from "../coverage/helpers.js"; import { getEdrArtifacts, getBuildInfos } from "./edr-artifacts.js"; import { @@ -148,7 +144,8 @@ const runSolidityTests: NewTaskActionFunction = async ( const options: RunOptions = solidityTestConfigToRunOptions(solidityTestConfig); - await markTestRunStart("solidity"); + // await markTestRunStart("solidity"); + await hre.hooks.runParallelHandlers("test", "onTestRunStart", ["solidity"]); const runStream = run( chainType, diff --git a/v-next/hardhat/src/internal/builtin-plugins/test/type-extensions.ts b/v-next/hardhat/src/internal/builtin-plugins/test/type-extensions.ts index 28f015820dc..6b4cc0ca7b1 100644 --- a/v-next/hardhat/src/internal/builtin-plugins/test/type-extensions.ts +++ b/v-next/hardhat/src/internal/builtin-plugins/test/type-extensions.ts @@ -39,5 +39,7 @@ declare module "../../../types/hooks.js" { filePath: string, ) => Promise, ) => Promise; + + onTestRunStart(context: HookContext, id: string): Promise; } }