| name | testing-jest |
| description | Apply when writing unit tests with Jest: assertions, mocking, async tests, and test organization. |
When to Use
Apply when writing unit tests with Jest: assertions, mocking, async tests, and test organization.
Patterns
Pattern 1: Basic Test Structure
describe('Calculator', () => {
describe('add', () => {
it('should add two positive numbers', () => {
expect(add(2, 3)).toBe(5);
});
it('should handle negative numbers', () => {
expect(add(-1, 5)).toBe(4);
});
});
});
Pattern 2: Common Matchers
expect(value).toBe(5);
expect(obj).toEqual({ a: 1 });
expect(value).toBeNull();
expect(value).toBeDefined();
expect(value).toBeTruthy();
expect(value).toBeFalsy();
expect(value).toBeGreaterThan(3);
expect(value).toBeCloseTo(0.3, 5);
expect(str).toMatch(/pattern/);
expect(array).toContain('item');
expect(obj).toHaveProperty('key', 'value');
expect(() => fn()).toThrow('error message');
expect(() => fn()).toThrow(CustomError);
Pattern 3: Mocking Functions
const mockFn = jest.fn();
mockFn.mockReturnValue(42);
mockFn.mockResolvedValue({ data: [] });
expect(mockFn).toHaveBeenCalled();
expect(mockFn).toHaveBeenCalledWith('arg1', 'arg2');
expect(mockFn).toHaveBeenCalledTimes(2);
jest.mock('./api', () => ({
fetchUser: jest.fn().mockResolvedValue({ id: '1', name: 'Test' }),
}));
Pattern 4: Async Tests
it('should fetch data', async () => {
const data = await fetchData();
expect(data).toEqual({ id: 1 });
});
it('should resolve with data', async () => {
await expect(fetchData()).resolves.toEqual({ id: 1 });
});
it('should reject with error', async () => {
await expect(failingFn()).rejects.toThrow('Network error');
});
Pattern 5: Setup and Teardown
describe('Database tests', () => {
let db: Database;
beforeAll(async () => {
db = await createTestDatabase();
});
afterAll(async () => {
await db.close();
});
beforeEach(async () => {
await db.clear();
});
it('should insert record', async () => {
await db.insert({ id: 1 });
expect(await db.count()).toBe(1);
});
});
Pattern 6: Snapshot Testing
it('should match snapshot', () => {
const component = render(<Button>Click</Button>);
expect(component).toMatchSnapshot();
});
expect(format(date)).toMatchInlineSnapshot(`"2025-01-10"`);
Anti-Patterns
- Testing implementation - Test behavior, not internal details
- Shared mutable state - Reset in beforeEach
- No assertion - Every test needs expect()
- Over-mocking - Test real code when possible
Verification Checklist