vitest

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

Updated Jul 17, 2026
One-click install
npx skills add https://github.com/usmangurowa/socialmocks --skill vitest-usmangurowa
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: vitest
Source: https://github.com/usmangurowa/socialmocks/tree/main/.agents/skills/vitest
Command: npx skills add https://github.com/usmangurowa/socialmocks --skill vitest-usmangurowa

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Setting up and maintaining a JavaScript/TypeScript test suite involves many decisions: configuration, mocking strategies, coverage thresholds, test filtering, and multi-project setups. This Skill provides structured, reference-backed guidance for the Vitest testing framework so you can write correct tests and configurations without digging through documentation. ## Core Features & Use Cases - Test Authoring: Covers the test/describe/expect APIs, lifecycle hooks, parameterized tests with test.each and test.for, and custom fixtures via test.extend. - Mocking & Utilities: Guides module mocking with vi.mock, spying with vi.spyOn, fake timers, date mocking, and global/environment stubbing. - Coverage, Filtering & Concurrency: Explains V8 vs Istanbul coverage providers, test filtering by name/tags/changed files, concurrent tests, and CI sharding. - Advanced Setups: Documents test environments (jsdom, happy-dom), type-level testing with expectTypeOf, and multi-project workspace configurations for monorepos. - Use Case: You are adding tests to a monorepo package and need jsdom for DOM tests, mocked API modules, and an 80% coverage threshold—this Skill walks you through the exact vitest.config.ts and test code. ## Quick Start Ask the assistant to help you write a Vitest test file with mocked dependencies and a 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. For partial mocks, use the importOriginal argument to spread the actual module and override specific exports. Use vi.doMock for non-hoisted dynamic mocking with await import.

How do I set up code coverage in Vitest?

Run vitest with the --coverage flag or set coverage.enabled in the config. Choose the v8 provider for speed or istanbul for broader compatibility, then configure reporters, include/exclude patterns, and thresholds for lines, functions, branches, and statements.

What is the difference between jsdom and happy-dom in Vitest?

jsdom provides a fuller browser environment simulation with more DOM APIs, while happy-dom is faster but supports fewer APIs. Set the environment in vitest.config.ts or per file with the // @vitest-environment comment.

Can Vitest run tests in parallel or across CI machines?

Yes. Test files run in parallel across worker threads by default, and test.concurrent runs tests within a file concurrently. For CI, use --shard=index/count to split tests across machines and --merge-reports to combine results.

How do I create reusable test fixtures in Vitest?

Use test.extend to define fixtures with setup and teardown logic, providing values through the use callback. Fixtures are lazy, support auto and file or worker scopes, and can be composed by extending an already extended test.

Why is my vi.mock factory unable to reference variables?

vi.mock calls are hoisted above imports, so regular variables are undefined at that point. Wrap shared mock values in vi.hoisted so they are initialized before the mock factory executes.