# Running only the tests a change reaches

Give test tasks the files of a change, so a pull request runs the tests that import what it touched, across packages, and nothing else.

[`--affected`](https://axlers.dev/docs/reference/affected.md) runs tests only in the packages a change affects. That still runs every test of `@acme/web` when one button in `@acme/ui` changed. With one line in `axle.toml`, Axle goes down to files: each test task gets the test files that import what changed, directly or through other files and packages, and runs only those.

## Setting it up

Add `{testFiles}` to the arguments of your test task in the root `axle.toml`:

```toml
[tasks.test]
arguments = ["{testFiles}"]
```

That's all a `test` script running `vitest run` or `jest` needs: both take test files as arguments and run only those. Then run the tests of a change:

```sh
pnpm exec axle run test --affected              # pnpm
npx axle run test --affected                    # npm
yarn axle run test --affected                   # Yarn
bunx axle run test --affected                   # Bun
nubx axle run test --affected                   # nub
aube exec axle run test --affected              # aube
```

```text
Comparing with main: 1 file changed in 1 package, which 1 other depends on.
Running test in 2 packages with pnpm, up to 16 tasks at once.

@acme/ui#test   > vitest run src/button.test.ts
@acme/web#test  > vitest run src/button.test.tsx
```

The change was to `packages/ui/src/button.ts`. `@acme/ui`'s test imports it, and `apps/web/src/button.test.tsx` imports `@acme/ui`, so each package runs the one test the change reaches. `@acme/docs` doesn't depend on `@acme/ui`, so its tests don't run at all.

Paths are relative to the package's folder, where its tests run. Without `--affected`, there's nothing to compare, and every test runs as before.

## How Axle follows imports

Axle reads the `import`, `export … from`, `import()` and `require` of every JavaScript and TypeScript file, the same way [`axle boundaries`](https://axlers.dev/docs/reference/boundaries.md) does, and keeps what it read in `.axle/imports.json`, so only files that changed are read again.

- A relative import such as `./button` is found as `button.ts`, `button.tsx`, `button/index.ts` and so on, and `./button.js` finds `button.ts` too. Imported files that aren't code, such as `./data.json` or `./theme.css`, count as well.
- An import of a package in the repository, such as `@acme/ui` or `@acme/ui/button`, reaches every file of that package, whether you use it from its source or its build output.
- A changed test file reaches itself.

## When every test runs

Running fewer tests is only safe when Axle can account for the whole change, so a task runs every test when:

| Case                                                              | Why                                                                                  |
| ----------------------------------------------------------------- | ------------------------------------------------------------------------------------ |
| nothing was compared, without `--affected`                        | there are no files to name                                                           |
| a changed file the task reads reaches none of its test files      | it may be a config or setup file, or imported through a path alias Axle can't follow |
| a file outside every package changed, or the history is too short | Axle can't tell what the change reaches                                              |
| the package's dependencies changed in the lockfile                | their code isn't in the repository                                                   |
| none of the files it reads changed, but a task it waits for runs  | the change reaches it through that task                                              |
| `{changedFiles}` would include a deleted file                     | a test runner can't be given a file that's gone                                      |
| the files would take more than 8,000 characters                   | they wouldn't fit on a Windows command line                                          |
| it's a task of the repository's root                              | its files are every package's files                                                  |

The second case is the one you'll meet most. A changed `vitest.config.ts`, a test setup file or a README reaches no test through imports, so every test of that package runs, which is the safe answer. A task that reads none of the changed files is skipped by `--affected` as before.

## Seeing what runs

`axle affected --files` lists the test files each affected package's `test` task would get, or why it runs every test:

```sh
pnpm exec axle affected --files                 # pnpm
npx axle affected --files                       # npm
yarn axle affected --files                      # Yarn
bunx axle affected --files                      # Bun
nubx axle affected --files                      # nub
aube exec axle affected --files                 # aube
```

```text
Comparing with main: 2 files changed in 1 package, which 1 other depends on.

@acme/ui#test  every test, since packages/ui/README.md changed, and none of its test files import it, directly or through other files

@acme/web#test  1 test file the change reaches
  apps/web/src/button.test.tsx
```

`--task` names another task, and `--json` prints it for other tools. To ask about one file:

```sh
pnpm exec axle why affected apps/web/src/button.test.tsx# pnpm
npx axle why affected apps/web/src/button.test.tsx# npm
yarn axle why affected apps/web/src/button.test.tsx# Yarn
bunx axle why affected apps/web/src/button.test.tsx# Bun
nubx axle why affected apps/web/src/button.test.tsx# nub
aube exec axle why affected apps/web/src/button.test.tsx# aube
```

```text
apps/web/src/button.test.tsx is affected because packages/ui/src/button.ts changed, and it imports it:

  apps/web/src/button.test.tsx → @acme/ui → packages/ui/src/button.ts
```

`axle run test --affected --dry` shows each task's command with its files, and a `Tests:` line saying how many it gets or why it runs every test.

## Which files are tests

By default, test files are the files named like `*.test.ts` or `*.spec.tsx`, in any JavaScript or TypeScript extension, and every such file in a `__tests__` folder, which is what Vitest and Jest look for unless told otherwise. If your tests live elsewhere, say so with `testFiles`, relative to each package:

```toml
[tasks.test]
arguments = ["{testFiles}"]
testFiles = ["tests/**/*.ts", "!tests/fixtures/**"]
```

A package's own `axle.toml` can set its own `testFiles`, as with any [task setting](https://axlers.dev/docs/reference/configuration.md#where-settings-go).

## Letting the test runner choose

Test runners can narrow a run themselves: `vitest related` and Jest's `--findRelatedTests` take source files and follow their own module graph to the tests. To use them, pass `{changedFiles}`, the changed files the task reads, and say with `fallbackArguments` what to run when Axle can't name them:

```toml
[tasks.test]
command = "vitest"
arguments = ["related", "--run", "{changedFiles}"]
fallbackArguments = ["run"]
```

```toml
[tasks.test]
command = "jest"
arguments = ["--findRelatedTests", "{changedFiles}"]
fallbackArguments = []
```

`fallbackArguments` matters here because `vitest related` with no files runs nothing and `--findRelatedTests` with none is an error. Axle still checks that every change reaches a test before narrowing, and the runner narrows further within that.

The runner only sees what it resolves. When a package uses another through its build output, such as `dist/index.js`, the runner can't trace a change in the other package's source to its tests, and runs none of them. `{testFiles}` doesn't have that gap, so prefer it for changes that cross packages.

## The cache, CI and watch

The files a task gets are part of its [hash](https://axlers.dev/docs/reference/cache.md), so a run given some tests is never restored for a run given others, and a run that falls back to every test shares its cache entry with a plain run.

In CI, fetch the whole history and run the tests of the change, as [axle affected](https://axlers.dev/docs/reference/affected.md#in-ci) shows. On the default branch, run every test without `--affected`. When even the tests of one change take too long for one job, `split = true` and [`--shard`](https://axlers.dev/docs/reference/run.md#splitting-a-run-across-ci-jobs) share them out between parallel jobs.

[`axle watch test`](https://axlers.dev/docs/reference/watch.md#test-files) runs every test once, then, after each change, the tests that change reaches.
