Skip to content
Tutorials Center
My learning

Topics

Jest Tutorial

Learn Jest step by step: 30 lessons in 11 chapters with examples, quizzes, diagrams and projects.

Start learning →

of lessons completed

Getting Started

  1. 1 What Is Jest? Learn what Jest does, how a test runner, assertions and mocks fit together, how Jest compares with Vitest and node:test, and what happens when a test file runs.
  2. 2 Your First Jest Test Write your first Jest test: create a function, add a .test.js file, use test() and expect().toBe(), run npm test and learn to read a passing and failing report.
  3. 3 describe, it and Test Organization Group Jest tests with describe, choose between it and test, use skip, only and todo, name tests so failures read clearly, and structure each test as arrange-act-assert.
  4. 4 The Jest CLI and Watch Mode Learn the Jest command line: run one file, filter tests with -t, use watch mode, collect coverage, run in band, stop early with --bail and read exit codes in CI.

Matchers

  1. 5 Common Matchers Learn the everyday Jest matchers: toBe versus toEqual versus toStrictEqual, truthiness matchers like toBeNull and toBeDefined, and how not flips any check.
  2. 6 Numbers and Strings Use Jest's number and string matchers: toBeGreaterThan, toBeCloseTo for floating point, toMatch with regex, toContain and toHaveLength, with runnable stand-ins.
  3. 7 Arrays and Objects Test arrays and objects in Jest with toContainEqual, toHaveProperty, toMatchObject, objectContaining, arrayContaining and expect.any, with a runnable stand-in.
  4. 8 Errors and Custom Matchers Test thrown errors with Jest toThrow, wrap calls correctly, and write your own matchers with expect.extend and clear failure messages. Runnable stand-in.

Asynchronous Code

  1. 9 Testing Promises and Async/Await Test promises and async functions in Jest: return or await them, use resolves and rejects, avoid the forgotten-await trap, and count assertions with expect.assertions.
  2. 10 Callbacks and the done Callback Test callback-based code in Jest with the done argument, handle timeouts and errors, turn callbacks into promises with promisify, and learn why done is a legacy tool.

Hooks and Isolation

  1. 11 Setup and Teardown Hooks Learn Jest setup and teardown: beforeEach, afterEach, beforeAll and afterAll, the order hooks run in across nested describe blocks, async hooks and common mistakes.
  2. 12 Test Isolation and Shared State Why Jest tests pass alone but fail together: leaked state, module registries, order-dependent tests, test data factories and the usual causes of flaky tests.

Mocking

  1. 13 Mock Functions with jest.fn Learn Jest mock functions: jest.fn, mock.calls and mock.results, mockReturnValue, mockImplementation, toHaveBeenCalledWith, plus a runnable stand-in built on node:test.
  2. 14 Spying with jest.spyOn Use jest.spyOn to watch or replace methods on real objects: clear vs reset vs restore, restoreMocks config, spying on console and Math.random, with runnable stand-ins.
  3. 15 Mocking Modules Replace whole modules in Jest with jest.mock: automock vs factory, hoisting, partial mocks with requireActual, ESM caveats, and dependency injection as a simpler alternative.
  4. 16 Mocking Timers, Dates and HTTP Test time and network code in Jest: useFakeTimers, advanceTimersByTime, setSystemTime, async timer helpers and mocking fetch, with runnable node:test stand-ins.

Snapshots and Tables

  1. 17 Snapshot Testing Learn Jest snapshot testing: toMatchSnapshot, toMatchInlineSnapshot, updating with -u, property matchers, and when snapshots help or hurt, with a runnable snapshot-store stand-in.
  2. 18 Table-Driven Tests with test.each Run one Jest test over many inputs with test.each and describe.each: array tables, tagged template tables, printf title formatting, edge-case tables and a runnable stand-in.

Jest in Real Projects

  1. 19 Configuring Jest Configure Jest with jest.config.js: testEnvironment, testMatch, setup files, moduleNameMapper, transform and projects, plus a runnable stand-in config loader.
  2. 20 Coverage and Test Quality Measure code coverage with Jest: read the report, set coverageThreshold, understand line vs branch coverage and why coverage is not quality, with a coverage counter stand-in.

Testing Real Applications

  1. 21 Testing UI with jsdom and Testing Library Test user interfaces in Jest with jsdom and Testing Library: query by role and accessible name, simulate user events, avoid brittle selectors, with browser stand-ins.
  2. 22 Testing APIs and Databases Test HTTP APIs and database code with Jest: the Supertest idea, test data, in-memory databases, isolation between tests, with runnable node:sqlite and loopback server stand-ins.

Tooling and CI

  1. 23 TypeScript, Babel and CI Run Jest on TypeScript with ts-jest, Babel or SWC, understand ESM mode, and set up Jest in GitHub Actions with caching and sharding, plus runnable transform and shard stand-ins.

Projects

  1. 24 Project: Test a Shopping Cart Build and test a shopping cart module test-first with Jest: red-green-refactor, edge cases, a mocked price API and clear failure messages, with runnable stand-ins.
  2. 25 Project: Test an API Client Build and test a small fetch-based API client: success paths, HTTP errors, network failures, retries with backoff and fake timers, with runnable stand-ins.
  3. 26 Project: Test an Event System Test a small event emitter with Jest: spies on listeners, once and off, call order, errors inside listeners and async ordering, with runnable stand-ins.
  4. 27 Project: Test a Form Validator Test a sign-up form validator with Jest: table-driven rules with test.each, a custom matcher for field errors and a snapshot of error messages, with stand-ins.

Practice and Review

  1. 28 Jest Exercises with Solutions Ten Jest exercises with full solutions covering matchers, errors, async code, mocks, fake timers and table tests, plus runnable stand-ins to check your answers.
  2. 29 Jest Interview Questions Prepare for Jest interviews: toBe vs toEqual, mocks vs spies, clear vs reset vs restore, fake timers, snapshots, async pitfalls and test strategy, with runnable stand-ins.
  3. 30 Jest Cheat Sheet A one-page Jest cheat sheet: structure, matchers, async patterns, mocks and spies, fake timers, hooks, CLI flags and config options, with runnable stand-ins.