Appearance
Vitest how-to
Unit tests for pure helpers and small logic. Browser journeys use Playwright.
Install
bash
pnpm add -D vitestConfig: vitest.config.mts (Node environment; colocated *.test.ts).
Commands
bash
pnpm test
pnpm run test:watch
pnpm exec vitest run path/to/file.test.ts
pnpm exec vitest run -t "partial name"Implementation examples
Config
ts
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
environment: 'node',
include: ['lib/**/*.test.ts', 'hooks/**/*.test.ts', 'components/**/*.test.ts', 'config/**/*.test.ts'],
exclude: ['node_modules', '.next', 'e2e'],
},
});Colocated test
ts
// components/AutoFitGrid.test.ts
import { describe, expect, it } from 'vitest';
import { normalizeAutoFitCssLength } from './AutoFitGrid';
describe('normalizeAutoFitCssLength', () => {
it('appends px to unitless numbers', () => {
expect(normalizeAutoFitCssLength('140')).toBe('140px');
});
});Scripts
json
{
"scripts": {
"test": "vitest run",
"test:watch": "vitest",
"check:unit": "pnpm run check && pnpm test"
}
}TDD for bug fixes
- Write a failing test.
- Confirm
vitest runfails. - Smallest production fix until green.