testing
Guide for testing TypeScript projects with Vitest, Jest, and Playwright. Use when writing unit tests, mocking modules or timers, asserting types at compile…
by vinnie357★ 30 GitHub stars
Install testing in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
About this skill
TypeScript Testing
Vitest and Jest for unit/integration tests, Playwright for end-to-end, type-level testing, coverage, and CI wiring.
Choosing Vitest vs Jest
Both are viable; the deciding factor is the rest of the toolchain, not raw feature count.
- Vitest reads the project's existing Vite config and reuses its transform pipeline, so a Vite-based app (React/Vue/Svelte via Vite, or a Vite-built library) gets test config for near-free. Vitest requires Vite ≥6 and Node ≥20. Its API is Jest-compatible (
describe/test/expect/vi.fn()), so migrating an existing Jest suite is mostly a find-and-replace of the mock namespace. - Jest is the default for a project with no Vite in its pipeline — plain Node libraries, Next.js (which documents first-class Jest support), or a codebase already standardized on Jest tooling (snapshot serializers, custom matchers written against Jest's APIs). TypeScript support comes via
ts-jest(type-checks as part of the test run) or Babel (transpile-only, faster, no type errors caught).
Restraint applies here directly: don't add Vitest to a project that already has a working Jest suite just because Vitest is newer, and don't bolt Jest onto a Vite app that would get Vitest for free.
Vitest
Setup
npm install -D vitest
No separate config file is required — Vitest reads vite.config.ts by default. Add a dedicated vitest.config.ts only when test-specific settings (environment, coverage thresholds, setup files) would otherwise clutter the Vite config:
// vitest.config.ts
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
environment: "node", // or "jsdom" / "happy-dom" for DOM tests
globals: false, // false = import describe/test/expect explicitly (recommended)
coverage: {
provider: "v8",
thresholds: { lines: 80, functions: 80, branches: 75, statements: 80 },
},
},
});
Leaving globals: false and importing describe/test/expect/vi explicitly from "vitest" keeps test files free of ambient globals — the same reasoning as avoiding any: an explicit import is a grep target and a type-checked one.
Assertions and structure
import { describe, test, expect } from "vitest";
import { add } from "./math.js";
describe("add", () => {
test("adds two positive numbers", () => {
expect(add(2, 3)).toBe(5);
});
test("adds negative numbers", () => {
expect(add(-1, -1)).toBe(-2);
});
});
Mocking
import { vi, expect, test } from "vitest";
import { fetchUser } from "./api.js";
vi.mock("./api.js", () => ({
fetchUser: vi.fn().mockResolvedValue({ id: 1, name: "Ada" }),
}));
test("uses the mocked fetch", async () => {
const user = await fetchUser(1);
expect(user.name).toBe("Ada");
});
vi.fn() creates a spy/mock function; vi.mock(path, factory) replaces a module's exports for the whole file. vi.useFakeTimers() / vi.advanceTimersByTime(ms) control time for code under test that uses setTimeout/setInterval — mock the boundary (the module doing I/O or the clock), never the function under test itself, matching the /core:agent-loop mock-external-boundaries convention this workspace uses across languages.
Jest
Setup
npm install -D jest ts-jest @types/jest
npx ts-jest config:init
ts-jest type-checks test files as part of the run — a type error in a test fails the test run, not just tsc. The Babel path (babel-jest + @babel/preset-typescript) is faster because it strips types without checking them; pick it only when a separate tsc --noEmit step in CI already covers type-checking, so Babel isn't silently letting a type error through untested.
Basic test
// math.test.ts
import { add } from "./math";
test("adds 1 + 2 to equal 3", () => {
expect(add(1, 2)).toBe(3);
});
Mocking modules
import { fetchUser } from "./api";
jest.mock("./api");
const mockFetchUser = fetchUser as jest.MockedFunction<typeof fetchUser>;
test("uses the mocked fetch", async () => {
mockFetchUser.mockResolvedValue({ id: 1, name: "Ada" });
const user = await fetchUser(1);
expect(user.name).toBe("Ada");
});
The as jest.MockedFunction<typeof fetchUser> cast is the standard Jest+TypeScript pattern for getting mock-specific methods (mockResolvedValue, mockReturnValueOnce) on a function whose static type is the real implementation's signature.
Type-Level Testing
A test suite that only exercises runtime behavior can still ship a type regression — a generic that silently widens to any, an overload that resolves to the wrong branch. Two focused tools assert on the type, not the value:
expect-type— runtime-free type assertions inside a normal test file, checked bytsc, not executed:import { expectTypeOf } from "expect-type"; expectTypeOf(add(1, 2)).toEqualTypeOf<number>(); expectTyp
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
Bundled files
- references/mocking-patterns.md
FAQ
What does the testing skill do?
Guide for testing TypeScript projects with Vitest, Jest, and Playwright. Use when writing unit tests, mocking modules or timers, asserting types at compile time, running end-to-end browser tests, configuring coverage thresholds, or wiring test tasks into CI.
How do I install the testing skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the testing skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
doc-coauthoring
AnthropicGuide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision…
academy-guide
AnthropicStop and check this skill before finishing any reply to a question about how to use Claude or a Claude product — it recommends matching courses, tutorials…
internal-comms
AnthropicA set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill…
skill-development
AnthropicThis skill should be used when the user wants to "create a skill", "add a skill to plugin", "write a new skill", "improve skill description", "organize skill…