Code Reference

Glossary

Jest · Reference cheat sheet

Glossary

Jest · Reference cheat sheet


📋 Overview

Alphabetical glossary of core Jest terms for matchers, mocks, lifecycle hooks, snapshots, and test configuration.

🔧 Core concepts

TermDefinition
AfterAllA hook that runs once after all tests in a file or describe finish.
AfterEachA hook that runs after every test in the current scope.
BeforeAllA hook that runs once before any tests in a file or describe start.
BeforeEachA hook that runs before every test in the current scope.
CoverageA report of which statements/branches tests executed.
DescribeA block that groups related tests under a shared name.
EachA helper (test.each / describe.each) that table-drives repeated cases.
ExpectThe assertion entry point that chains matchers onto a value.
Fake timersMocked clock APIs that let tests advance time manually.
Fnjest.fn(), the factory for mock functions.
HoistingJest’s behavior of lifting jest.mock calls to the top of a module.
It / testA single test case function registered with Jest.
MatcherAn assertion method such as toBe, toEqual, or toHaveBeenCalled.
MockA stand-in function or module that records calls and controls returns.
Module mockReplacing an imported module with a mock via jest.mock.
Only / skipFocusing (.only) or excluding (.skip) specific suites or tests.
Setup fileA file run before the test framework is installed in each environment.
SnapshotA serialized output stored on disk and compared on later runs.
SpyA wrapper around a real function that records calls while optionally calling through.
SuiteA group of tests, typically created by a describe block.
Test environmentThe runtime context such as node or jsdom for each test file.
Test runnerThe component that discovers files and executes suites.
TimeoutThe maximum duration a test may run before Jest fails it.
TransformerA preprocessor (often Babel/ts-jest) that compiles files before tests.
Watch modeA continuous mode that re-runs affected tests on file changes.

💡 Examples

Describe, test, and matchers:

describe("sum", () => {
  test("adds numbers", () => {
    expect(1 + 2).toBe(3);
    expect({ a: 1 }).toEqual({ a: 1 });
  });
});

Mocks and spies:

const fetchUser = jest.fn().mockResolvedValue({ id: 1 });
const log = jest.spyOn(console, "log").mockImplementation(() => {});

await fetchUser(1);
expect(fetchUser).toHaveBeenCalledWith(1);
log.mockRestore();

Lifecycle hooks:

beforeEach(() => {
  jest.clearAllMocks();
});

⚠️ Pitfalls

  • Confusing toBe (reference/primitive identity) with toEqual (deep value equality).
  • Mixing mock (full replacement) with spy (wrap existing implementation).
  • Treating snapshot failures as always product bugs — snapshots need intentional updates.
  • Equating fake timers with real delays — you must advance timers explicitly.
  • Assuming clearAllMocks resets implementations the same way resetAllMocks does.

On this page