# Jest

Run and cache the tests of every package with a Jest config.

The Jest plugin gives every package that uses Jest a `test` task, so its tests run and are cached without a script or a line of `axle.toml`.

## Add it

```sh
axle add jest
```

This writes it into the root `axle.toml`:

```toml
[plugins.jest]
source = "npm:@axle-rs/plugin-jest@0.1.0"
hash = "sha256-…"
```

## Tasks

A package or project gets a `test` task, `jest`, when its folder has a `jest.config.ts`, `.mts`, `.cts`, `.js`, `.mjs`, `.cjs` or `.json`, or its `package.json` has a `jest` key. The command runs through the repository's package manager, such as `pnpm exec jest`, so it uses the Jest the package installs. Its `outputs` are `coverage/**`, so a coverage report comes back from the cache with the result.

The task reads every file in the package, as a task without `inputs` does, and waits for the `test` task of the packages it depends on. When tests import a workspace package's build, wait for it in `axle.toml`:

```toml
[tasks.test]
dependsOn = ["^build"]
```

## Only the tests a change reaches

The task's `arguments` are `["--runTestsByPath", "{testFiles}"]`, so with `axle run test --affected` or in `axle watch`, Jest runs only the test files the change reaches, following imports across packages; see [running only affected tests](https://axlers.dev/docs/crafting-your-repository/running-only-affected-tests.md). When Axle can't name them, `fallbackArguments` is empty and the task runs as plain `jest`, every test. Test files are found by Axle's default patterns, `*.test.*`, `*.spec.*` and `__tests__` folders; a package whose `testMatch` differs sets `testFiles` for its `test` task in its `axle.toml`.

## Scripts the repository already has

A package's own script wins. When its `package.json` has a `test` script that runs something else, such as `jest --runInBand`, the plugin leaves the task alone and Axle runs the script. A `test` script of plain `jest` is replaced by the plugin's task, which runs the same command and adds its settings.

## Settings

| Setting | Default  | Meaning                                          |
| ------- | -------- | ------------------------------------------------ |
| `test`  | `"test"` | the name of the task, or `false` to leave it out |

A package with both a Jest and a Vitest config would get a `test` task from each plugin, and only one command can run, so give one of them another name, such as `test = "jest"`.

The plugin's answers are kept until a `jest.config.*`, `package.json`, `pnpm-workspace.yaml` or lockfile changes.
