nodejs / nodejs/node

Unexpected behaviour of `beforeEach` outside of suite without test isolation

Open
#61,854 7 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

test_runner test_runner-agenda
Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

What is the problem this feature will solve?

In test runners such as jest or vitest, callbacks passed to the methods before, after, beforeEach or afterEach not wrapped in any describe, are called only when tests in their module are about to start or end. That's also the current behaviour when --test-isolation=process. Unfortunately, when the --test-isolation is none, the callbacks we pass to those 4 functions are added as global callbacks and they start to be called before/after each test in any file.

That's unexpected behaviour and it makes it more difficult to migrate from other test runners to node test runner or from the default --test-isolation=process.

What is the feature you are proposing to solve the problem?

Each module should be wrapped in a suite by default, so the callbacks don't leak to the other test modules. The only exception should be callbacks that we pass to those 4 methods in the global setup file(or its imports I guess).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how the Node.js test runner handles before, after, beforeEach, and afterEach callbacks under --test-isolation=process and --test-isolation=none. Check the existing behavior across separate test modules, including global setup and its imports. Done means module-level callbacks no longer run for tests in other modules while global setup callbacks retain their intended scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
testing-qa
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.