Skip to content
Skillpedia
Testing48k · #3374

jest-skill

Generates unit and integration tests in JavaScript or TypeScript using Jest. Useful for checking how code parts and components work.

What it does

This tool helps you create automatic tests for your JavaScript or TypeScript code using the Jest library. It can generate basic tests, simulate parts of your code (mocking), test asynchronous functions, and even React components.

When to use it

  • ✓When you need to verify that your code works as expected.
  • ✓To ensure new changes don't break existing functionality.
  • ✓When creating tests to simulate external dependencies or complex functions.

Example of what to ask your AI

“Generate a test for the 'add' function that takes two numbers and returns their sum.”

Want more? Open the sections below.

How to use

The easiest way: copy the message below and paste it into Claude, ChatGPT, Cursor or any AI. It reads the skill and follows the instructions.

Skill link

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/jest-skill/SKILL.md

Ready-to-paste message

Use this skill to help me: https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/jest-skill/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
How it works

The tool uses the Jest library to write tests. It creates code blocks called 'describe' and 'it' to organize tests and uses 'expect' to check if the results are as expected.

Installation

Easiest way: copy the skill link and paste it into Claude, ChatGPT or Cursor asking it to follow the instructions.

In the terminal (Claude Code, Codex, Cursor), in your project folder:

npx skills add sickn33/agentic-awesome-skills --skill jest-skill

Or download just the file:

curl -L https://raw.githubusercontent.com/sickn33/agentic-awesome-skills/main/plugins/agentic-awesome-skills-claude/skills/jest-skill/SKILL.md -o SKILL.md
Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

Jest Testing Skill

When to Use

Use this skill when you need generates Jest unit and integration tests in JavaScript or TypeScript. Covers mocking, snapshots, async testing, and React component testing. Use when user mentions "Jest", "describe/it/expect", "jest.mock", "toMatchSnapshot". Triggers on: "Jest", "expect().toBe()", "jest.mock",...

Core Patterns

Basic Test

describe('Calculator', () => {
  let calc;
  beforeEach(() => { calc = new Calculator(); });

  test('adds two numbers', () => {
    expect(calc.add(2, 3)).toBe(5);
  });

  test('throws on division by zero', () => {
    expect(() => calc.divide(10, 0)).toThrow('Division by zero');
  });
});

Matchers

expect(value).toBe(exact);                 // === strict
expect(value).toEqual(object);             // deep equality
expect(value).toBeTruthy();
expect(value).toBeNull();
expect(value).toBeGreaterThan(3);
expect(value).toBeCloseTo(0.3, 5);
expect(str).toMatch(/regex/);
expect(arr).toContain(item);
expect(arr).toHaveLength(3);
expect(obj).toHaveProperty('name');
expect(obj).toMatchObject({ name: 'Alice' });
expect(() => fn()).toThrow(CustomError);

Mocking

// Mock function
const mockFn = jest.fn();
mockFn.mockReturnValue(42);
mockFn.mockResolvedValue({ data: 'test' });
expect(mockFn).toHaveBeenCalledWith('arg1');
expect(mockFn).toHaveBeenCalledTimes(1);

// Mock module
jest.mock('./database');
const db = require('./database');
db.getUser.mockResolvedValue({ name: 'Alice' });

// Mock with implementation
jest.mock('./api', () => ({
  fetchUsers: jest.fn().mockResolvedValue([{ name: 'Alice' }]),
}));

// Spy
const spy = jest.spyOn(console, 'log').mockImplementation();
expect(spy).toHaveBeenCalledWith('expected');
spy.mockRestore();

// Fake timers
jest.useFakeTimers();
jest.advanceTimersByTime(1000);
jest.useRealTimers();

Async Testing

test('fetches users', async () => {
  const users = await fetchUsers();
  expect(users).toHaveLength(3);
});

test('resolves with data', () => {
  return expect(fetchData()).resolves.toEqual({ data: 'value' });
});

test('rejects with error', () => {
  return expect(fetchBadData()).rejects.toThrow('not found');
});

React Component Testing (Testing Library)

import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom';
import LoginForm from './LoginForm';

test('submits login form', async () => {
  const onSubmit = jest.fn();
  render(<LoginForm onSubmit={onSubmit} />);

  fireEvent.change(screen.getByLabelText('Email'), {
    target: { value: 'user@test.com' },
  });
  fireEvent.change(screen.getByLabelText('Password'), {
    target: { value: 'password123' },
  });
  fireEvent.click(screen.getByRole('button', { name: /login/i }));

  await waitFor(() => {
    expect(onSubmit).toHaveBeenCalledWith({
      email: 'user@test.com', password: 'password123',
    });
  });
});

Snapshot Testing

test('renders correctly', () => {
  const tree = renderer.create(<Button label="Click" />).toJSON();
  expect(tree).toMatchSnapshot();
});
// Update: jest --updateSnapshot

Anti-Patterns

Bad Good Why
expect(x === y).toBe(true) expect(x).toBe(y) Better errors
No await on async Always await Swallows failures
Snapshot everything Snapshot UI, assert logic Snapshot fatigue

Quick Reference

Task Command
Run all npx jest
Watch npx jest --watch
Coverage npx jest --coverage
Update snapshots npx jest --updateSnapshot
Run file npx jest tests/calc.test.js
Single test test.only('name', () => {})

Deep Patterns

For production-grade patterns, see reference/playbook.md:

Section What's Inside
§1 Production Config Node + React configs, path aliases, coverage thresholds
§2 Mocking Deep Dive Module/partial/manual mocks, spies, timers, env vars
§3 Async Patterns Promises, rejections, event emitters, streams
§4 test.each Array, tagged template, describe.each for table-driven tests
§5 Custom Matchers toBeWithinRange, toBeValidEmail, TypeScript declarations
§6 React Testing Library userEvent, hooks, context providers
§7 Snapshot Testing Component, inline, property matchers
§8 API Service Testing Mocked axios, CRUD patterns, error handling

…

FAQ

What is 'mocking'?

Mocking is like creating a 'substitute' for a part of your code (like a function or a module) so you can test another part in isolation, without relying on the substitute.

What are 'snapshots'?

Snapshots are like 'photos' of your code's expected output. If the output changes unexpectedly, the test fails, helping to identify bugs.

How do I test code that uses 'setTimeout' or 'fetch'?

Jest has special tools to handle asynchronous code, like 'async/await' and 'jest.useFakeTimers()', so you can test these operations reliably.

Details

Original description by the author

Generates Jest unit and integration tests in JavaScript or TypeScript. Covers mocking, snapshots, async testing, and React component testing. Use when user mentions \"Jest\", \"describe/it/expect\", \"jest.mock\", \"toMatchSnapshot\".

repo: sickn33/agentic-awesome-skills

Popularity

GitHub stars
48k
Ranking position
#3374
License
MIT
Updated
10/15/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills