Skip to content
Back to skills

Vitest Testing

ASecurity

Vitest v4 testing patterns, configuration, mocking, and verification gates. Use when writing unit or integration tests, configuring vitest.config, mocking modules with vi.mock/vi.hoisted/vi.fn, setting up coverage, or configuring Browser Mode.

  • 92 stars
  • 0 votes
  • 0 copies
  • 5 views
  • Added February 8, 2026
developmenttestingdocumentation

Security analysis

A100/100

Pro scans all 7 files and shows the line behind each finding

Scanned November 26, 2026

npx -y skills add existential-birds/beagle --skill vitest-testing --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Vitest Testing?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Vitest Testing
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/existential-birds-vitest-testing/badge)](https://www.skillsdirectory.com/skills/existential-birds-vitest-testing)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: vitest-testing
description: Vitest v4 testing patterns, configuration, mocking, and verification gates. Use when writing unit or integration tests, configuring vitest.config, mocking modules with vi.mock/vi.hoisted/vi.fn, setting up coverage, or configuring Browser Mode.
---

# Vitest Best Practices

## Quick Reference

```ts
import { describe, it, expect, beforeEach, vi } from 'vitest'

describe('feature name', () => {
  beforeEach(() => {
    vi.clearAllMocks()
  })

  it('should do something specific', () => {
    expect(actual).toBe(expected)
  })

  it.todo('planned test')
  it.skip('temporarily disabled')
  it.only('run only this during dev')
})
```

## Common Assertions

```ts
// Equality
expect(value).toBe(42)                    // Strict (===)
expect(obj).toEqual({ a: 1 })             // Deep equality
expect(obj).toStrictEqual({ a: 1 })       // Strict deep (checks types & undefined keys)

// Numbers & Strings/Arrays
expect(0.1 + 0.2).toBeCloseTo(0.3)
expect(str).toMatch(/pattern/)
expect(array).toContain(item)
expect(array).toHaveLength(3)

// Objects
expect(obj).toHaveProperty('nested.key', 'value')
expect(obj).toMatchObject({ subset: 'of properties' })

// Exceptions
expect(() => fn()).toThrow('error message')
expect(() => fn()).toThrow(/pattern/)
```

## Async Testing

```ts
// Async/await (preferred)
it('fetches data', async () => {
  const data = await fetchData()
  expect(data).toEqual({ id: 1 })
})

// Promise matchers - ALWAYS await these
await expect(fetchData()).resolves.toEqual({ id: 1 })
await expect(fetchData()).rejects.toThrow('Error')

// Wrong - creates false positive
expect(promise).resolves.toBe(value)  // Missing await!
```

## Quick Mock Reference

```ts
const mockFn = vi.fn()
mockFn.mockReturnValue(42)
mockFn.mockResolvedValue({ data: 'value' })

expect(mockFn).toHaveBeenCalled()
expect(mockFn).toHaveBeenCalledWith('arg1', 'arg2')
expect(mockFn).toHaveBeenCalledTimes(2)
```

For `vi.hoisted()`, `vi.mock()`, `importOriginal`, and mock cleanup, see [references/mocking.md](references/mocking.md).

## Verification gates

Use this sequence when you add or change tests; each step has an objective pass condition.

1. **Run the test suite** — From the package or workspace root, run the same command CI uses (check `package.json` scripts; often `vitest run`, `pnpm test`, or `npm test`). **Pass:** exit code is 0 and the report shows zero failing tests.
2. **Async matchers** — **Pass:** every `expect(…).resolves` and `expect(…).rejects` is prefixed with `await` (`await expect(...)`), as in [Async Testing](#async-testing). A line where `resolves` or `rejects` appears without `await` fails this gate.
3. **Vitest v4 config compatibility** — **Pass:** config uses top-level `maxWorkers` / `isolate` / `fileParallelism` (not removed v3 `poolOptions`), explicit `coverage.include` for AST coverage, and `@vitest/browser-playwright` (`provider: playwright()`) if Browser Mode is configured.

## Additional Documentation

- **Mocking**: See [references/mocking.md](references/mocking.md) for `vi.hoisted()`, module mocking, spying, and cleanup
- **Configuration**: See [references/config.md](references/config.md) for Vitest v4 `vitest.config`, workers, Browser Mode, and coverage
- **Patterns**: See [references/patterns.md](references/patterns.md) for timers, snapshots, and anti-patterns

## Test Methods Quick Reference

| Method | Purpose |
|--------|---------|
| `it()` / `test()` | Define test |
| `describe()` | Group tests |
| `beforeEach()` / `afterEach()` | Per-test hooks |
| `beforeAll()` / `afterAll()` | Per-suite hooks |
| `.skip` | Skip test/suite |
| `.only` | Run only this |
| `.todo` | Placeholder |
| `.concurrent` | Parallel execution |
| `.each([...])` | Parameterized tests |

Files in this skill

  • CONFIG.md3.1 KB
  • MOCKING.md2.7 KB
  • PATTERNS.md3.3 KB
  • SKILL.md2.9 KB
  • references/config.md3.4 KB
  • references/mocking.md3.2 KB
  • references/patterns.md3.5 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.