write-tests
GitHub为现有代码添加测试覆盖,根据模块类型(路由、仓库等)选择正确 Mock 策略。处理 mockReset 陷阱及 TDZ 问题,避免常见错误,确保测试隔离与稳定性。
Trigger Scenarios
Install
npx skills add NeverSight/learn-skills.dev --skill write-tests -g -y
SKILL.md
Frontmatter
{
"name": "write-tests",
"description": "Add test coverage to existing code with correct mock patterns. Use when adding tests to untested modules, writing regression tests for bugs, or user asks to test a specific file. Handles mockReset:true, vi.hoisted(), forwarding pattern, and test app builder utilities."
}
Write Tests for Existing Code
Before Writing, Ask Yourself
- Module type? Route handler, repository, plugin, utility, or service — each has a different mock strategy
- Blast radius? Does this module have side effects (DB writes, API calls) that need isolation?
- Nearest test file? Find the closest
*.test.tsand match its structure exactly
Mock Strategy by Module Type
| Module Type | Strategy |
|---|---|
| Route handler | Test app builder + session simulation + app.inject() |
| Repository | Mock DB connection + counter-based execute |
| Framework plugin | Real framework instance + selective dependency mocks |
| Pure utility | No mocks — test inputs/outputs directly |
| Service w/ DI | Mock injected deps via forwarding pattern |
Mock Setup (mockReset: true)
If your test runner uses mockReset: true, most examples from the internet will silently fail.
const { mockFn } = vi.hoisted(() => ({
mockFn: vi.fn(),
}));
vi.mock("./dependency", () => ({
dependency: (...args: unknown[]) => mockFn(...args),
}));
beforeEach(() => {
// MUST reconfigure here — mockReset clears return values between tests
mockFn.mockResolvedValue(defaultResult);
});
For complex TDZ cases (multiple interdependent mocks), use the globalThis registry pattern.
NEVER
- NEVER chain
mockResolvedValueOnce—mockResetclears the chain between tests. Use counter-basedmockImplementationinstead. - NEVER define mock variables at module scope then reference in
vi.mock()factories — hoisting creates a temporal dead zone. Usevi.hoisted()or globalThis. - NEVER
vi.importActual()for modules with side effects — use selective re-exports. - NEVER test implementation details (private state, internal call order) — test behavior through the public API.
- NEVER copy mock patterns from other projects — check YOUR test runner config first.
- NEVER modify source code — this skill writes tests only.
Metacognitive Rule
If >3 tests fail on first run: STOP. The root cause is almost certainly a mock wiring issue affecting all tests, not individual test logic errors. Re-examine the mock setup strategy holistically before fixing tests one by one.
Run
npx vitest run <test-file> --reporter=verbose
Arguments
$ARGUMENTS: Path to the source file or module to cover- Example:
/write-tests src/routes/admin/settings.ts - If empty, ask the user which file needs test coverage
- Example:
Version History
- e0220ca Current 2026-07-05 22:58


