Skip to content
Latchkey

Jest Cheat Sheet: Matchers, Mocks & CLI Flags

Jest matchers, mocks, and CLI flags in one quick reference.

Assert, mock, and run JavaScript tests with Jest.

CLI flags

FlagDoes
jest -t "name"Run tests matching name
jest path.test.jsRun one file
jest --watchWatch changed files
jest -uUpdate snapshots
jest --coverageCollect coverage
jest --ciCI mode (no snapshot writes)
jest --runInBandSerial (debugging)

Matchers

MatcherAsserts
toBe(x)Strict equality
toEqual(x)Deep equality
toContain(x)Array/string contains
toThrow()Function throws
toHaveBeenCalledWith(a)Mock called with args
resolves / rejectsAsync promise outcome

Mocks & hooks

example.test.js
const fn = jest.fn().mockReturnValue(1);
jest.mock('./api');
beforeEach(() => jest.clearAllMocks());

test('async', async () => {
  await expect(fetchUser()).resolves.toEqual({ id: 1 });
});

Key takeaways

  • toBe is reference/primitive equality; toEqual deep-compares.
  • --ci fails instead of writing new snapshots.
  • clearAllMocks in beforeEach keeps tests isolated.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →