Skip to content

Jest Cheat Sheet

This page collects the most important Jest APIs, matchers, and CLI flags from across the entire course into one quick-reference cheat sheet.

How to Use This Cheat Sheet

Each table below focuses on one area of Jest: core test structure, matchers, mocking, async testing, snapshots, coverage, and the CLI. Bookmark this page and use it as a lookup reference while writing tests, rather than re-reading full lessons for a syntax reminder.

describe('feature', () => {
  beforeEach(() => { /* setup */ });

  test('does something specific', () => {
    expect(actual).toBe(expected);
  });
});

The core shape nearly every Jest test file is built around.

Core Test Structure

test('name', fn)          // define a test (alias: it)
describe('name', fn)      // group related tests
beforeEach(fn) / afterEach(fn)
beforeAll(fn) / afterAll(fn)
test.only / test.skip / test.todo
  • Use this section as the fastest reminder of Jest's foundational building blocks.
  • Full matcher tables follow below, organized by category.
  • Mocking, async, snapshot, and coverage references are further down this page.
  • CLI flags are listed at the very end for quick command-line lookups.

Core Matchers Reference

The matchers used in the overwhelming majority of everyday Jest tests.

Matcher Checks
.toBe(x) Strict equality (primitives, references)
.toEqual(x) Deep equality for objects/arrays
.toStrictEqual(x) Deep equality including type and key presence
.toBeNull() / .toBeUndefined() / .toBeDefined() Exact null/undefined checks
.toBeTruthy() / .toBeFalsy() General truthy/falsy checks
.toBeGreaterThan(x) / .toBeLessThan(x) Numeric comparisons
.toBeCloseTo(x) Floating point comparison
.toMatch(str|regex) String contains substring/matches pattern
.toContain(x) / .toContainEqual(x) Array contains a primitive/object
.toHaveLength(n) Array/string length
.toHaveProperty(path, val?) Object has a (nested) property
.toThrow(err?) Function throws when called

Mocking Reference

Everything you need for creating and configuring mocks, spies, and module mocks.

API Purpose
jest.fn(impl?) Create a mock function
jest.spyOn(obj, 'method') Wrap an existing method
jest.mock('./module', factory?) Replace an entire module
.mockReturnValue(x) / .mockReturnValueOnce(x) Configure return value(s)
.mockImplementation(fn) / .mockImplementationOnce(fn) Configure custom behavior
.mockResolvedValue(x) / .mockRejectedValue(err) Configure async return/throw
.mockRestore() Restore a spy's original implementation
expect(mockFn).toHaveBeenCalledWith(...) Assert on call arguments
jest.clearAllMocks() / resetAllMocks() Clear call history / also clear configured behavior

Async, Snapshot, and Coverage Reference

Quick lookups for asynchronous testing, snapshots, and coverage-related commands.

Category API / Command
Async await expect(p).resolves.toBe(x) / .rejects.toThrow()
Async expect.assertions(n)
Fake timers jest.useFakeTimers() / jest.advanceTimersByTime(ms)
Snapshots expect(x).toMatchSnapshot() / toMatchInlineSnapshot()
Snapshots jest -u to update
Coverage jest --coverage
Coverage coverageThreshold in jest.config.js
CLI jest --watch / jest -t "name" / jest path/to/file

Common Mistakes

  • Treating this cheat sheet as a substitute for understanding why each API behaves the way it does.
  • Forgetting .resolves/.rejects still require await.
  • Using .toBe() where .toEqual() belongs, even with this reference open.
  • Not bookmarking this page for quick lookups during real project work.

Key Takeaways

  • This cheat sheet consolidates matchers, mocking, async, snapshot, and coverage references in one place.
  • Use it as a lookup tool, not a replacement for understanding the underlying concepts.
  • Combine it with your project's own jest.config.js for a complete quick-reference setup.
  • Revisit specific lessons whenever a cheat sheet entry needs a deeper refresher.

Pro Tip

Keep this cheat sheet page open in a browser tab while writing tests for the first few weeks — most developers stop needing it once the core matcher and mocking APIs become muscle memory.