skills/c0ntr0lledcha0s/jest-testing/SKILL.md
Automatically activated when user works with Jest tests, mentions Jest configuration, asks about Jest matchers/mocks, or has files matching *.test.js, *.test.ts, jest.config.*. Provides Jest-specific expertise for testing React, Node.js, and JavaScript applications. Also applies to Vitest due to API compatibility. Does NOT handle general quality analysis - use analyzing-test-quality for that.
npx skillsauth add aiskillstore/marketplace jest-testingInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
You are an expert in Jest testing framework with deep knowledge of its configuration, matchers, mocks, and best practices for testing JavaScript and TypeScript applications.
Claude should automatically invoke this skill when:
*.test.js, *.test.ts, *.test.jsx, *.test.tsx are encounteredUse {baseDir} to reference files in this skill directory:
{baseDir}/scripts/{baseDir}/references/{baseDir}/assets/This skill includes ready-to-use resources in {baseDir}:
describe('ComponentName', () => {
beforeEach(() => {
// Setup
});
afterEach(() => {
// Cleanup
});
describe('method or behavior', () => {
it('should do expected thing when condition', () => {
// Arrange
// Act
// Assert
});
});
});
const mockFn = jest.fn();
mockFn.mockReturnValue('value');
mockFn.mockResolvedValue('async value');
mockFn.mockImplementation((arg) => arg * 2);
jest.mock('./module', () => ({
func: jest.fn().mockReturnValue('mocked'),
}));
jest.useFakeTimers();
jest.advanceTimersByTime(1000);
jest.runAllTimers();
expect(value).toBe(expected); // Strict equality
expect(value).toEqual(expected); // Deep equality
expect(value).toBeTruthy(); // Truthy
expect(value).toContain(item); // Array/string contains
expect(fn).toHaveBeenCalledWith(args); // Function called with
expect(value).toMatchSnapshot(); // Snapshot
expect(fn).toThrow(error); // Throws
// Promises
it('async test', async () => {
await expect(asyncFn()).resolves.toBe('value');
});
// Callbacks
it('callback test', (done) => {
callbackFn((result) => {
expect(result).toBe('value');
done();
});
});
// jest.config.js
module.exports = {
testEnvironment: 'node', // or 'jsdom'
roots: ['<rootDir>/src'],
testMatch: ['**/__tests__/**/*.ts', '**/*.test.ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
},
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80,
},
},
};
// test-utils.tsx
import { render, RenderOptions } from '@testing-library/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { BrowserRouter } from 'react-router-dom';
const AllProviders = ({ children }: { children: React.ReactNode }) => {
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false } },
});
return (
<QueryClientProvider client={queryClient}>
<BrowserRouter>
{children}
</BrowserRouter>
</QueryClientProvider>
);
};
export const renderWithProviders = (
ui: React.ReactElement,
options?: RenderOptions
) => render(ui, { wrapper: AllProviders, ...options });
export * from '@testing-library/react';
// 1. Accessible queries (best)
screen.getByRole('button', { name: 'Submit' });
screen.getByLabelText('Email');
screen.getByPlaceholderText('Enter email');
screen.getByText('Welcome');
// 2. Semantic queries
screen.getByAltText('Profile picture');
screen.getByTitle('Close');
// 3. Test IDs (last resort)
screen.getByTestId('submit-button');
import userEvent from '@testing-library/user-event';
test('form submission', async () => {
const user = userEvent.setup();
render(<LoginForm />);
// Type in inputs
await user.type(screen.getByLabelText('Email'), '[email protected]');
await user.type(screen.getByLabelText('Password'), 'password123');
// Click button
await user.click(screen.getByRole('button', { name: 'Sign in' }));
// Check result
await waitFor(() => {
expect(screen.getByText('Welcome!')).toBeInTheDocument();
});
});
test('keyboard navigation', async () => {
const user = userEvent.setup();
render(<Form />);
await user.tab(); // Focus first element
await user.keyboard('{Enter}'); // Press enter
await user.keyboard('[ShiftLeft>][Tab][/ShiftLeft]'); // Shift+Tab
});
import { renderHook, act } from '@testing-library/react';
import { useCounter } from './useCounter';
test('useCounter increments', () => {
const { result } = renderHook(() => useCounter());
expect(result.current.count).toBe(0);
act(() => {
result.current.increment();
});
expect(result.current.count).toBe(1);
});
// With wrapper for context
test('hook with context', () => {
const wrapper = ({ children }) => (
<ThemeProvider theme="dark">{children}</ThemeProvider>
);
const { result } = renderHook(() => useTheme(), { wrapper });
expect(result.current.theme).toBe('dark');
});
import { waitFor, waitForElementToBeRemoved } from '@testing-library/react';
test('async loading', async () => {
render(<DataFetcher />);
// Wait for loading to disappear
await waitForElementToBeRemoved(() => screen.queryByText('Loading...'));
// Wait for content
await waitFor(() => {
expect(screen.getByText('Data loaded')).toBeInTheDocument();
});
// With timeout
await waitFor(
() => expect(screen.getByText('Slow content')).toBeInTheDocument(),
{ timeout: 5000 }
);
});
// src/mocks/handlers.ts
import { http, HttpResponse } from 'msw';
export const handlers = [
http.get('/api/users', () => {
return HttpResponse.json([
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
]);
}),
http.post('/api/users', async ({ request }) => {
const body = await request.json();
return HttpResponse.json({ id: 3, ...body }, { status: 201 });
}),
http.delete('/api/users/:id', ({ params }) => {
return HttpResponse.json({ deleted: params.id });
}),
];
// src/mocks/server.ts
import { setupServer } from 'msw/node';
import { handlers } from './handlers';
export const server = setupServer(...handlers);
// jest.setup.ts
import { server } from './src/mocks/server';
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }));
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
import { server } from '../mocks/server';
import { http, HttpResponse } from 'msw';
test('handles error response', async () => {
// Override for this test only
server.use(
http.get('/api/users', () => {
return HttpResponse.json(
{ error: 'Server error' },
{ status: 500 }
);
})
);
render(<UserList />);
await waitFor(() => {
expect(screen.getByText('Failed to load users')).toBeInTheDocument();
});
});
test('handles network error', async () => {
server.use(
http.get('/api/users', () => {
return HttpResponse.error();
})
);
render(<UserList />);
await waitFor(() => {
expect(screen.getByText('Network error')).toBeInTheDocument();
});
});
test('sends correct request', async () => {
let capturedRequest: Request | null = null;
server.use(
http.post('/api/users', async ({ request }) => {
capturedRequest = request.clone();
return HttpResponse.json({ id: 1 });
})
);
render(<CreateUserForm />);
await userEvent.type(screen.getByLabelText('Name'), 'John');
await userEvent.click(screen.getByRole('button', { name: 'Create' }));
await waitFor(() => {
expect(capturedRequest).not.toBeNull();
});
const body = await capturedRequest!.json();
expect(body).toEqual({ name: 'John' });
});
// jest.setup.ts
expect.extend({
toBeWithinRange(received: number, floor: number, ceiling: number) {
const pass = received >= floor && received <= ceiling;
return {
pass,
message: () =>
pass
? `expected ${received} not to be within range ${floor} - ${ceiling}`
: `expected ${received} to be within range ${floor} - ${ceiling}`,
};
},
toHaveBeenCalledOnceWith(received: jest.Mock, ...args: unknown[]) {
const pass =
received.mock.calls.length === 1 &&
JSON.stringify(received.mock.calls[0]) === JSON.stringify(args);
return {
pass,
message: () =>
pass
? `expected not to be called once with ${args}`
: `expected to be called once with ${args}, but was called ${received.mock.calls.length} times`,
};
},
});
// Type declarations
declare global {
namespace jest {
interface Matchers<R> {
toBeWithinRange(floor: number, ceiling: number): R;
toHaveBeenCalledOnceWith(...args: unknown[]): R;
}
}
}
test('asymmetric matchers', () => {
const data = {
id: 123,
name: 'Test',
createdAt: new Date().toISOString(),
};
expect(data).toEqual({
id: expect.any(Number),
name: expect.stringContaining('Test'),
createdAt: expect.stringMatching(/^\d{4}-\d{2}-\d{2}/),
});
expect(['a', 'b', 'c']).toEqual(
expect.arrayContaining(['a', 'c'])
);
expect({ a: 1, b: 2, c: 3 }).toEqual(
expect.objectContaining({ a: 1, b: 2 })
);
});
import { screen } from '@testing-library/react';
test('debugging', () => {
render(<MyComponent />);
// Print DOM
screen.debug();
// Print specific element
screen.debug(screen.getByRole('button'));
// Get readable DOM
console.log(prettyDOM(container));
});
# Run with verbose timing
jest --verbose
# Detect open handles
jest --detectOpenHandles
# Run tests serially to find interactions
jest --runInBand
// Check what's in the DOM
test('debug queries', () => {
render(<MyComponent />);
// Log all available roles
screen.getByRole(''); // Will error with available roles
// Check accessible name
screen.logTestingPlaygroundURL(); // Opens playground
});
// Debug async issues
test('async debug', async () => {
render(<AsyncComponent />);
// Use findBy for async elements
const element = await screen.findByText('Loaded');
// Log state at each step
screen.debug();
});
# .github/workflows/test.yml
name: Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test -- --coverage --ci
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
files: ./coverage/lcov.info
// jest.config.js
module.exports = {
// ... other config
// CI-specific settings
...(process.env.CI && {
maxWorkers: 2,
ci: true,
coverageReporters: ['lcov', 'text-summary'],
}),
// Coverage thresholds
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80,
},
},
};
# In GitHub Actions
- name: Cache Jest
uses: actions/cache@v3
with:
path: |
node_modules/.cache/jest
key: jest-${{ runner.os }}-${{ hashFiles('**/jest.config.js') }}
jest.mock() for expensive modules--maxWorkersbeforeAll for expensive setupwaitFor for async state changesfindBy queries for async elementsjest.resetModules() between testsjest.doMock() for dynamic mocksafterEachjest.useFakeTimers()--detectLeaks flagWhen testing React components:
When testing code that makes API calls:
The patterns in this skill require the following minimum versions:
| Package | Minimum Version | Features Used | |---------|----------------|---------------| | Jest | 29.0+ | Modern mock APIs, ESM support | | @testing-library/react | 14.0+ | renderHook in main package | | @testing-library/user-event | 14.0+ | userEvent.setup() API | | msw | 2.0+ | http, HttpResponse (v1 used rest, ctx) | | @testing-library/jest-dom | 6.0+ | Modern matchers |
MSW v1 → v2:
// v1 (deprecated)
import { rest } from 'msw';
rest.get('/api', (req, res, ctx) => res(ctx.json(data)));
// v2 (current)
import { http, HttpResponse } from 'msw';
http.get('/api', () => HttpResponse.json(data));
user-event v13 → v14:
// v13 (deprecated)
userEvent.click(button);
// v14 (current)
const user = userEvent.setup();
await user.click(button);
{baseDir} variable to reference skill resourcesdevelopment
Apple Human Interface Guidelines for content display components. Use this skill when the user asks about charts component, collection view, image view, web view, color well, image well, activity view, lockup, data visualization, content display, displaying images, rendering web content, color pickers, or presenting collections of items in Apple apps. Also use when the user says how should I display charts, what's the best way to show images, should I use a web view, how do I build a grid of items, what component shows media, or how do I present a share sheet. Cross-references: hig-foundations for color/typography/accessibility, hig-patterns for data visualization patterns, hig-components-layout for structural containers, hig-platforms for platform-specific component behavior.
tools
Automate HelpDesk tasks via Rube MCP (Composio): list tickets, manage views, use canned responses, and configure custom fields. Always search tools first for current schemas.
testing
Expert Haskell engineer specializing in advanced type systems, pure functional design, and high-reliability software. Use PROACTIVELY for type-level programming, concurrency, and architecture guidance.
tools
GraphQL gives clients exactly the data they need - no more, no less. One endpoint, typed schema, introspection. But the flexibility that makes it powerful also makes it dangerous. Without proper controls, clients can craft queries that bring down your server. This skill covers schema design, resolvers, DataLoader for N+1 prevention, federation for microservices, and client integration with Apollo/urql. Key insight: GraphQL is a contract. The schema is the API documentation. Design it carefully.