shinpr/typescript-testing
Applies Vitest test design and quality standards. Provides coverage requirements and mock usage guides. Use when writing unit tests.
npx skills add https://github.com/shinpr/ai-coding-project-boilerplate --skill typescript-testing
Inspect package.json, the lockfile, test configuration, and existing test imports before selecting a framework or command. Apply Vitest-specific rules only when Vitest is configured; otherwise use the repository's configured TypeScript test harness while preserving the behavior, isolation, and evidence rules below. If no runnable harness is identifiable, report the inspected paths and missing command or configuration.
import { describe, it, expect, beforeEach, vi } from 'vitest'vi.mock()__tests__/ directory beside the module under test{target-file-name}.test.ts{target-file-name}.int.test.tsKeep every committed test active. Repair a test that protects current behavior; remove a test only when its behavior is no longer required and the source requirement or implementation contract confirms the removal.
Include boundary values and error cases alongside happy paths.
Use expected values that are independent of the implementation calculation: state the contract's value directly as a literal, or take it from a separate authoritative fixture or specification. An expectation computed from the same constants or formula as the subject passes even when both are wrong. When a mock supplies the input, the expected value differs from the mock's return value wherever the implementation transforms it.
Verify results, not invocation order or count.
Each test asserts the property its consumer depends on and the state the operation established, not merely that a value came back.
A probe that checks whether something works passes only when it uses the consumer's boundary and asserts the exact property the consumer needs.
Command exit status, successful import, and object existence show the thing is reachable, so treat them as the probe's preconditions and put the consumer-facing property in the assertion.
| Probe intent | Setup evidence (insufficient alone) | Assert instead |
|---|---|---|
| The module is usable | import resolves, expect(mod).toBeDefined() | Call the exported function through the consumer's entry point and assert its returned value or effect |
| The command works | Exit code 0 | The output, file, or state change the caller consumes |
| The config is applied | The config file parses | The observable behavior the config is supposed to change |
| The migration ran | The command reported success | A query through the real engine returns the migrated shape |
Use real implementations for every in-process component whose coordination is under test. Substitute a direct external I/O dependency when the test targets higher-layer behavior; use the real engine or a production-equivalent test instance when the external adapter, query, migration, or service contract itself is the target. When substituting, still assert the request the subject sends and the response shape it accepts, so the boundary contract stays verified.
Use fast-check in fc.assert(fc.property(...)) form when a Design Doc AC carries a Property annotation.
Type a mock to the surface the subject under test actually consumes — Pick<T, 'usedMethod'> — rather than the full interface, so an unused method changing shape does not break the test and a consumed one does. Constrain mock object literals with satisfies against that picked type so an extra or misnamed property fails at compile time.
Mocks validate call patterns, so these data-layer properties pass through undetected under mock-only testing:
Routing rule: when one of these properties is the target — including the repository or data-access implementation itself — verify against a real engine per the ladder below. When data access is a dependency rather than the subject, mocks are correct: business logic receiving data (mock the repository, test the service), error-handling paths (connection failures, timeouts), and unit tests where the data layer is not under test.
Options for verifying data layer correctness against a real database engine:
Select the first option that matches repository evidence:
When none is available and data-layer correctness is the target, stop and report the missing environment prerequisite. A mock-only result is not evidence of query, schema, constraint, or migration correctness.
Generated data-access code can be syntactically correct while referencing schema elements that do not exist, and mock-based tests pass either way. Design Docs therefore carry explicit schema references, so review can cross-check documented schema against the data-access code.
Take shinpr/typescript-testing from the repository into ~/.claude/skills for personal
use, or into .claude/skills inside a project.
The agent identifies a skill by the name field in its header. Two skills with the
same name cannot sit side by side — one of them will be ignored.