LinuxCommandLibrary

vitest

Run unit tests with Vitest

TLDR

Run all available tests

$ vitest run
copy

Run the test suites from the given files
$ vitest run [path/to/file1 path/to/file2 ...]
copy

Run the test suites from files within the current and subdirectories, whose paths match the given regular expression
$ vitest run [regular_expression1] [regular_expression2]
copy

Run the tests whose names match the given regular expression
$ vitest run --testNamePattern [regular_expression]
copy

Watch files for changes and automatically re-run related tests
$ vitest
copy

Run tests with coverage
$ vitest run --coverage
copy

Run all tests but stops immediately after the first test failure
$ vitest run --bail=1
copy

Display help
$ vitest --help
copy

SYNOPSIS

vitest [options] [files...]

PARAMETERS

--config
    Specify a custom config file to use.

--root
    Specify the root directory.

--environment
    Specify the test environment.

--watch
    Enable watch mode. Vitest will re-run tests when files change.

--run
    Run tests once and exit.

--ui
    Enable UI mode to visualize the test results in a browser.

--coverage
    Enable coverage reporting.
Note:Requires additional setup and plugins.

--reporter
    Specify the reporter to use (e.g., 'default', 'verbose', 'json').

--globals
    Expose Vitest API globally.

--clearCache
    Clears the cache.

DESCRIPTION

Vitest is a blazing fast unit test framework powered by Vite. It aims to provide a seamless testing experience, especially for projects already using Vite as their build tool. It leverages the speed and configuration of Vite to offer near-instant hot module replacement (HMR), fast startup, and excellent TypeScript support. Vitest encourages writing tests close to your source code and allows for easy mocking, spying, and stubbing. It runs tests in Node.js or browsers. Vitest offers a developer-friendly environment with features like watch mode, code coverage reporting, and a rich API for creating expressive and maintainable tests. Its ease of use and speed make it a compelling alternative to traditional testing frameworks.

FILE PATTERNS

Vitest uses glob patterns to discover test files. You can specify the files directly on the command line or configure them in the `vitest.config.ts` file.

CONFIGURATION

The Vitest configuration file (usually `vitest.config.ts`) allows you to customize various aspects of the test environment, such as the test environment, reporters, coverage settings, and more.
This file defines how Vitest behaves during test runs.

DEBUGGING

Vitest supports debugging in VS Code and other IDEs. You can configure your IDE to attach to the Vitest process and step through your test code.

HISTORY

Vitest was created to provide a faster and more streamlined testing experience for Vite-based projects.
It leverages Vite's configuration and infrastructure, making it easy to integrate into existing projects.
Its popularity has grown significantly due to its speed, ease of use, and seamless integration with the Vite ecosystem.

SEE ALSO

jest(1), mocha(1), ava(1)

Copied to clipboard