# Vitest

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

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

## Add it

```sh
axle add vitest
```

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

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

## Tasks

A package or project gets a `test` task, `vitest run`, when its folder has a `vitest.config.*`, or a `vite.config.*` with a `test` section. The command runs through the repository's package manager, such as `pnpm exec vitest run`, so it uses the Vitest 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 `["{testFiles}"]`, so with `axle run test --affected` or in `axle watch`, Vitest gets 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, the task runs as plain `vitest run`, every test. Test files are found by Axle's default patterns, `*.test.*`, `*.spec.*` and `__tests__` folders; a package whose Vitest `include` 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 `vitest run --coverage`, the plugin leaves the task alone and Axle runs the script. A `test` script of `vitest` or `vitest run` is replaced by the plugin's task, which runs the same tests once and adds its settings.

## One process for each package

Each package's tests run in their own Vitest process, in the package's folder. The plugin doesn't use [batch mode](https://axlers.dev/docs/reference/plugins.md#batch-mode) to run many packages in one Vitest process with its `projects`, since that isn't the same as running each package on its own: every package would share one Vitest version, one working folder and one environment, the output of each package would have to be told apart from the others', and a config that reads `process.cwd()` or sets up globals would behave differently. Axle already runs the packages' tests side by side, and caches each.

## Settings

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

A package with both a Vitest and a Jest 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 = "vitest"`.

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