Skip to content

Determine Test Impact from a Git Diff with AI

CI/CD for QAUpdated 2026-10-21advanced
TL;DR

Reads a git diff and an import graph and returns the minimal test set to run for that diff: direct hits, transitive impact (1-2 levels), and explicit full-suite triggers (config / migration / lock file changes).

When to use it

  • Optimizing CI runtime by avoiding running all tests when only one file changed.
  • Implementing PR-time test selection for monorepos.
  • Onboarding to a CI system where flaky 'run everything' is the current default.
  • Debugging why tests run / don't run for specific PRs.

The prompt

XML-tagged: works best with Claude
test-impact-from-git-diff.claude.mdprompt
<role>You are a build-systems engineer. You know test impact analysis (TIA) is a tradeoff: false negatives (skipping a test that would catch a bug) are MUCH worse than false positives (running a test you didn't need). You err on the side of running more, not less.</role> <context>TIA categories:- **Direct hits**: tests that import the changed file directly- **Transitive impact**: tests that import a file that imports the changed file (1-2 levels deep is the practical limit; deeper transitives lose meaning)- **Full-suite triggers**: changes to: package.json / lockfile, build config (tsconfig, vite.config), CI config, db migrations, env files, anything in shared/ or lib/ False negatives in TIA cause production incidents. Default to RUN MORE when uncertain.</context> <task>For the diff and import graph below:1. Classify each changed file: source / test / config / migration / other.2. Identify direct test hits (tests importing the changed source file).3. Identify transitive impact (1-2 levels).4. Identify full-suite triggers and flag them.5. Emit a recommended test command (e.g., `vitest related <files> --run`, `jest --findRelatedTests <files>`, `playwright test --only-changed=origin/main`, or specific test files).6. Note any uncertainty: files where the import graph is incomplete or ambiguous.</task> <input>Git diff (paths and brief change context): {diff}Import graph (or sample of imports per file): {graph}Test framework + TIA command (vitest, jest, Playwright): {tooling}</input> <constraints>- Distinguish DIRECT vs TRANSITIVE in output.- Full-suite triggers are P0: surface immediately if any change matches.- Transitive impact limited to 1-2 levels: beyond that, false-positive cost dominates.- Emit CI command, not just a list of tests.- Acknowledge uncertainty (incomplete import graphs are common).</constraints> <output_format>Five sections:1. **File classification**: table: File | Type | Notes2. **Direct hits**: list of test files3. **Transitive impact**: list (1-2 levels) with depth indicator4. **Full-suite triggers**: list with reasoning (or "None: selective testing applies")5. **CI command**: exact command to run</output_format> <approach>Before writing, scan the diff for ANY full-suite trigger: if present, all other analysis is moot.</approach>

Example

Input
Git diff:
- modified: src/orders/CheckoutService.ts (added a new method calculateDiscount)
- modified: src/orders/CheckoutService.test.ts (added test for new method)
- modified: package.json (bumped lodash from 4.17.20 to 4.17.21)

Import graph (sampled):
- tests/orders/checkout.spec.ts imports src/orders/CheckoutService
- tests/orders/discount.spec.ts imports src/orders/CheckoutService AND src/orders/DiscountCalculator
- src/orders/OrderProcessor imports src/orders/CheckoutService
- tests/orders/order-processor.spec.ts imports src/orders/OrderProcessor
- tests/integration/full-checkout.spec.ts imports src/orders/CheckoutService (indirectly via index re-export)

Test framework + TIA command: Vitest. Vitest supports `--changed` (since commit) and `vitest related <files>`. Playwright for E2E.
Output
## 1. File classification

| File | Type | Notes |
|------|------|-------|
| src/orders/CheckoutService.ts | source | Modified: added `calculateDiscount` method |
| src/orders/CheckoutService.test.ts | test | Modified: added test for new method (sanity coverage) |
| package.json | full-suite trigger | Dependency upgrade: affects all code |

## 2. Direct hits

- `tests/orders/checkout.spec.ts`: imports CheckoutService directly
- `tests/orders/discount.spec.ts`: imports CheckoutService directly (and DiscountCalculator, but only CheckoutService changed)
- `src/orders/CheckoutService.test.ts`: co-located test file, modified in diff
- `tests/integration/full-checkout.spec.ts`: imports via re-export

## 3. Transitive impact (1-2 levels)

- `tests/orders/order-processor.spec.ts`: depth 1: imports OrderProcessor → which imports CheckoutService.

## 4. Full-suite triggers

- **package.json**: lodash upgrade. Run full suite. Even patch version bumps can break behavior; ESPECIALLY when downstream code depends on edge cases.

## 5. CI command

Because a full-suite trigger (package.json) is present, run all tests:

```bash
# Unit + Integration:
vitest run --coverage

# E2E:
npx playwright test
```

If you want to optimize for a hypothetical "selective testing" version of this diff WITHOUT the package.json change:

```bash
# Selective (no full-suite trigger present):
vitest related src/orders/CheckoutService.ts --run
npx playwright test --only-changed=origin/main
```

Notes:
- `vitest related` recursively follows the import graph: typically more thorough than naive grep.
- Playwright 1.46+ has `--only-changed=<ref>`: it runs spec files that changed or import a changed file. It cannot see E2E specs that hit the code only over HTTP, so keep a tagged smoke set (`--grep @checkout`) as the safety net.
- Transitive level capped at 2: going deeper produces false positives; if a 3-level transitive matters, the import graph is too coupled and that's a separate architecture issue.
- Sample import graph may be incomplete (real graph may include more transitive consumers); validate with a static analysis pass before relying on TIA in CI.

Common pitfalls

  • Model treats package.json bump as 'minor: no impact': every dependency change can break code. Always full-suite.
  • Transitive analysis goes 4+ levels deep: meaningless beyond 2; ranks every test as 'affected'.
  • Selective testing skips integration tests that don't import the changed file but DO test its consumers: false negative.
  • Model produces a list of tests without a CI command; engineers can't run it as-is.

Tips

  • Default to running MORE tests when uncertain. False negatives cost production incidents.
  • Use Vitest `--changed` for git-based TIA; `vitest related <files>` for explicit file-based.
  • For Playwright (no native TIA), tag specs with feature names (`@checkout`, `@billing`) and grep.
  • Pair with `parallel-test-execution-config`: TIA reduces what to run; parallelization reduces how long.

FAQ

How do I integrate with Vitest related-test selection?

`vitest related <files> --run` computes the import graph and runs only tests transitively related. Provide the changed source file as input. Best for non-monorepo Vite/Vitest projects. Less mature than Bazel's TIA but free.

TIA at scale (10k+ tests)?

Use Bazel for the most rigorous TIA: it computes exact import graphs and caches test results. For non-Bazel projects, accept some false positives; the saved CI minutes outweigh extra runs.

Dynamic vs static import graph?

Static analysis (read source code) catches most cases. Dynamic analysis (runtime instrumentation) catches deps loaded at runtime (dynamic imports, plugins). Most projects use static; dynamic is rare in JS / TS.

What about flaky tests breaking TIA confidence?

Flaky tests sabotage TIA: engineers stop trusting selective testing. Fix flaky tests FIRST (see refactor-flaky-test); then TIA becomes reliable.