jest
FreeNo executable scriptsNot checkedJest testing best practices for JavaScript and TypeScript applications, covering test structure, mocking, and assertion patterns.
by Mindrally★ 323 GitHub starsAbout this skill
Jest Testing Best Practices
You are an expert in JavaScript, TypeScript, and Jest testing.
Core Principles
Test Structure
- Use descriptive test names that clearly explain expected behavior
- Organize tests using
describeblocks for logical grouping - Follow the Arrange-Act-Assert (AAA) pattern in each test
- Keep tests focused on a single behavior or outcome
Setup and Teardown
- Use
beforeEachandafterEachfor test isolation - Use
beforeAllandafterAllfor expensive setup that can be shared - Clean up any side effects in teardown hooks
Mocking
- Use
jest.mock()for module mocking - Use
jest.fn()for function mocks - Use
jest.spyOn()when you need to track calls but keep implementation - Clear mocks between tests with
jest.clearAllMocks() - Avoid over-mocking: test real behavior when feasible
Assertions
- Use the most specific matcher available
- Prefer
toEqualfor object comparison,toBefor primitives - Use
toMatchSnapshotsparingly and with meaningful names - Include negative test cases (what should NOT happen)
Async Testing
- Always return promises or use async/await in async tests
- Use
waitForfrom testing libraries for async assertions - Set appropriate timeouts for long-running tests
Coverage
- Aim for meaningful coverage, not just high percentages
- Test edge cases and error conditions
- Use
--coverageflag to track coverage metrics
Best Practices
- Keep tests DRY but readable (prefer clarity over brevity)
- Test behavior, not implementation details
- Make tests deterministic (no random data without seeding)
- Use factories or builders for test data creation
Example Patterns
describe('fetchUser', () => {
it('should return user data when API call succeeds', async () => {
const mockUser = { id: 1, name: 'John' };
jest.spyOn(api, 'get').mockResolvedValue(mockUser);
const result = await fetchUser(1);
expect(result).toEqual(mockUser);
expect(api.get).toHaveBeenCalledWith('/users/1');
});
it('should throw error when API call fails', async () => {
jest.spyOn(api, 'get').mockRejectedValue(new Error('Not found'));
await expect(fetchUser(999)).rejects.toThrow('Not found');
});
});
Install jest 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
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
FAQ
What does the jest skill do?
Jest testing best practices for JavaScript and TypeScript applications, covering test structure, mocking, and assertion patterns.
How do I install the jest 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 jest skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
mcp-builder
Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when…
by Anthropicclaude-api
Reference for the Claude API / Anthropic SDK — model ids, pricing, params, streaming, tool use, MCP, agents, caching, token counting, model migration. TRIGGER…
by Anthropicalgorithmic-art
Creating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code…
by Anthropicmcp-integration
This skill should be used when the user asks to "add MCP server", "integrate MCP", "configure MCP in plugin", "use .mcp.json", "set up Model Context…
by Anthropic