typescript-testinglisted
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