About vitest-testing
A skill containing Vitest testing practices for assertions, asynchronous tests, mocks, and lifecycle hooks. It also defines verification gates for running the suite and ensuring asynchronous matchers use `await`.
Key features
- 01Reference for common assertions
- 02Guidance on asynchronous tests
- 03Examples of vi mock operations
- 04Overview of test hooks and methods
- 05Defined test verification gates
Use cases
- 01Write assertions in Vitest tests
- 02Test async functions and promises
- 03Verify test changes and async matchers
Install
Copy the command and run it in a terminal.
$
npx skills add https:/ / github.com/ existential-birds/ beagle --skill vitest-testingAfter installing, type/vitest-testingin Claude Code to confirm it loaded.
Bundled files
References and scripts installed along with SKILL.md.
CONFIG.mdMOCKING.mdPATTERNS.mdreferences/config.mdmocking.mdpatterns.mdDetails
From the project READMEVitest Best Practices
Quick Reference
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
// Equality
expect(value).toBe(42) // Strict (===)
expect(obj).toEqual({ a: 1 }) // Deep equality
expect(obj).toStrictEqual({ a: 1 }) // Strict deep (checks types)
// Truthiness
expect(value).toBeTruthy()
expect(value).toBeFalsy()
expect(value).toBeNull()
expect(value).toBeUndefined()
// Numbers
expect(0.1 + 0.2).toBeCloseTo(0.3)
expect(value).toBeGreaterThan(5)
// Strings/Arrays
expect(str).toMatch(/pattern/)
expect(str).toContain('substring')
expect(array).toContain(item)
expect(array).toHaveLength(3)
// Objects
expect(obj).toHaveProperty('key')
expect(obj).toHaveProperty('nested.key', 'value')
expect(obj).toMatchObject({ subset: 'of properties' })
// Exceptions
expect(() => fn()).toThrow()
expect(() => fn()).toThrow('error message')
expect(() => fn()).toThrow(/pattern/)
Async Testing
// 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
const mockFn = vi.fn()
mockFn.mockReturnValue(42)
mockFn.mockResolvedValue({ data: 'value' })
expect(mockFn).toHaveBeenCalled()
expect(mockFn).toHaveBeenCalledWith('arg1', 'arg2')
expect(mockFn).toHaveBeenCalledTimes(2)
Verification gates
Use this sequence when you add or change tests; each step has an objective pass condition.
- Run the test suite — From the package or workspace root, run the same command CI uses (check
package.jsonscripts; oftenvitest run,pnpm test, ornpm test). Pass: exit code is 0 and the report shows zero failing tests. - Async matchers — Pass: every
expect(…).resolvesandexpect(…).rejectsis prefixed withawait(await expect(...)), as in Async Testing. A line whereresolvesorrejectsappears withoutawaitfails this gate.
Additional Documentation
- Mocking: See references/mocking.md for module mocking, spying, cleanup
- Configuration: See references/config.md for vitest.config, setup files, coverage
- Patterns: See references/patterns.md for timers, snapshots, 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 |
Comments
Markdown supported