# `vitest_test`

JavaScript and TypeScript tests run with Vitest.

## Start Here

Install Vitest in the package with the project's JavaScript package manager,
or make the `vitest` command available on the executable search path. For
example:

```sh
npm install --save-dev vitest
```

Retrieve the runnable starter when you want a complete declaration, package
manifest, and sample tests:

```sh
struo query example vitest_test vitest-test-minimal --format json
```

Copy the declaration below into `struo.toml`, adjust `srcs` and any setup files
for the project, then establish discovery and run the file batches:

```sh
struo test tests --format json
struo query test-manifest tests --format json
struo test tests --jobs 4 --format json
```

The first command runs the complete target. Later runs can use the resulting
manifest for automatic batching and exact selection. See
[Testing and Scheduling](/guide/graph/testing) for affected plans, case-level
batching, and exact unit commands.

## Description

`vitest_test` prefers the package-local Vitest entry point, then falls back to
the installed `vitest` command. It uses Vitest's structured reporter, records
stable file and full-name identifiers, and writes normalized Struo results.
Exact execution selects the discovered file and an anchored test name pattern.

The target kind declares Node.js and Vitest as tool requirements. A
workspace-relative `runner` is treated as an input, while an installed runner
is identified as part of the toolchain.

Automatic batching uses one batch per test file by default. Set `batching` to
`case` for individual cases, or `target` for one target batch. A complete run
must establish a current manifest before smaller batches are planned.
Struo disables Vitest's mutable local cache inside the action because the
installed packages are declared inputs. Struo's action cache reuses successful
test results when those inputs have not changed.

When an unfiltered run contains duplicate file/full-name identities, Struo
automatically reports one target-owned suite unit instead of failing the run
or inventing case identities. Every assertion remains in the native JSON
report, and assertion totals are available in `runner.metadata.native_summary`.
Any native failure still fails the suite. The discovered manifest disables
case listing, exact selection, and sharding, so subsequent runs remain whole
targets and successful results stay cacheable. An explicitly filtered run
still rejects duplicate identities. Set `unit = "suite"` to always use this
whole-target reporting mode, even when names are unique.

## Attributes

| Attribute | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `node` | string | no | `node` | Node.js executable name, absolute path, or workspace-relative path |
| `runner` | string | no | automatic | Package-relative Vitest entry point or installed command. Struo prefers `node_modules/vitest/vitest.mjs`, then searches for `vitest` |
| `config` | list&lt;string&gt; | no | package and lock files | Dependency and runner configuration inputs |
| `dependencies` | list&lt;string&gt; | no | `node_modules/**/*` | Installed runner and package files required during execution |
| `data` | list&lt;string&gt; | no | `[]` | Setup, transform, snapshot, and runtime inputs |
| `args` | list&lt;string&gt; | no | `[]` | Additional Vitest arguments |
| `coverage_per_test` | bool | no | `false` | Measure each exact case with native V8 coverage |
| `coverage_sources` | list&lt;string&gt; | no | `[]` | Product source globs to instrument, required for coverage selection |
| `env` | map&lt;string, string&gt; | no | `{}` | Explicit test environment variables |
| `env_inherit` | list&lt;string&gt; | no | `[]` | Host environment variables inherited by name |
| `unit` | string | no | `case` | `case` for unique case identities with automatic whole-target fallback on duplicates; `suite` to always disable case selection and sharding |
| `batching` | string | no | `file` | Automatic granularity: `file`, `case`, or `target` |
| `labels` | list&lt;string&gt; | no | `[]` | Labels exposed through test discovery |
| `timeout_ms` | int | no | empty | Optional timeout in milliseconds |

## Dependency Edges

`deps` names targets whose changes should select this test target.

## Providers and Capabilities

The target emits `struo_test_info` and exposes `test` with the `default`,
`test_results`, and `logs` output groups.

`test-discovery` collects current case identities without running test bodies.
With `coverage_per_test = true`, Struo runs each exact case in isolation with
the native thread pool limited to one worker. Install `@vitest/coverage-v8`
at the same version as Vitest and declare product inputs in `coverage_sources`.
Struo preserves unexecuted sources as zero-hit inputs. User-created worker
threads, child processes, missing instrumentation, duplicate identities,
skipped cases and failures cannot establish complete checkpoints. Suite units
do not support this collector. Coverage observations bypass the action cache.

## Example

```toml
[[target]]
name = "tests"
kind = "vitest_test"
srcs = ["tests/**/*.test.js"]
```

Include setup files, transforms, snapshots, and other runtime files in `data`.
