Skip to content

Affected Tests in CI

codegraph affected traces import dependencies transitively to find which test files are affected by a set of changed source files — so CI can run only the relevant tests.

Terminal window
codegraph affected src/utils.ts src/api.ts # pass files as arguments
git diff --name-only | codegraph affected --stdin # pipe from git diff
codegraph affected src/auth.ts --filter "e2e/*" # custom test-file pattern
OptionDescriptionDefault
--stdinRead the file list from stdinfalse
-d, --depth <n>Max dependency traversal depth5
-f, --filter <glob>Custom glob for the files that count as tests (see below)auto-detect
-j, --jsonOutput as JSONfalse
-q, --quietOutput file paths onlyfalse
-p, --path <path>Project pathcurrent project
--cross-tierAlso follow links across a process boundary: a client’s HTTP call to its server’s route, a queue job to its consumer, an event to its handler (see below)false

Without --filter, affected uses the same test detection as search and codegraph_explore. A file is a test when its name says so — test_foo.py, foo_test.go, foo.test.ts, foo.spec.ts, FooTest.java, or Google-style foo_unittest.cc and foo_unittest.py — or when it sits in a test folder such as tests/, test/, __tests__/, spec/, e2e/ or unittests/. A folder named just unittest/ isn’t one: Python’s own unittest package lives in one.

--filter <glob> replaces that detection, and the glob has to match a whole name, not part of one:

  • No / (*_test.go, test_*.py): the file name, in any folder.
  • With a / (e2e/*.spec.ts): the path from any folder down, so it also matches apps/web/e2e/login.spec.ts.
  • Starting with / (/tests/**/*.py): from the project root only.
  • Ending with / (tests/): everything under that folder.

So tests/**/*.py also takes tests/x.py, directly in tests/, and tests/*.py doesn’t take tests/x.py.bak.

CodeGraph links a client’s HTTP call to the route that serves it, a queue job to its consumer and an event to its handler, so codegraph_explore can follow a request from one side to the other. affected stops at those links: a test on the other side mocks the boundary, the way an Angular service’s spec stubs its HTTP client, so a change to the Spring controller the service calls doesn’t affect that spec. Pass --cross-tier to follow them too, when your tests make the real call, as end-to-end tests do.

#!/usr/bin/env bash
AFFECTED=$(git diff --name-only HEAD | codegraph affected --stdin --quiet)
if [ -n "$AFFECTED" ]; then
npx vitest run $AFFECTED
fi