jest-testing
Skill2 starsChanged 4 months agoMITView on GitHub
Jest testing framework patterns for JavaScript and TypeScript projects, covering test structure, mocking strategies, async testing, snapshot testing, React component testing with Testing Library, and practical test organization. Use when the task involves `Jest`, `JavaScript testing`, `React testing`, `jest.config`, or `unit testing JS`.
In this repository43 other files it gives its agents.
- accessibilitySkill
- best-practicesSkill
- brainstormingSkill
- core-web-vitalsSkill
- cypress-e2eSkill
- docker-expertSkill
- drizzle-ormSkill
- elixir-developmentSkill
- eslint-configSkill
- express-apiSkill
- frontend-designSkill
- github-actionsSkill
- go-developmentSkill
- grafana-dashboardsSkill
- grpc-apiSkill
- helm-chartsSkill
- kubernetes-orchestrationSkill
- makefileSkill
- markdown-a11ySkill
- nothing-designSkill
- performanceSkill
- php-developmentSkill
- pinned-tagSkill
- postgresql-developmentSkill
- pr-creatorSkill
- prettier-formattingSkill
- pydanticSkill
- qwik-developmentSkill
- rails-developmentSkill
- ruby-developmentSkill
- rust-async-patternsSkill
- seoSkill
- skill-creatorSkill
- solidjs-developmentSkill
- sqlalchemySkill
- sql-optimization-patternsSkill
- swift-ios-developmentSkill
- tensorflow-mlSkill
- testing-librarySkill
- trpc-type-safetySkill
- webapp-testingSkill
- web-quality-auditSkill
- zig-developmentSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
14 rules. About 0 are common, 5 are distinctive, and 9 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈5
Distinctive · estimate
This author's own choices. Worth a look.
9
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 24
Arrange-Act-Assert: Every test follows three clear phases — set up state, perform the action, verify the outcome.
- Distinctive: line 32
Deterministic Tests: No reliance on real time, network, or random values.
- Very specific: line 257
Use describe blocks to group related tests by function or feature.
- Very specific: line 259
Use beforeEach for setup that every test in a describe needs — never rely on test execution order.
- Very specific: line 261
Run tests in watch mode during development: jest --watch.
- Very specific: line 262
Use jest.requireActual() when partially mocking a module to preserve unmocked exports.
- Very specific: line 263
Run --detectOpenHandles to debug tests that won't exit.
- Very specific: line 267
DON'T use toBeTruthy() when you can be specific — use toBe(true), toEqual(value), or toBeNull().
- Distinctive: line 269
DON'T mock the module under test — only mock its dependencies.
- Distinctive: line 270
DON'T write tests that depend on execution order or shared mutable state.
- Distinctive: line 271
DON'T snapshot large component trees — snapshots become noise that nobody reviews.
- Very specific: line 272
DON'T use test.only or describe.only in committed code — they silently skip other tests.
- Very specific: line 273
DON'T suppress errors with try/catch inside tests — let Jest catch and report them with rejects.toThrow.
- Very specific: line 275
DON'T use arbitrary setTimeout or sleep for async tests — use waitFor, fake timers, or resolved promises.
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
When to Use
- Writing or refactoring unit and integration tests with Jest.
- Setting up mocking strategies for modules, functions, or APIs.
- Testing async code (Promises, async/await, timers).
- Testing React components alongside
@testing-library/react. - Configuring Jest for TypeScript, coverage, or CI environments.
- Debugging flaky or slow tests.
Critical Patterns
- Arrange-Act-Assert: Every test follows three clear phases — set up state, perform the action, verify the outcome. No mixing.
- Test Behavior, Not Implementation: Assert on observable outcomes (return values, DOM changes, thrown errors), not internal function calls.
- Isolate with Mocking: Mock external dependencies (APIs, databases, file system) but NOT the unit under test. Over-mocking hides real bugs.
- One Assertion Focus Per Test: Each
itblock should test one logical behavior. Multiple assertions are fine if they verify the same behavior. - Deterministic Tests: No reliance on real time, network, or random values. Use fake timers, mocked fetch, and seeded data.
- Clean Up After Yourself: Reset mocks in
afterEach. Avoid shared mutable state between tests.
Code Examples
Basic Test Structure
describe("UserService", () => {
let service: UserService;
beforeEach(() => {
service = new UserService();
});
afterEach(() => {
jest.restoreAllMocks();
});
describe("findById", () => {
it("returns user when found", async () => {
// Arrange
const mockUser = { id: "1", name: "Alice" };
jest.spyOn(db, "query").mockResolvedValue(mockUser);
// Act
const result = await service.findById("1");
// Assert
expect(result).toEqual(mockUser);
});
it("throws NotFoundError when user does not exist", async () => {
jest.spyOn(db, "query").mockResolvedValue(null);
await expect(service.findById("999")).rejects.toThrow(NotFoundError);
});
});
});Common Matchers
// Equality
expect(value).toBe(primitive); // strict equality (===)
expect(value).toEqual(object); // deep equality
expect(value).toStrictEqual(object); // deep equality + type checking
// Truthiness
expect(value).toBeTruthy();
expect(value).toBeFalsy();
expect(value).toBeNull();
expect(value).toBeUndefined();
expect(value).toBeDefined();
// Numbers
expect(value).toBeGreaterThan(3);
expect(value).toBeCloseTo(0.3, 5); // floating point
// Strings
expect(value).toMatch(/regex/);
expect(value).toContain("substring");
// Arrays / Iterables
expect(array).toContain(item);
expect(array).toHaveLength(3);
expect(array).toEqual(expect.arrayContaining([1, 2]));
// Objects
expect(obj).toHaveProperty("key", "value");
expect(obj).toMatchObject({ name: "Alice" }); // partial match
// Exceptions
expect(() => fn()).toThrow(ErrorType);
expect(() => fn()).toThrow("message");Mocking Strategies
// 1. jest.fn() — create a standalone mock function
const mockCallback = jest.fn();
mockCallback.mockReturnValue(42);
mockCallback.mockResolvedValue({ data: "ok" }); // async
expect(mockCallback).toHaveBeenCalledTimes(1);
expect(mockCallback).toHaveBeenCalledWith("arg1", "arg2");
// 2. jest.spyOn() — spy on existing methods (preserves original unless mocked)
const spy = jest.spyOn(Math, "random").mockReturnValue(0.5);
// ... test code ...
spy.mockRestore(); // restore original
// 3. jest.mock() — mock entire modules
jest.mock("../services/emailService", () => ({
sendEmail: jest.fn().mockResolvedValue({ sent: true }),
}));
// 4. Manual mocks — __mocks__ directory
// __mocks__/axios.ts
export default {
get: jest.fn().mockResolvedValue({ data: {} }),
post: jest.fn().mockResolvedValue({ data: {} }),
};Async Testing
// async/await — preferred
it("fetches user data", async () => {
const data = await fetchUser("1");
expect(data.name).toBe("Alice");
});
// Rejections
it("rejects with an error", async () => {
await expect(fetchUser("bad")).rejects.toThrow("Not found");
});
// Fake timers
it("debounces the search", () => {
jest.useFakeTimers();
const callback = jest.fn();
const debounced = debounce(callback, 300);
debounced("query");
expect(callback).not.toHaveBeenCalled();
jest.advanceTimersByTime(300);
expect(callback).toHaveBeenCalledWith("query");
jest.useRealTimers();
});Testing React Components
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { LoginForm } from "./LoginForm";
describe("LoginForm", () => {
it("submits valid credentials", async () => {
const user = userEvent.setup();
const onSubmit = jest.fn();
render(<LoginForm onSubmit={onSubmit} />);
await user.type(screen.getByLabelText(/email/i), "alice@example.com");
await user.type(screen.getByLabelText(/password/i), "secret123");
await user.click(screen.getByRole("button", { name: /sign in/i }));
expect(onSubmit).toHaveBeenCalledWith({
email: "alice@example.com",
password: "secret123",
});
});
it("shows validation error for empty email", async () => {
const user = userEvent.setup();
render(<LoginForm onSubmit={jest.fn()} />);
await user.click(screen.getByRole("button", { name: /sign in/i }));
expect(screen.getByText(/email is required/i)).toBeInTheDocument();
});
});Snapshot Testing
// Use sparingly — only for stable, serializable output
it("renders correctly", () => {
const { container } = render(<Badge variant="success">Active</Badge>);
expect(container.firstChild).toMatchSnapshot();
});
// Inline snapshots — better for small outputs
it("formats the date", () => {
expect(formatDate("2024-01-15")).toMatchInlineSnapshot(`"January 15, 2024"`);
});Jest Configuration
// jest.config.ts
import type { Config } from "jest";
const config: Config = {
preset: "ts-jest",
testEnvironment: "jsdom", // "node" for backend
roots: ["<rootDir>/src"],
testMatch: ["**/__tests__/**/*.test.ts(x)?", "**/*.spec.ts(x)?"],
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/$1",
},
setupFilesAfterSetup: ["<rootDir>/jest.setup.ts"],
collectCoverageFrom: [
"src/**/*.{ts,tsx}",
"!src/**/*.d.ts",
"!src/**/index.ts", // barrel exports
],
coverageThresholds: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80,
},
},
};
export default config;Best Practices
DO
- Use
describeblocks to group related tests by function or feature. - Name tests as complete sentences:
it("returns null when user is not found"). - Use
beforeEachfor setup that every test in adescribeneeds — never rely on test execution order. - Run tests in watch mode during development:
jest --watch. - Use
jest.requireActual()when partially mocking a module to preserve unmocked exports. - Run
--detectOpenHandlesto debug tests that won't exit.
DON'T
- DON'T use
toBeTruthy()when you can be specific — usetoBe(true),toEqual(value), ortoBeNull(). - DON'T mock the module under test — only mock its dependencies.
- DON'T write tests that depend on execution order or shared mutable state.
- DON'T snapshot large component trees — snapshots become noise that nobody reviews.
- DON'T use
test.onlyordescribe.onlyin committed code — they silently skip other tests. - DON'T suppress errors with
try/catchinside tests — let Jest catch and report them withrejects.toThrow. - DON'T use arbitrary
setTimeoutorsleepfor async tests — usewaitFor, fake timers, or resolved promises.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

