diff --git a/v-next/hardhat/package.json b/v-next/hardhat/package.json index b9ebb4e87d9..c41ed6d5c3b 100644 --- a/v-next/hardhat/package.json +++ b/v-next/hardhat/package.json @@ -37,6 +37,8 @@ "./types/utils": "./dist/src/types/utils.js", "./types/solidity": "./dist/src/types/solidity.js", "./console.sol": "./console.sol", + "./internal/coverage": "./dist/src/internal/builtin-plugins/coverage/exports.js", + "./internal/gas-analytics": "./dist/src/internal/builtin-plugins/gas-analytics/exports.js", "./utils/contract-names": "./dist/src/utils/contract-names.js", "./utils/result": "./dist/src/utils/result.js", "./types/runtime": "./dist/src/internal/deprecated-module-imported-from-hardhat2-plugin.js", diff --git a/v-next/hardhat/src/internal/builtin-plugins/coverage/exports.ts b/v-next/hardhat/src/internal/builtin-plugins/coverage/exports.ts new file mode 100644 index 00000000000..021edb014d4 --- /dev/null +++ b/v-next/hardhat/src/internal/builtin-plugins/coverage/exports.ts @@ -0,0 +1,5 @@ +export { + markTestRunStart, + markTestRunDone, + markTestWorkerDone, +} from "./helpers.js"; diff --git a/v-next/hardhat/src/internal/builtin-plugins/coverage/helpers.ts b/v-next/hardhat/src/internal/builtin-plugins/coverage/helpers.ts index f45f2dcb731..023396fcf71 100644 --- a/v-next/hardhat/src/internal/builtin-plugins/coverage/helpers.ts +++ b/v-next/hardhat/src/internal/builtin-plugins/coverage/helpers.ts @@ -1,5 +1,32 @@ import path from "node:path"; +import { + testRunDone, + testRunStart, + testWorkerDone, +} from "./hook-handlers/test.js"; + export function getCoveragePath(rootPath: string): string { return path.join(rootPath, "coverage"); } + +/** + * The following helpers are kept for backward compatibility with older versions + * of test runner plugins (hardhat-mocha, hardhat-node-test-runner) that import + * from "hardhat/internal/coverage". + */ + +export async function markTestRunStart(id: string): Promise { + const { default: hre } = await import("../../../index.js"); + await testRunStart(hre, id); +} + +export async function markTestWorkerDone(id: string): Promise { + const { default: hre } = await import("../../../index.js"); + await testWorkerDone(hre, id); +} + +export async function markTestRunDone(id: string): Promise { + const { default: hre } = await import("../../../index.js"); + await testRunDone(hre, id); +} 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 index 08f8fb9c7ea..78fd84b20f4 100644 --- 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 @@ -1,43 +1,68 @@ -import type { TestHooks } from "../../../../types/hooks.js"; +import type { HookContext, TestHooks } from "../../../../types/hooks.js"; import { assertHardhatInvariant } from "@nomicfoundation/hardhat-errors"; +import { isObject } from "@nomicfoundation/hardhat-utils/lang"; -import { HardhatRuntimeEnvironmentImplementation } from "../../../core/hre.js"; +import { CoverageManagerImplementation } from "../coverage-manager.js"; export default async (): Promise> => ({ onTestRunStart: async (context, id, next) => { await next(context, id); - - if (context.globalOptions.coverage === true) { - assertHardhatInvariant( - context instanceof HardhatRuntimeEnvironmentImplementation, - "Expected context to be an instance of HardhatRuntimeEnvironmentImplementation", - ); - await context._coverage.clearData(id); - } + await testRunStart(context, id); }, onTestWorkerDone: async (context, id, next) => { await next(context, id); - - if (context.globalOptions.coverage === true) { - assertHardhatInvariant( - context instanceof HardhatRuntimeEnvironmentImplementation, - "Expected context to be an instance of HardhatRuntimeEnvironmentImplementation", - ); - await context._coverage.saveData(id); - } + await testWorkerDone(context, id); }, onTestRunDone: async (context, id, next) => { await next(context, id); - - if (context.globalOptions.coverage === true) { - assertHardhatInvariant( - context instanceof HardhatRuntimeEnvironmentImplementation, - "Expected context to be an instance of HardhatRuntimeEnvironmentImplementation", - ); - await context._coverage.report(id); - } + await testRunDone(context, id); }, }); + +export async function testRunStart( + context: HookContext, + id: string, +): Promise { + if (context.globalOptions.coverage === true) { + assertHardhatInvariant( + "_coverage" in context && + isObject(context._coverage) && + context._coverage instanceof CoverageManagerImplementation, + "Expected HookContext#_coverage to be an instance of CoverageManagerImplementation", + ); + await context._coverage.clearData(id); + } +} + +export async function testWorkerDone( + context: HookContext, + id: string, +): Promise { + if (context.globalOptions.coverage === true) { + assertHardhatInvariant( + "_coverage" in context && + isObject(context._coverage) && + context._coverage instanceof CoverageManagerImplementation, + "Expected HookContext#_coverage to be an instance of CoverageManagerImplementation", + ); + await context._coverage.saveData(id); + } +} + +export async function testRunDone( + context: HookContext, + id: string, +): Promise { + if (context.globalOptions.coverage === true) { + assertHardhatInvariant( + "_coverage" in context && + isObject(context._coverage) && + context._coverage instanceof CoverageManagerImplementation, + "Expected HookContext#_coverage to be an instance of CoverageManagerImplementation", + ); + await context._coverage.report(id); + } +} diff --git a/v-next/hardhat/src/internal/builtin-plugins/gas-analytics/exports.ts b/v-next/hardhat/src/internal/builtin-plugins/gas-analytics/exports.ts new file mode 100644 index 00000000000..021edb014d4 --- /dev/null +++ b/v-next/hardhat/src/internal/builtin-plugins/gas-analytics/exports.ts @@ -0,0 +1,5 @@ +export { + markTestRunStart, + markTestRunDone, + markTestWorkerDone, +} from "./helpers.js"; diff --git a/v-next/hardhat/src/internal/builtin-plugins/gas-analytics/helpers.ts b/v-next/hardhat/src/internal/builtin-plugins/gas-analytics/helpers.ts new file mode 100644 index 00000000000..4d8d4535e2e --- /dev/null +++ b/v-next/hardhat/src/internal/builtin-plugins/gas-analytics/helpers.ts @@ -0,0 +1,26 @@ +import { + testRunDone, + testRunStart, + testWorkerDone, +} from "./hook-handlers/test.js"; + +/** + * The following helpers are kept for backward compatibility with older versions + * of test runner plugins (hardhat-mocha, hardhat-node-test-runner) that import + * from "hardhat/internal/gas-analytics". + */ + +export async function markTestRunStart(id: string): Promise { + const { default: hre } = await import("../../../index.js"); + await testRunStart(hre, id); +} + +export async function markTestWorkerDone(id: string): Promise { + const { default: hre } = await import("../../../index.js"); + await testWorkerDone(hre, id); +} + +export async function markTestRunDone(id: string): Promise { + const { default: hre } = await import("../../../index.js"); + await testRunDone(hre, id); +} diff --git a/v-next/hardhat/src/internal/builtin-plugins/gas-analytics/hook-handlers/test.ts b/v-next/hardhat/src/internal/builtin-plugins/gas-analytics/hook-handlers/test.ts index 99700f37fd1..1fecf07c182 100644 --- a/v-next/hardhat/src/internal/builtin-plugins/gas-analytics/hook-handlers/test.ts +++ b/v-next/hardhat/src/internal/builtin-plugins/gas-analytics/hook-handlers/test.ts @@ -1,43 +1,68 @@ -import type { TestHooks } from "../../../../types/hooks.js"; +import type { HookContext, TestHooks } from "../../../../types/hooks.js"; import { assertHardhatInvariant } from "@nomicfoundation/hardhat-errors"; +import { isObject } from "@nomicfoundation/hardhat-utils/lang"; -import { HardhatRuntimeEnvironmentImplementation } from "../../../core/hre.js"; +import { GasAnalyticsManagerImplementation } from "../gas-analytics-manager.js"; export default async (): Promise> => ({ onTestRunStart: async (context, id, next) => { await next(context, id); - - if (context.globalOptions.gasStats === true) { - assertHardhatInvariant( - context instanceof HardhatRuntimeEnvironmentImplementation, - "Expected context to be an instance of HardhatRuntimeEnvironmentImplementation", - ); - await context._gasAnalytics.clearGasMeasurements(id); - } + await testRunStart(context, id); }, onTestWorkerDone: async (context, id, next) => { await next(context, id); - - if (context.globalOptions.gasStats === true) { - assertHardhatInvariant( - context instanceof HardhatRuntimeEnvironmentImplementation, - "Expected context to be an instance of HardhatRuntimeEnvironmentImplementation", - ); - await context._gasAnalytics.saveGasMeasurements(id); - } + await testWorkerDone(context, id); }, onTestRunDone: async (context, id, next) => { await next(context, id); - - if (context.globalOptions.gasStats === true) { - assertHardhatInvariant( - context instanceof HardhatRuntimeEnvironmentImplementation, - "Expected context to be an instance of HardhatRuntimeEnvironmentImplementation", - ); - await context._gasAnalytics.reportGasStats(id); - } + await testRunDone(context, id); }, }); + +export async function testRunStart( + context: HookContext, + id: string, +): Promise { + if (context.globalOptions.gasStats === true) { + assertHardhatInvariant( + "_gasAnalytics" in context && + isObject(context._gasAnalytics) && + context._gasAnalytics instanceof GasAnalyticsManagerImplementation, + "Expected HookContext#_gasAnalytics to be an instance of GasAnalyticsManagerImplementation", + ); + await context._gasAnalytics.clearGasMeasurements(id); + } +} + +export async function testWorkerDone( + context: HookContext, + id: string, +): Promise { + if (context.globalOptions.gasStats === true) { + assertHardhatInvariant( + "_gasAnalytics" in context && + isObject(context._gasAnalytics) && + context._gasAnalytics instanceof GasAnalyticsManagerImplementation, + "Expected HookContext#_gasAnalytics to be an instance of GasAnalyticsManagerImplementation", + ); + await context._gasAnalytics.saveGasMeasurements(id); + } +} + +export async function testRunDone( + context: HookContext, + id: string, +): Promise { + if (context.globalOptions.gasStats === true) { + assertHardhatInvariant( + "_gasAnalytics" in context && + isObject(context._gasAnalytics) && + context._gasAnalytics instanceof GasAnalyticsManagerImplementation, + "Expected HookContext#_gasAnalytics to be an instance of GasAnalyticsManagerImplementation", + ); + await context._gasAnalytics.reportGasStats(id); + } +}