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 regex
$ vitest run [regex1] [regex2]
copy

Run the tests whose names match the given regex
$ vitest run --testNamePattern [regex]
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 [command] [options] [files...]
command: Optional subcommand (e.g., run, watch, ui, coverage). 'watch' is default.
options: Global or command-specific options (e.g., --config, --environment).
files...: Optional list of test files or glob patterns to run.

PARAMETERS

--root
    Specifies the project root directory where tests are located.

--config
    Provides a custom path to the Vitest configuration file.

--environment
    Defines the test environment, e.g., 'node' for server-side or 'jsdom'/'happy-dom' for browser-like DOM.

--globals
    Exposes test APIs (like 'describe', 'it', 'expect') globally, avoiding explicit imports.

--passWithNoTests
    Allows the test suite to pass even if no test files are found or executed.

--reporter
    Selects the test result reporter, e.g., 'default', 'verbose', 'json', 'dot'.

--watch
    Runs tests in interactive watch mode, re-running affected tests on file changes (default behavior).

--run
    Executes tests once and then exits, typically used in CI/CD environments.

--coverage
    Enables code coverage reporting for the test run, requiring a coverage provider.

--ui
    Starts the Vitest UI, providing a visual interface for test results.

--threads
    Controls whether tests are run in worker threads (default true), improving performance.

--shard /
    Distributes tests across multiple parallel CI jobs by sharding.

DESCRIPTION

Vitest is a blazing-fast unit test framework powered by Vite, designed for seamless integration into modern JavaScript and TypeScript projects. It offers a familiar API inspired by Jest, but with significant performance advantages, especially within the Vite ecosystem. Key features include instant Hot Module Replacement (HMR) for tests, out-of-the-box support for TypeScript and JSX, a smart and instant watch mode, and a highly configurable environment. It aims to provide an exceptional developer experience by significantly speeding up the test feedback loop, making it an ideal choice for contemporary web development workflows.

CAVEATS

Vitest is a Node.js-based tool and must be installed as a development dependency via a package manager (npm, yarn, pnpm) within a project. It is not a system-level command available by default on Linux. Its optimal performance and seamless integration are primarily realized within Vite-powered projects, where it leverages shared infrastructure. Configuration often resides in vitest.config.ts or extends vite.config.ts.

CONFIGURATION FLEXIBILITY

Vitest offers extensive configuration options via vitest.config.ts (or by extending vite.config.ts). This allows developers to fine-tune every aspect of the testing environment, including test glob patterns, reporters, test environments (e.g., JSDOM), setup files, mocks, and coverage thresholds, ensuring adaptability to diverse project requirements.

HOT MODULE REPLACEMENT (HMR) FOR TESTS

A core differentiator is Vitest's integration with Vite's HMR. When source code or test files change, Vitest intelligently re-runs only the affected tests almost instantaneously. This dramatically reduces waiting times during development, providing immediate feedback and significantly enhancing developer productivity compared to traditional test runners.

HISTORY

Vitest was publicly announced in late 2021 by the creators of Vite, quickly gaining momentum throughout 2022. Its inception aimed to fill the gap for a rapid, modern testing solution that integrates natively with the Vite development server, offering a similar developer experience. Inspired by Jest's API but built with Vite's underlying architecture, it addresses the need for faster test feedback loops in contemporary web development. Its rapid adoption underscores its effectiveness and the growing demand for highly optimized developer tools.

SEE ALSO

jest(1), mocha(1), npm(1), yarn(1), pnpm(1)

Copied to clipboard