Skip to content

Commit 02662ff

Browse files
feat(vitest): add snapshot testing documentation (#66)
1 parent fca489b commit 02662ff

25 files changed

Lines changed: 5136 additions & 37 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Generation Info
2+
3+
- **Source:** `sources/vitest`
4+
- **Git SHA:** `4a7321e10672f00f0bb698823a381c2cc245b8f7`
5+
- **Generated:** 2026-01-28

.claude/skills/vitest/SKILL.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
name: vitest
3+
description: Vitest fast unit testing framework powered by Vite with Jest-compatible API. Use when writing tests, mocking, configuring coverage, or working with test filtering and fixtures.
4+
metadata:
5+
author: Anthony Fu
6+
version: "2026.1.28"
7+
source: Generated from https://github.com/vitest-dev/vitest, scripts located at https://github.com/antfu/skills
8+
---
9+
10+
Vitest is a next-generation testing framework powered by Vite. It provides a Jest-compatible API with native ESM, TypeScript, and JSX support out of the box. Vitest shares the same config, transformers, resolvers, and plugins with your Vite app.
11+
12+
**Key Features:**
13+
- Vite-native: Uses Vite's transformation pipeline for fast HMR-like test updates
14+
- Jest-compatible: Drop-in replacement for most Jest test suites
15+
- Smart watch mode: Only reruns affected tests based on module graph
16+
- Native ESM, TypeScript, JSX support without configuration
17+
- Multi-threaded workers for parallel test execution
18+
- Built-in coverage via V8 or Istanbul
19+
- Snapshot testing, mocking, and spy utilities
20+
21+
> The skill is based on Vitest 3.x, generated at 2026-01-28.
22+
23+
## Core
24+
25+
| Topic | Description | Reference |
26+
|-------|-------------|-----------|
27+
| Configuration | Vitest and Vite config integration, defineConfig usage | [core-config](references/core-config.md) |
28+
| CLI | Command line interface, commands and options | [core-cli](references/core-cli.md) |
29+
| Test API | test/it function, modifiers like skip, only, concurrent | [core-test-api](references/core-test-api.md) |
30+
| Describe API | describe/suite for grouping tests and nested suites | [core-describe](references/core-describe.md) |
31+
| Expect API | Assertions with toBe, toEqual, matchers and asymmetric matchers | [core-expect](references/core-expect.md) |
32+
| Hooks | beforeEach, afterEach, beforeAll, afterAll, aroundEach | [core-hooks](references/core-hooks.md) |
33+
34+
## Features
35+
36+
| Topic | Description | Reference |
37+
|-------|-------------|-----------|
38+
| Mocking | Mock functions, modules, timers, dates with vi utilities | [features-mocking](references/features-mocking.md) |
39+
| Snapshots | Snapshot testing with toMatchSnapshot and inline snapshots | [features-snapshots](references/features-snapshots.md) |
40+
| Coverage | Code coverage with V8 or Istanbul providers | [features-coverage](references/features-coverage.md) |
41+
| Test Context | Test fixtures, context.expect, test.extend for custom fixtures | [features-context](references/features-context.md) |
42+
| Concurrency | Concurrent tests, parallel execution, sharding | [features-concurrency](references/features-concurrency.md) |
43+
| Filtering | Filter tests by name, file patterns, tags | [features-filtering](references/features-filtering.md) |
44+
45+
## Advanced
46+
47+
| Topic | Description | Reference |
48+
|-------|-------------|-----------|
49+
| Vi Utilities | vi helper: mock, spyOn, fake timers, hoisted, waitFor | [advanced-vi](references/advanced-vi.md) |
50+
| Environments | Test environments: node, jsdom, happy-dom, custom | [advanced-environments](references/advanced-environments.md) |
51+
| Type Testing | Type-level testing with expectTypeOf and assertType | [advanced-type-testing](references/advanced-type-testing.md) |
52+
| Projects | Multi-project workspaces, different configs per project | [advanced-projects](references/advanced-projects.md) |
Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
---
2+
name: test-environments
3+
description: Configure environments like jsdom, happy-dom for browser APIs
4+
---
5+
6+
# Test Environments
7+
8+
## Available Environments
9+
10+
- `node` (default) - Node.js environment
11+
- `jsdom` - Browser-like with DOM APIs
12+
- `happy-dom` - Faster alternative to jsdom
13+
- `edge-runtime` - Vercel Edge Runtime
14+
15+
## Configuration
16+
17+
```ts
18+
// vitest.config.ts
19+
defineConfig({
20+
test: {
21+
environment: 'jsdom',
22+
23+
// Environment-specific options
24+
environmentOptions: {
25+
jsdom: {
26+
url: 'http://localhost',
27+
},
28+
},
29+
},
30+
})
31+
```
32+
33+
## Installing Environment Packages
34+
35+
```bash
36+
# jsdom
37+
npm i -D jsdom
38+
39+
# happy-dom (faster, fewer APIs)
40+
npm i -D happy-dom
41+
```
42+
43+
## Per-File Environment
44+
45+
Use magic comment at top of file:
46+
47+
```ts
48+
// @vitest-environment jsdom
49+
50+
import { expect, test } from 'vitest'
51+
52+
test('DOM test', () => {
53+
const div = document.createElement('div')
54+
expect(div).toBeInstanceOf(HTMLDivElement)
55+
})
56+
```
57+
58+
## jsdom Environment
59+
60+
Full browser environment simulation:
61+
62+
```ts
63+
// @vitest-environment jsdom
64+
65+
test('DOM manipulation', () => {
66+
document.body.innerHTML = '<div id="app"></div>'
67+
68+
const app = document.getElementById('app')
69+
app.textContent = 'Hello'
70+
71+
expect(app.textContent).toBe('Hello')
72+
})
73+
74+
test('window APIs', () => {
75+
expect(window.location.href).toBeDefined()
76+
expect(localStorage).toBeDefined()
77+
})
78+
```
79+
80+
### jsdom Options
81+
82+
```ts
83+
defineConfig({
84+
test: {
85+
environmentOptions: {
86+
jsdom: {
87+
url: 'http://localhost:3000',
88+
html: '<!DOCTYPE html><html><body></body></html>',
89+
userAgent: 'custom-agent',
90+
resources: 'usable',
91+
},
92+
},
93+
},
94+
})
95+
```
96+
97+
## happy-dom Environment
98+
99+
Faster but fewer APIs:
100+
101+
```ts
102+
// @vitest-environment happy-dom
103+
104+
test('basic DOM', () => {
105+
const el = document.createElement('div')
106+
el.className = 'test'
107+
expect(el.className).toBe('test')
108+
})
109+
```
110+
111+
## Multiple Environments per Project
112+
113+
Use projects for different environments:
114+
115+
```ts
116+
defineConfig({
117+
test: {
118+
projects: [
119+
{
120+
test: {
121+
name: 'unit',
122+
include: ['tests/unit/**/*.test.ts'],
123+
environment: 'node',
124+
},
125+
},
126+
{
127+
test: {
128+
name: 'dom',
129+
include: ['tests/dom/**/*.test.ts'],
130+
environment: 'jsdom',
131+
},
132+
},
133+
],
134+
},
135+
})
136+
```
137+
138+
## Custom Environment
139+
140+
Create custom environment package:
141+
142+
```ts
143+
// vitest-environment-custom/index.ts
144+
import type { Environment } from 'vitest/runtime'
145+
146+
export default <Environment>{
147+
name: 'custom',
148+
viteEnvironment: 'ssr', // or 'client'
149+
150+
setup() {
151+
// Setup global state
152+
globalThis.myGlobal = 'value'
153+
154+
return {
155+
teardown() {
156+
delete globalThis.myGlobal
157+
},
158+
}
159+
},
160+
}
161+
```
162+
163+
Use with:
164+
165+
```ts
166+
defineConfig({
167+
test: {
168+
environment: 'custom',
169+
},
170+
})
171+
```
172+
173+
## Environment with VM
174+
175+
For full isolation:
176+
177+
```ts
178+
export default <Environment>{
179+
name: 'isolated',
180+
viteEnvironment: 'ssr',
181+
182+
async setupVM() {
183+
const vm = await import('node:vm')
184+
const context = vm.createContext()
185+
186+
return {
187+
getVmContext() {
188+
return context
189+
},
190+
teardown() {},
191+
}
192+
},
193+
194+
setup() {
195+
return { teardown() {} }
196+
},
197+
}
198+
```
199+
200+
## Browser Mode (Separate from Environments)
201+
202+
For real browser testing, use Vitest Browser Mode:
203+
204+
```ts
205+
defineConfig({
206+
test: {
207+
browser: {
208+
enabled: true,
209+
name: 'chromium', // or 'firefox', 'webkit'
210+
provider: 'playwright',
211+
},
212+
},
213+
})
214+
```
215+
216+
## CSS and Assets
217+
218+
In jsdom/happy-dom, configure CSS handling:
219+
220+
```ts
221+
defineConfig({
222+
test: {
223+
css: true, // Process CSS
224+
225+
// Or with options
226+
css: {
227+
include: /\.module\.css$/,
228+
modules: {
229+
classNameStrategy: 'non-scoped',
230+
},
231+
},
232+
},
233+
})
234+
```
235+
236+
## Fixing External Dependencies
237+
238+
If external deps fail with CSS/asset errors:
239+
240+
```ts
241+
defineConfig({
242+
test: {
243+
server: {
244+
deps: {
245+
inline: ['problematic-package'],
246+
},
247+
},
248+
},
249+
})
250+
```
251+
252+
## Key Points
253+
254+
- Default is `node` - no browser APIs
255+
- Use `jsdom` for full browser simulation
256+
- Use `happy-dom` for faster tests with basic DOM
257+
- Per-file environment via `// @vitest-environment` comment
258+
- Use projects for multiple environment configurations
259+
- Browser Mode is for real browser testing, not environment
260+
261+
<!--
262+
Source references:
263+
- https://vitest.dev/guide/environment.html
264+
-->

0 commit comments

Comments
 (0)