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.
Basic Tests 6
test("desc", () => { expect(1 + 2).toBe(3) })it("desc", fn)describe("module", () => { it(...) })test.skip("skip", () => {})test.only("focus", () => {})test.todo("todo")Matchers 8
expect(value).toBe(other)expect(obj).toEqual({ a: 1 })expect(arr).toHaveLength(3)expect(str).toMatch(/regex/)expect(arr).toContain(item)expect(value).toBeTruthy()expect(fn).toThrow("error")expect(value).toBeNull()Async Tests 6
test("async", async () => { await fn() })test("promise", () => fn().then(d => expect(d).toBe(1)))expect(promise).resolves.toBe(value)expect(promise).rejects.toThrow("err")test("done", done => { fn(done) })jest.useFakeTimers()Hooks 4
beforeEach(() => setup())afterEach(() => cleanup())beforeAll(() => init())afterAll(() => teardown())Mock Functions 8
const fn = jest.fn()jest.fn().mockReturnValue(42)jest.fn().mockResolvedValue(val)jest.fn().mockImplementation(n => n * 2)jest.spyOn(obj, "method")jest.mock("./module")jest.requireActual("./module")fn.mockClear() / mockReset() / mockRestore()Assertions & Call Verification 5
expect(fn).toHaveBeenCalled()expect(fn).toHaveBeenCalledWith(arg)expect(fn).toHaveBeenCalledTimes(2)expect(fn).toHaveBeenLastCalledWith(arg)expect(obj).toMatchObject({ a: 1 })Snapshots & CLI 7
expect(tree).toMatchSnapshot()expect(obj).toMatchInlineSnapshot()expect(value).toBeInstanceOf(Class)npx jest --coveragenpx jest --watchnpx jest --bailnpx jest -t "case name"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