vitest

Write, configure, and run Vitest unit tests with mocking, coverage, and fixtures.

Updated Mar 18, 2026
One-click install
npx skills add https://github.com/Surojit012/CMO --skill vitest-surojit012
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: vitest
Source: https://github.com/Surojit012/CMO/tree/main/.agents/skills/vitest
Command: npx skills add https://github.com/Surojit012/CMO --skill vitest-surojit012

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Setting up and maintaining a fast, reliable test suite requires deep knowledge of Vitest's configuration options, APIs, and mocking utilities, which are spread across extensive documentation. This Skill consolidates that knowledge so you can write tests, configure environments, and debug test setups without leaving your workflow. ## Core Features & Use Cases - Test Authoring Guidance: Covers the test/describe/expect APIs, lifecycle hooks, parameterized tests, and custom fixtures via test.extend. - Mocking & Utilities: Explains vi.fn, vi.spyOn, module mocking with vi.mock, fake timers, and stubbed globals or environment variables. - Configuration & Tooling: Details vitest.config.ts setup, coverage with V8 or Istanbul, test environments (node, jsdom, happy-dom), multi-project workspaces, sharding, and CLI filtering. - Use Case: You are migrating a Jest test suite to Vitest and need to configure jsdom for DOM tests, set up coverage thresholds, and convert jest.mock calls to vi.mock with hoisted factories. ## Quick Start Ask the assistant to help you write a Vitest test file with mocked API calls and coverage configuration for your project.

Frequently Asked Questions about vitest

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I mock a module in Vitest?

Use vi.mock with a factory function, which is hoisted to the top of the file before imports run. For partial mocks, call importOriginal inside the factory and spread the actual module. Use vi.doMock for non-hoisted dynamic mocking with await import.

How to set up Vitest with jsdom for DOM testing?

Install jsdom as a dev dependency and set environment: 'jsdom' in the test section of vitest.config.ts. Alternatively, add a // @vitest-environment jsdom comment at the top of individual test files. happy-dom is a faster alternative with fewer APIs.

Vitest vs Jest: can I migrate my existing tests?

Vitest provides a Jest-compatible API, so most test suites work as drop-in replacements. Replace jest.fn and jest.mock with vi.fn and vi.mock from 'vitest', and move configuration from jest.config into the test property of vitest.config.ts.

How do I run only specific tests in Vitest?

Pass a filename pattern as a CLI argument, use -t or --testNamePattern to filter by test name, or add test.only in the file. You can also use --changed for tests affected by git changes and --project to run a specific workspace project.

Why is my vi.mock factory not seeing my variables?

vi.mock calls are hoisted above imports, so top-level variables are not yet defined when the factory registers. Wrap shared values in vi.hoisted to create them before the mock factory executes, then reference them inside vi.mock.

Does Vitest support code coverage thresholds?

Yes, configure coverage.thresholds in vitest.config.ts with minimum percentages for lines, functions, branches, and statements. Install @vitest/coverage-v8 for the default fast provider or @vitest/coverage-istanbul for broader compatibility, then run vitest run --coverage.