Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 6 min read

Code Graph Test Selection: 99.6% of Faults Caught, 3.4% of Suite Time

A code-intelligence graph can tell you which tests to run for a change, but only its file-level answer is good enough. On hono, code-review-graph impact picked tests that caught 236 of 237 seeded faults (99.6%) and ran a

A code-intelligence graph can tell you which tests to run for a change, but only its file-level answer is good enough. On hono, code-review-graph impact picked tests that caught 236 of 237 seeded faults (99.6%) and ran a mean 3.4% of the suite's test time; vitest related caught all 237 at 16.1%, and the graph's function-level query caught only 146 (61.6%).

Every script and raw result is in the PoC repo.

The setup

code-review-graph builds a local map of a codebase (files, functions, calls, imports, tests) so that AI tools can ask what a change touches. Test selection asks the same question, so I tested it as a test selector against the import graph Vitest already has.

  • Target: honojs/hono at a8ae5b1d. The suite is the Vitest project main, test files under src/: 123 files and 5,110 tests, about 5.2 s wall-clock.
  • Tools: code-review-graph 2.4.0, with the graph built once at that commit. Vitest 5.0.1, Node 24.21.0, macOS 15.7.4 on arm64.
  • Faults: a script found 1,373 operator sites in 155 source files and drew 300 with a fixed seed, at most 6 per file. Each fault swaps one operator: ===/!==, &&/||, </<=, >/>=, + 1/- 1, true/false.
  • Ground truth: apply the fault, run the whole suite, and record the test files that fail. Then run those files again on their own with the fault applied and keep only the files that fail again. No file dropped out in this run.

Each selector gets the changed file and returns test files:

Selector What it picks
impact, depth 1, 2 or 3 test files in code-review-graph impact --files <file> --depth N. Depth 2 is the default
tests_for test files from code-review-graph query tests_for for each function detect-changes reports as changed
vitest related the test files vitest related <file> runs
same name <file>.test.ts or <file>.test.tsx, if it exists
full all 123 test files

A selector "caught" a fault when it picked at least one failing test file, so CI goes red. It "picked all" when it picked every failing test file. Share of suite time is the sum of the picked files' durations divided by the sum for all files, from the median of 3 clean runs.

These are the two commands, run from the hono checkout:

uvx --python 3.12 [email protected] impact --files src/middleware/jwk/jwk.ts
node node_modules/vitest/vitest.mjs related src/middleware/jwk/jwk.ts --run --project main

impact prints JSON. Its impacted_files list holds source and test files, so the harness keeps only the files that are in the suite.

The results

Of the 300 faults, 237 made at least one test file fail, 61 made none fail, 1 timed out and 1 did not parse. The last two are excluded. The 237 are in 73 source files, and the median fault broke 1 test file.

Selector Caught Picked all failing files Median test files Mean share of suite time
impact, depth 1 219 (92.4%) 162 (68.4%) 1 1.3%
impact, depth 2 (default) 236 (99.6%) 198 (83.5%) 2 3.4%
impact, depth 3 236 (99.6%) 203 (85.7%) 2 5.0%
tests_for 146 (61.6%) 104 (43.9%) 1 1.2%
vitest related 237 (100%) 237 (100%) 3 16.1%
same name 190 (80.2%) 117 (49.4%) 1 0.6%
full 237 (100%) 237 (100%) 123 100%

The default impact call is the interesting row. It turned CI red for all but one fault and ran about a fifth of the test time vitest related ran. The cost is completeness: for 38 of the 236 faults it caught, at least one other failing test file was not picked. CI goes red, but the developer sees only part of the damage until the full suite runs.

Depth 3 picked all failing files for 5 more faults than depth 2 and ran 1.5 points more suite time (4.97% against 3.44%). It missed the same one fault.

The fault the graph missed

Fault #218 is in src/adapter/cloudflare-workers/websocket.ts. The test imports upgradeWebSocket through the folder's index.ts, and the changed code is an arrow function passed to defineWebSocketHelper. For that file impact returned 0 impacted files at every depth. vitest related follows the module imports, so it picked the test.

One more fault counts as caught for a reason worth knowing. Fault #295 (src/middleware/jwk/jwk.ts, || to &&) failed no test. jwk() now throws its "requires options" error while Vitest collects the tests, so src/middleware/jwk/index.test.ts fails with a file-level error and no failed test. CI goes red, so it counts.

Function-level selection did not hold up

The graph also answers a finer question: which tests cover this function. That would be the bigger saving, so I tried it. For each fault, detect-changes named the changed functions and query tests_for returned the tests for each.

It caught 146 of 237 faults (61.6%). For 58 faults it returned no tests at all, and for 4 of those detect-changes reported no changed function. An empty selection is the dangerous case: a selector that returns nothing lets CI pass.

Real commits

Seeded faults are small, so I also replayed the last 100 hono commits that changed source under src/. 88 of them also changed test files. There is no fault to catch here, so the check is whether each selector picks the test files the author changed.

Selector Median test files Mean share of suite time Empty selections Picked all the author's test files
impact, depth 1 2 3.8% 3 73 of 88
impact, depth 2 4 11.1% 0 83 of 88
impact, depth 3 4 11.8% 0 83 of 88
vitest related 6 19.1% 1 84 of 88
same name 1 0.8% 13 60 of 88

On real commits the gap narrows. Default impact picked the author's tests in 83 of 88 commits against 84 for vitest related, at 11.1% of suite time against 19.1%.

What didn't work

  • The node limit. impact has a default limit of 500 impacted nodes (--max-results). Among the 73 changed files of caught faults, the impacted node count went over it for src/context.ts at every depth, and at depth 3 also for src/request.ts, src/utils/mime.ts and src/utils/url.ts. For src/context.ts, depths 1, 2 and 3 all picked the same 13 test files. I did not measure whether the limit cost any test files.
  • Re-exports and callbacks. The one miss had both: the test reached the code through an index.ts re-export, and the changed code was an arrow function passed to a helper. One case does not show which of the two broke the link.
  • tests_for is not a selector on its own. Too many changed functions had no linked test.

The limits

  • One repository, one machine, seeded faults. Operator swaps are smaller than most real changes.
  • The graph was built once at the measured commit. The commit replay uses that graph and today's import graph, not the state at each old commit.
  • "The author's test files" is a proxy for the tests that matter. One commit (859e2c2) only moved import sorting to a new tool. It changed 4 source files and 45 test files.
  • hono's suite runs in about 5 s, so the time saved here is seconds. The share of suite time is the number that carries over to a larger suite.

What I'd do with it

  • If your runner already has an import graph, start there. Short of the full suite, vitest related was the only selector that picked every failing file, and it needs no extra tool.
  • Use code-review-graph impact at its default depth when the import graph runs too much, and run the full suite on merge. That is where its 99.6% at 3.4% of suite time is worth the 38 partial picks.
  • Treat an empty selection, or an impacted node count over the limit, as "run everything". Do not use the function-level query to select tests.

For a Playwright suite, playwright-affected selects tests from the diff with a map you write, and runs the full suite whenever it cannot decide. The mutation testing experiment uses seeded faults in the same way. More measured experiments are in the playbook.

Repo: qa-gary-parker/poc-test-impact-graph.

Originally published at https://garyparker.dev/blog/code-graph-test-impact-analysis

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.