Skip to content

feat: replace test lifecycle helpers with hooks#8001

Merged
schaable merged 5 commits intomainfrom
add-test-hooks
Feb 26, 2026
Merged

feat: replace test lifecycle helpers with hooks#8001
schaable merged 5 commits intomainfrom
add-test-hooks

Conversation

@schaable
Copy link
Copy Markdown
Member

@schaable schaable commented Feb 24, 2026

This PR replaces the direct helper calls that test runners make into the coverage and gas-analytics plugins with test hooks. This decouples the runners from those plugins and is preparatory work for combined gas statistics and support --json output for --gas-stats.

This follows the direction explored in #7568.

Summary

  • Replace direct markTestRunStart/markTestWorkerDone/markTestRunDone helper calls with onTestRunStart/onTestWorkerDone/onTestRunDone test hooks
  • Consolidate per-runner worker files (coverage.ts + gas-stats.ts) into a single test-worker-done.ts to avoid double-firing hooks when both features are enabled
  • Remove hardhat/internal/coverage and hardhat/internal/gas-analytics export paths (no longer needed)

How it works

Before: Each test runner imported and called helpers from both plugins directly:

// v-next/hardhat/src/internal/builtin-plugins/solidity-test/task-action.ts
import {
  markTestRunStart as coverageMarkTestRunStart,
} from "../coverage/helpers.js";
import {
  markTestRunStart as gasAnalyticsMarkTestRunStart,
} from "../gas-analytics/helpers.js";

const runSolidityTests: NewTaskActionFunction<TestActionArguments> = async (
  { testFiles, chainType, grep, noCompile, verbosity },
  hre,
) => {
  await coverageMarkTestRunStart("solidity");
  await gasAnalyticsMarkTestRunStart("solidity");
  // code ...
}

After: Each test runner fires a single hook, and plugins respond via registered handlers:

// v-next/hardhat/src/internal/builtin-plugins/solidity-test/task-action.ts
const runSolidityTests: NewTaskActionFunction<TestActionArguments> = async (
  { testFiles, chainType, grep, noCompile, verbosity },
  hre,
) => {
  await hre.hooks.runSequentialHandlers("test", "onTestRunStart", ["solidity"]);
  // code ...
}

// v-next/hardhat/src/internal/builtin-plugins/gas-analytics/hook-handlers/test.ts
export default async (): Promise<Partial<TestHooks>> => ({
  onTestRunStart: async (context, id) => {
    // code ...
  },
}

// v-next/hardhat/src/internal/builtin-plugins/coverage/hook-handlers/test.ts
export default async (): Promise<Partial<TestHooks>> => ({
  onTestRunStart: async (context, id) => {
    // code ...
  },
}

Questions

  • Do we have documentation that covers the hooks? Should we add docs for the test hooks?

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Feb 24, 2026

🦋 Changeset detected

Latest commit: 515c622

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@nomicfoundation/hardhat-node-test-runner Patch
@nomicfoundation/hardhat-mocha Patch
hardhat Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@schaable schaable self-assigned this Feb 24, 2026
@schaable schaable added no changeset needed This PR doesn't require a changeset no docs needed This PR doesn't require links to documentation and removed no changeset needed This PR doesn't require a changeset labels Feb 24, 2026
@schaable schaable requested a review from alcuadrado February 24, 2026 19:27
* @param context The hook context.
* @param id A string identifier for the test runner (e.g., "solidity", "nodejs", "mocha").
*/
onTestRunStart: (context: HookContext, id: string) => Promise<void>;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change these to chained hooks, and always call next() first

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 9912d2a

Copy link
Copy Markdown
Member

@alcuadrado alcuadrado left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pre-approving

@schaable schaable added this pull request to the merge queue Feb 26, 2026
Merged via the queue into main with commit 52cce8a Feb 26, 2026
213 checks passed
@schaable schaable deleted the add-test-hooks branch February 26, 2026 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no docs needed This PR doesn't require links to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants