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
Simple behavior test
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")
  })
})
One happy path, one failure path, both tied to observable behavior.
Good unit-test targets
TargetWhy
Pure functionsDeterministic and fast
Parsers/validatorsBoundary safety
Reducers/state machinesMany state transitions
API wrappersError/status handling
Formatting/mapping logicEasy regressions
Sources
  • Vitest DocumentationVitest guide
  • Vite DocumentationVite integration
  • Node.js Documentation and Learn Node.jsTesting