Jest Cheatsheet - Testing Framework

Tests only earn their keep when a failure tells you precisely what broke. This reference covers describe/it grouping, matchers beyond toBe, async and promise testing, jest.fn/jest.mock module mocking, snapshot testing, and coverage output. It emphasizes the matchers and mock strategies that pinpoint broken behavior instead of vague red. For teams adding real unit coverage to JS/TS code. After reading you write focused assertions and mock the exact seams you intend to.

Languages·44 commands·Last updated 2026-07-21

Basic Tests 6

test("desc", () => { expect(1 + 2).toBe(3) })
Define a test case
it("desc", fn)
Alias of test, more natural BDD style
describe("module", () => { it(...) })
Group related tests
test.skip("skip", () => {})
Skip this test temporarily
test.only("focus", () => {})
Run only this test (debug)
test.todo("todo")
Placeholder for a pending test

Matchers 8

expect(value).toBe(other)
Strict equality (Object.is)
expect(obj).toEqual({ a: 1 })
Deep equality (recursive)
expect(arr).toHaveLength(3)
Length of array/string
expect(str).toMatch(/regex/)
Match a string by regex
expect(arr).toContain(item)
Array contains an item
expect(value).toBeTruthy()
Truthy check (toBeFalsy opposite)
expect(fn).toThrow("error")
Expect a thrown error
expect(value).toBeNull()
Null check (also toBeUndefined/toBeDefined)

Async Tests 6

test("async", async () => { await fn() })
async/await style
test("promise", () => fn().then(d => expect(d).toBe(1)))
Return a promise for Jest to await
expect(promise).resolves.toBe(value)
Assert the resolved value
expect(promise).rejects.toThrow("err")
Assert the rejection reason
test("done", done => { fn(done) })
done callback style (manual)
jest.useFakeTimers()
Fake timers (with runAllTimers)

Hooks 4

beforeEach(() => setup())
Before each test
afterEach(() => cleanup())
After each test
beforeAll(() => init())
Once before all tests
afterAll(() => teardown())
Once after all tests

Mock Functions 8

const fn = jest.fn()
Create a mock function
jest.fn().mockReturnValue(42)
Set a fixed return value
jest.fn().mockResolvedValue(val)
Return a resolved promise
jest.fn().mockImplementation(n => n * 2)
Custom implementation
jest.spyOn(obj, "method")
Spy on a method (keeps impl)
jest.mock("./module")
Auto-mock a whole module
jest.requireActual("./module")
Require the real module in a mock
fn.mockClear() / mockReset() / mockRestore()
Clear/reset/restore a mock

Assertions & Call Verification 5

expect(fn).toHaveBeenCalled()
Verify a function was called
expect(fn).toHaveBeenCalledWith(arg)
Verify call arguments
expect(fn).toHaveBeenCalledTimes(2)
Verify call count
expect(fn).toHaveBeenLastCalledWith(arg)
Verify the last call args
expect(obj).toMatchObject({ a: 1 })
Partial object match

Snapshots & CLI 7

expect(tree).toMatchSnapshot()
Create/compare snapshot (-u to update)
expect(obj).toMatchInlineSnapshot()
Inline snapshot (written to file)
expect(value).toBeInstanceOf(Class)
Instance type assertion
npx jest --coverage
Generate a coverage report
npx jest --watch
Watch mode (rerun related; --watchAll for all)
npx jest --bail
Stop at first failure (--bail)
npx jest -t "case name"
Filter by test name regex (-t)

Tips

  • jest --coverage reports coverage; --watch enables watch mode.
  • jest.mock() auto-mocks all exports, returning jest.fn().
  • toMatchSnapshot suits UI/serialized-structure tests; update with -u.
  • jest.spyOn spies on a method without fully mocking it.
  • useFakeTimers with runAllTimers tests timer logic synchronously.
  • Remove test.only after debugging, or other tests get skipped.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us