Skip to content

Latest commit

 

History

History
276 lines (182 loc) · 9.98 KB

File metadata and controls

276 lines (182 loc) · 9.98 KB
title Command Line Interface | Guide
outline 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

Use --related to list only the tests that import the specified source files:

vitest list --related --filesOnly src/index.ts src/utils.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.

vitest doctor

vitest doctor measures how much faster the test suite would run under alternative configurations by running it under each of them. The candidates are picked based on the current config:

vitest doctor
Results (min of 3 runs each)

  baseline (pool: forks · isolate: true)  4.08s
  pool: 'threads'                         3.64s (-11%)
  pool: 'vmThreads'                       1.33s (-67%)
  isolate: false                          1.28s (-69%)

Recommendation: pool: 'vmThreads' (-67%)

  // vitest.config.ts
  import { defineConfig } from 'vitest/config'

  export default defineConfig({
    test: {
      pool: 'vmThreads', // measured -67% on this suite
    },
  })

The isolate: false candidate is additionally validated by running the suite twice with a shuffled file order: if any test depends on isolation, the candidate is reported as failed instead of recommended. When several candidates are close to the fastest, doctor prefers the one that keeps per-file isolation.

Doctor also probes lower maxWorkers values on top of the winning configuration: every worker funnels its transform requests through the single main-thread Vite server, so past a certain count more workers make the run slower, not faster. Starting from half the current worker count, doctor keeps halving while the suite gets at least 5% faster, and includes the winning value in the recommendation.

Suites running a DOM environment are measured under both vm pools, vmThreads and vmForks: they amortize the environment creation cost by keeping one environment per worker while every file still gets a fresh VM context. vmForks uses child processes instead of worker threads: each child gets its own heap and garbage collector, so either pool can come out faster depending on the suite, and vmForks is the vm option for suites that cannot run in worker threads.

Projects running jsdom are also measured under environment: 'happy-dom' when the package is installed. The swap is applied per project; projects on other environments keep them. happy-dom implements the DOM differently than jsdom, so tests that depend on layout or navigation should be verified before adopting the swap. When the fs module cache is off, doctor measures fsModuleCache: true after an untimed priming run that populates the cache, so the reported time is what repeated runs pay.

Every measurement runs the full suite, including browser projects: isolate: false also affects browser mode. Candidates that cannot affect browser projects (pool, environment, the fs module cache) are picked based on the node-side projects only.

Failing candidates are reported with an excerpt of their errors. If the suite fails under the current configuration, doctor aborts and shows the errors: it needs a passing baseline to compare against.

Short suites are measured multiple times and the best time is reported, so the comparison reflects a warm steady state. Doctor runs the full suite several times, so it takes a multiple of a normal run's time. See Improving Performance for the trade-offs behind every candidate.

Doctor measures and reports the baseline even when there are no candidates to compare. Configurations on a vm pool are additionally compared against pool: 'threads' with isolate: false, which also reuses workers but shares module state between files; a configuration already on one vm pool is still measured under the other.

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

  • count is a positive integer, count of divided parts
  • index is 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