6.4 KiB
title, outline
| title | outline |
|---|---|
| Command Line Interface | Guide | deep |
Command Line Interface
Commands
vitest
Start Vitest in the current directory. Will enter the watch mode in development environment and run mode in CI (or non-interactive terminal) automatically.
You can pass an additional argument as the filter of the test files to run. For example:
vitest foobar
Will run only the test file that contains foobar in their paths. This filter only checks inclusion and doesn't support regexp or glob patterns (unless your terminal processes it before Vitest receives the filter).
Since Vitest 3, you can also specify the test by filename and line number:
$ vitest basic/foo.test.ts:10
::: warning Note that Vitest requires the full filename for this feature to work. It can be relative to the current working directory or an absolute file path.
$ vitest basic/foo.js:10 # ✅
$ vitest ./basic/foo.js:10 # ✅
$ vitest /users/project/basic/foo.js:10 # ✅
$ vitest foo:10 # ❌
$ vitest ./basic/foo:10 # ❌
At the moment Vitest also doesn't support ranges:
$ vitest basic/foo.test.ts:10, basic/foo.test.ts:25 # ✅
$ vitest basic/foo.test.ts:10-25 # ❌
:::
vitest run
Perform a single run without watch mode.
vitest watch
Run all test suites but watch for changes and rerun tests when they change. Same as calling vitest without an argument. Will fallback to vitest run in CI or when stdin is not a TTY (non-interactive environment).
vitest dev
Alias to vitest watch.
vitest related
Run only tests that cover a list of source files. Works with static imports (e.g., import('./index.js') or import index from './index.js), but not the dynamic ones (e.g., import(filepath)). All files should be relative to root folder.
Useful to run with lint-staged or with your CI setup.
vitest related /src/index.ts /src/hello-world.js
::: tip
Don't forget that Vitest runs with enabled watch mode by default. If you are using tools like lint-staged, you should also pass --run option, so that command can exit normally.
export default {
'*.{js,ts}': 'vitest related --run',
}
:::
vitest bench
Run only benchmark tests, which compare performance results.
vitest init
vitest init <name> can be used to setup project configuration. At the moment, it only supports browser value:
vitest init browser
vitest list
vitest list command inherits all vitest options to print the list of all matching tests. This command ignores reporters option. By default, it will print the names of all tests that matched the file filter and name pattern:
vitest list filename.spec.ts -t="some-test"
describe > some-test
describe > some-test > test 1
describe > some-test > test 2
You can pass down --json flag to print tests in JSON format or save it in a separate file:
vitest list filename.spec.ts -t="some-test" --json=./file.json
If --json flag doesn't receive a value, it will output the JSON into stdout.
You also can pass down --filesOnly flag to print the test files only:
vitest list --filesOnly
tests/test1.test.ts
tests/test2.test.ts
Since Vitest 4.1, you may pass --static-parse to parse test files instead of running them to collect tests. Vitest parses test files with limited concurrency, defaulting to os.availableParallelism(). You can change it via the --static-parse-concurrency option.
Shell Autocompletions
Vitest provides shell autocompletions for commands, options, and option values powered by @bomb.sh/tab.
Setup
For permanent setup in zsh, add this to your ~/.zshrc:
# Add to ~/.zshrc for permanent autocompletions (same can be done for other shells)
source <(vitest complete zsh)
Package Manager Integration
@bomb.sh/tab integrates with package managers. Autocompletions work when running vitest directly:
::: code-group
npm vitest <Tab>
npm exec vitest <Tab>
pnpm vitest <Tab>
yarn vitest <Tab>
bun vitest <Tab>
:::
For package manager autocompletions, you should install tab's package manager completions separately.
Options
::: tip
Vitest supports both camel case and kebab case for CLI arguments. For example, --passWithNoTests and --pass-with-no-tests will both work (--no-color and --inspect-brk are the exceptions).
Vitest also supports different ways of specifying the value: --reporter dot and --reporter=dot are both valid.
If option supports an array of values, you need to pass the option multiple times:
vitest --reporter=dot --reporter=default
Boolean options can be negated with no- prefix. Specifying the value as false also works:
vitest --no-api
vitest --api=false
:::
shard
- Type:
string - Default: disabled
Test suite shard to execute in a format of <index>/<count>, where
countis a positive integer, count of divided partsindexis a positive integer, index of divided part
This command will divide all tests into count equal parts, and will run only those that happen to be in an index part. For example, to split your tests suite into three parts, use this:
vitest run --shard=1/3
vitest run --shard=2/3
vitest run --shard=3/3
:::warning
You cannot use this option with --watch enabled (enabled in dev by default).
:::
::: tip
If --reporter=blob is used without an output file, the default path will include the current shard config and blob label from VITEST_BLOB_LABEL or the blob reporter label option to avoid collisions with other Vitest processes.
:::
merge-reports
- Type:
boolean | string
Merges every blob report located in the specified folder (.vitest/blob/ by default). You can use any reporters with this command (except blob):
vitest --merge-reports --reporter=junit