nodejs / nodejs/node

test_runner: change-aware test selection

Open
#66,006 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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?

node --test always runs every test file. There is no way to run only the tests affected by a change.

--watch does this, but only while it stays running. CI, a pre-commit hook, or a branch diff all start cold and run everything.

Jest (--onlyChanged) and Vitest (--changed) both do this.

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

Run only the test files whose module graph reaches a given set of files.

$ node --test --related=src/util.ts               # tests that depend on a file
$ git diff --name-only | node --test --related=-  # tests affected by a change

Graph-aware, not path-based: a test that imports a module that imports the changed file is selected. Conservative by default, so anything the graph cannot see has to run, and a change to package.json or a lockfile disables filtering entirely.

Paths rather than a --changed=<rev> flag, because that would mean core shelling out to git. There is no VCS dependency anywhere in lib/ today and I don't think this justifies introducing one. --changed can be layered on later if the team wants it.

Things to figure out
  • CJS. Static imports can be extracted from an ES module without executing it. There is no equivalent for require(), since the bundled lexer reports exports rather than requires. CJS files would be opaque: any test reaching one always runs. Correct, but a CJS-heavy project gets little out of this.
  • Invisible dependencies. A test that reads a fixture with fs, or uses dynamic import(), cannot be selected statically. Those have to resolve to "run it".

Under-selection is the failure mode that matters. Silently skipping a test the change broke is worse than not having the feature.

What alternatives have you considered?

Userland: a wrapper computing the list and passing it to run({ files }). Every such tool has to reimplement module resolution, and gets TypeScript, subpath imports and node_modules boundaries subtly wrong. The resolver is already in core.

--test-rerun-failures covers rerunning what failed, not running what a change could break.

cc @nodejs/test_runner

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 with the node --test entry point, the existing --watch implementation, and run({ files }); then trace the resolver used by the test runner. Done means selecting tests through static module dependencies while conservatively running tests with opaque dependencies or global package changes, with coverage for the stated CJS and invisible-dependency cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
cli, testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.