← ClaudeAtlas

typescript-testinglisted

TypeScript and JavaScript testing patterns using Vitest (primary) and Jest. Activate this skill whenever you are writing or fixing tests in a TypeScript or JavaScript project, touching *.test.ts / *.spec.ts / *.test.tsx files, configuring vitest.config.ts or jest.config.ts, asking about mocking strategies (vi.fn, vi.mock, MSW), debugging flaky async tests, or wiring up coverage thresholds. Also activates when test output shows "floating promise", "timeout", or "cannot find module" in a test context. For TDD methodology and the coverage policy, see tdd-workflow.
sardonyx0827/dotfiles · ★ 0 · Testing & QA · score 72
Install: claude install-skill sardonyx0827/dotfiles
# TypeScript Testing Patterns Reliable, maintainable tests in TypeScript using Vitest as the primary runner. Jest equivalents are noted where syntax differs. For TDD methodology and the coverage policy, see the **tdd-workflow** skill. --- ## 1. Test Structure Name every `describe` and `it` block so the full path reads as a plain sentence. A reader should understand what is being tested and what is expected without opening the source file. ```ts // ❌ WRONG: cryptic names, no context describe("utils", () => { it("test1", () => { ... }) it("works", () => { ... }) }) // ✅ CORRECT: subject → scenario → expectation describe("formatCurrency", () => { it("returns a USD string when given a positive amount", () => { // Arrange const amount = 1234.5 // Act const result = formatCurrency(amount, "USD") // Assert expect(result).toBe("$1,234.50") }) it("returns '—' when amount is null", () => { expect(formatCurrency(null, "USD")).toBe("—") }) }) ``` **Why Arrange-Act-Assert matters**: blank lines between phases make failures obvious and diffs readable. One behavior per test — when a test has two `expect` calls that test different behaviors, split it. --- ## 2. Table-Driven Tests with `test.each` Mirror the Go table-test philosophy: enumerate cases in a data structure, not in repeated `it` blocks. This scales from 2 cases to 20 without duplication. ```ts // ✅ CORRECT: test.each with tagged template literals (Vitest / Jest identical) desc