Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions v-next/hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export {
markTestRunStart,
markTestRunDone,
markTestWorkerDone,
} from "./helpers.js";
27 changes: 27 additions & 0 deletions v-next/hardhat/src/internal/builtin-plugins/coverage/helpers.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
const { default: hre } = await import("../../../index.js");
await testRunStart(hre, id);
}

export async function markTestWorkerDone(id: string): Promise<void> {
const { default: hre } = await import("../../../index.js");
await testWorkerDone(hre, id);
}

export async function markTestRunDone(id: string): Promise<void> {
const { default: hre } = await import("../../../index.js");
await testRunDone(hre, id);
}
Original file line number Diff line number Diff line change
@@ -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<Partial<TestHooks>> => ({
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<void> {
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<void> {
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<void> {
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export {
markTestRunStart,
markTestRunDone,
markTestWorkerDone,
} from "./helpers.js";
Original file line number Diff line number Diff line change
@@ -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<void> {
const { default: hre } = await import("../../../index.js");
await testRunStart(hre, id);
}

export async function markTestWorkerDone(id: string): Promise<void> {
const { default: hre } = await import("../../../index.js");
await testWorkerDone(hre, id);
}

export async function markTestRunDone(id: string): Promise<void> {
const { default: hre } = await import("../../../index.js");
await testRunDone(hre, id);
Comment thread
schaable marked this conversation as resolved.
}
Original file line number Diff line number Diff line change
@@ -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<Partial<TestHooks>> => ({
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<void> {
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<void> {
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<void> {
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);
}
}