Testing & Code Quality
Vitest Unit Tests
Vitest is a Vite-native test runner for JavaScript and TypeScript projects, supporting fast unit tests, mocks, fake timers, coverage, browser mode, and familiar Jest-like APIs.
- Unit tests should verify behavior, not implementation trivia
- Fast tests shape developer behavior
- Vitest fits Vite projects naturally
- Coverage is a signal, not a goal
- Test boundaries should be chosen deliberately
import { describe, expect, it } from "vitest"
import { parseUserId } from "./user-id"
describe("parseUserId", () => {
it("accepts valid user ids", () => {
expect(parseUserId("user_123")).toBe("user_123")
})
it("rejects invalid ids", () => {
expect(() => parseUserId("product_123")).toThrow("Invalid")
})
})| Target | Why |
|---|---|
| Pure functions | Deterministic and fast |
| Parsers/validators | Boundary safety |
| Reducers/state machines | Many state transitions |
| API wrappers | Error/status handling |
| Formatting/mapping logic | Easy regressions |