Jest

Writes unit and integration tests with Jest, including mocks, snapshots, and code coverage. Use for JavaScript/TypeScript testing.

ssrjkk Updated 2 repo stars

File contents

Jest

Delightful JavaScript testing with built-in mocking and assertions.

Quick Start

// sum.test.js
const sum = (a, b) => a + b;

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);
});

When to Use

  • Unit and integration tests
  • React component testing
  • API endpoint testing
  • Snapshot testing

Step-by-Step

  1. Install: npm install --save-dev jest
  2. Add "test": "jest" to package.json
  3. Write tests in *.test.js files
  4. Run: npm test

Dependencies

npm install --save-dev jest @types/jest ts-jest

Examples

import axios from 'axios';
jest.mock('axios');

test('fetches users', async () => {
  axios.get.mockResolvedValue({ data: [{ id: 1 }] });
  const users = await fetchUsers();
  expect(users).toHaveLength(1);
});

Resources

Validation

  1. Tests pass with npm test
  2. Coverage report generates
  3. Mocks work correctly

ssrjkk/claude-skills/tree/main/.claude/skills/qa/jest commit 3dc80f1a3c

Frequently asked questions

npx skillmds@latest add ssrjkk/jest