Skip to content

test_runner: scope file-level hooks per file in no-isolation mode#62615

Draft
marcopiraccini wants to merge 1 commit intonodejs:mainfrom
marcopiraccini:test_runner-scope-hooks-per-file-no-isolation
Draft

test_runner: scope file-level hooks per file in no-isolation mode#62615
marcopiraccini wants to merge 1 commit intonodejs:mainfrom
marcopiraccini:test_runner-scope-hooks-per-file-no-isolation

Conversation

@marcopiraccini
Copy link
Copy Markdown
Contributor

@marcopiraccini marcopiraccini commented Apr 6, 2026

When using --test-isolation=none, beforeEach/afterEach/before/after hooks declared at the top level of a test file were added to the root test and leaked into tests from other files. This is inconsistent with --test-isolation=process (the default), where each file runs in its own process and hooks are naturally scoped.

This PR wraps every test file in an implicit suite when using isolation: 'none', so that top-level hooks are scoped to their file. Wrapping is unconditional because hooks are registered at load time and we cannot know ahead of loading whether a file declares any.
Global hooks registered via --import or --require continue to apply to all tests.

NOTE: Updated existing no-isolation tests: the previous assertions expected hooks to leak across files (e.g. beforeEach from one.test.js running for tests in two.test.js). Updated to match the new scoped behavior, including the additional implicit file suites in pass/suite counts.

Fixes: #61854

Signed-off-by: marcopiraccini <marco.piraccini@gmail.com>
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/test_runner

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem. labels Apr 6, 2026
@marcopiraccini marcopiraccini marked this pull request as ready for review April 6, 2026 14:00
@marcopiraccini marcopiraccini marked this pull request as draft April 6, 2026 14:10
@marcopiraccini
Copy link
Copy Markdown
Contributor Author

marcopiraccini commented Apr 6, 2026

Not sure this is the correct approach, though. This gives a nesting level to every test automatically, channging the ouput. Mark as draft, will amend.

@pmarchini
Copy link
Copy Markdown
Member

Hey @marcopiraccini, thanks for your contribution!

The current behavior is actually expected. That said, it seems we have different perspectives on it, and it would be worth discussing further before considering such a significant breaking change.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.74%. Comparing base (4f08c64) to head (a0750d0).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #62615      +/-   ##
==========================================
- Coverage   89.75%   89.74%   -0.01%     
==========================================
  Files         696      696              
  Lines      214812   214807       -5     
  Branches    41108    41104       -4     
==========================================
- Hits       192801   192787      -14     
- Misses      14120    14125       +5     
- Partials     7891     7895       +4     
Files with missing lines Coverage Δ
lib/internal/test_runner/runner.js 93.61% <100.00%> (-0.04%) ⬇️

... and 28 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

isolation mode is `'process'`. This flag is ignored if the `--test` flag is not
present. See the [test runner execution model][] section for more information.
`'none'`, all test files run in the same process as the test runner. Each test
file is wrapped in an implicit suite, so that lifecycle hooks such as
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.

this is an implementation detail, I dont think it needs documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected behaviour of beforeEach outside of suite without test isolation

4 participants