TEST SELECTION

Run the tests that depend on the change, not the tests next to it

L215 MINTESTING
PREDICT

A pull request changes only util.js, and CI runs only the tests for changed files. util.js has tests and they pass. billing.js imports util.js, and its tests would now fail. What does CI report?

Selecting by changed file, or by matching test names to file names, finds the tests next to the change. A change breaks code that uses it, and that code’s tests are elsewhere. To find them, build the import graph, then walk it backwards: from each changed file to the files that import it, to the files that import those. Run the tests of every file you reach.

Two details decide whether the walk is safe. Dependencies can form cycles, so visit each file once. And some changed files (package.json, CI config) are not in the graph at all, so the safe answer is the full suite. Keep a full run on merge as the safety net, in the same spirit as the flaky-test lesson, where a green run also needed a second look.

▶ SIMULATION
PULL REQUEST
changes: nothing yet
selection: —
CI
tests run: — of 14
build: —
THE CODE
tests that would fail: none
skipped dependents: 0
util.jsutil.test ; util-format.test
db.jsdb.test
billing.jsbilling.test
orders.jsorders.test
api.jsapi.test
report.jsreport.test
ring-a.jsring-a.test
ring-b.jsring-b.test
chain-a.jschain-a.test
chain-b.jschain-b.test
chain-c.jschain-c.test
chain-d.jschain-d.test
chain-e.jschain-e.test
✓ the build is red exactly when the change breaks a test✓ every test that depends on a changed file was selected
SCHEDULE
01[DEV]DEV: opens a PR that changes util.js
02[CI]CI: selects the tests of the changed files
03[CI]CI: runs the selected tests and reports the build
Start of own tests only scenario. 3 steps.
// CI: only the tests next to the files that changed
const tests = changedFiles.flatMap((f) => testsByFile[f] || []);
// util.js changed -> util.test runs. billing.test never does.

Watch the two invariants. honest fails when the build colour is wrong; covered fails as soon as a dependent test is skipped. In LEAF CHANGE selecting the changed file’s own tests is enough, which is why the shortcut survives so long. In CAUGHT AFTER MERGE the full run does find the problem, but after the change is in.

BREAK IT NOT YET

CI selects the tests of the changed files only. The repository has a shared util, a leaf report and a package.json. Inject one change that makes CI report a green build while tests that would fail never run.

GOAL: the build is green and a test that would fail was not run

FIX IT NOT YET

Write selectTests(changedFiles, importGraph, testsByFile). importGraph[file] lists the files that file imports; testsByFile[file] lists the tests for that file. Return every test (each once, any order) for the changed files and for every file that depends on them, directly or through other files. The graph can contain a cycle. A changed file that is not in the graph (package.json, CI config) could affect anything: return every test.

Mental model:

A test selector makes a claim: the tests I skipped cannot fail. That claim is only true if you follow every path a change can travel, so select by the reverse dependency graph, treat anything the graph does not know as “everything”, and keep a full run on merge to catch what the model missed.

IN PRODUCTION

Monorepos and big CI fleets. Build tools such as Bazel can compute the reverse dependency closure for you, and many teams add a learned or heuristic selector on top to save more. Holes appear in dynamic imports, generated code, config files, database migrations and anything wired by string name instead of import.

IN INTERVIEWS

“How would you speed up CI without losing coverage?” Say you would select tests by the transitive reverse dependencies of the changed files, fall back to the full suite for files the graph does not cover, run everything on merge or nightly, and track how often the full run finds a failure that selection skipped. That last number tells you whether the selector can be trusted.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: DETERMINISTIC SIMULATION →