Comprehensive testing guidelines for Vitest and React Testing Library. Covers quality standards, AAA pattern, naming conventions, branch coverage, and best practices...
This skill defines quality standards, structure, and naming conventions for test code using Vitest and React Testing Library.
テスト作成時:
references/を参照test-reviewエージェントとの連携:
Use this checklist when writing or reviewing tests:
vitest (no global definitions)actual and expected variablesdescribe blocks[ComponentName].test.tsx or [functionName].test.tsAlways import necessary functions from vitest explicitly:
import { describe, expect, test, vi } from "vitest";
Rationale: Avoids relying on global definitions, making dependencies clear.
Write describe and test descriptions in Japanese with specific conditions and expected results:
test("商品が複数の場合、合計金額を返すこと", () => {
// ...
});
Format: "when [condition], it should [result]"
Strictly follow the AAA pattern with actual and expected variables:
test("商品が1つの場合、その価格を返すこと", () => {
// Arrange
const items = [{ price: 100 }];
const expected = 100;
// Act
const actual = calculateTotal(items);
// Assert
expect(actual).toBe(expected);
});
Rationale: Makes tests easier to read, understand, and maintain.
Each test verifies one behavior. For multiple properties, use object comparison:
// ✅ Correct: Object comparison
test("ユーザー情報が正しいこと", () => {
const expected = { name: "Taro", age: 30, email: "taro@example.com" };
const actual = getUser();
expect(actual).toEqual(expected);
});
Prohibit nested describe blocks. Use descriptive test names instead:
// ✅ Correct
describe("UserService", () => {
test("ユーザーが存在する場合、ユーザー情報を返すこと", () => {});
test("ユーザーが存在しない場合、エラーがスローされること", () => {});
});
Focus on what the component does, not how it does it:
// ✅ Testing user-visible behavior
test("カウンターが1増加すること", async () => {
const { user } = render(<Counter />);
const button = screen.getByRole("button", { name: "増やす" });
await user.click(button);
expect(screen.getByText("1")).toBeInTheDocument();
});
[ComponentName].test.tsx or [functionName].test.tsPlace shared data in the top-level describe scope:
describe("formatDate", () => {
// Shared data in top-level scope
const testDate = new Date("2024-01-15T10:30:00");
test("年月日形式でフォーマットされること", () => {
// Use shared data
});
});
Identify all branches and exception paths:
if/else branchesdefaultTest components based on user-visible behavior:
Use snapshots only for:
aria-*, role, etc.)Do NOT use snapshots for:
For detailed code examples and patterns, consult:
references/test-patterns.md: Comprehensive examples including:
references/aaa-pattern-guide.md: In-depth AAA pattern guidance including:
This skill emphasizes:
actual and expected variablesAlways prioritize test maintainability and readability over brevity.