TEST SELECTION
Run the tests that depend on the change, not the tests next to it
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.
// 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.// CI: every test that depends on a change, directly or not
const affected = reverseClosure(changedFiles, importGraph); // visited set: cycles terminate
const tests = [...affected].flatMap((f) => testsByFile[f] || []);
// a file the graph does not know (package.json, CI config) -> run everything
// and a full run on merge, so a hole in the graph is caught on main, not in production 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.
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
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.
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.
“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.
USED IN
Chapters that build on this lesson.
- CI at scale: speed, sharding and test selectionSoftware Testing and Quality
- The shape of a suite: pyramid, trophy and what fits your systemSoftware Testing and Quality