skip to content

Jest

The batteries-included JavaScript test runner: matchers, mocking, snapshots, coverage, and a config that mostly works out of the box. Interviewers ask because it is the default in most React and Node codebases.

on this pageshow

explore

questions

page 2 of 2

In a Jest 30 monorepo with a jsdom React package and a node API package, how do you run both through one projects config, and what must stay at the root?

level: seniorimportance: should knowfreq 32%

basics

~20 s

projects lists paths, globs or inline config objects, and each entry becomes its own project with its own rootDir, testEnvironment, transform and moduleNameMapper. Run-wide options such as reporters, maxWorkers and coverageThreshold belong only in the root config.

open as a page

A Jest 30 CI job keeps passing tests whose snapshots were never committed, writing them afresh on every run; why does that happen, and how do you stop it?

level: seniorimportance: should knowfreq 38%

basics

~20 s

Jest only refuses to write a missing snapshot in CI mode, which it detects from environment variables such as CI; if detection fails or -u is passed, the job writes and passes. Run jest --ci explicitly and never pass -u in CI.

open as a page

A Jest 30 snapshot of a rendered config includes an absolute cache path that differs on every machine; how do you fix it with a custom snapshot serializer?

level: seniorimportance: should knowfreq 22%

basics

~10 s

Write a serializer whose test(val) picks strings containing the machine-specific root and whose serialize(...) replaces it and hands the result to printer; register it in snapshotSerializers or with expect.addSnapshotSerializer in that test file.

open as a page

A Jest 30 custom matcher registered with expect.extend works in one file, prints a wrong message under .not, and is not a function in other files. Why?

level: seniorimportance: should knowfreq 30%

basics

~20 s

A matcher returns { pass, message }, and under .not Jest fails when pass is true, so message must describe that case too, typically via this.isNot. expect.extend registers only in the calling file; move it into a setupFilesAfterEnv module.

open as a page

How would you set the coverage gate for a Jest 30 monorepo so it catches untested code without the percentage becoming the goal?

level: principalimportance: should knowfreq 40%

basics

~20 s

Count all source with collectCoverageFrom, keep one coverage provider, and set coverageThreshold as a rising global floor, higher floors on critical paths and negative counts that freeze legacy code. Coverage proves code ran, not that it was checked.

open as a page

Which equality matcher would you make your team's Jest 30 default for API responses and domain objects, and when would you allow partial matching instead?

level: principalimportance: should knowfreq 33%

basics

~20 s

Default to an exact matcher over the whole object, ideally toStrictEqual, for anything crossing a trust boundary: partial matchers pass a leaked extra field such as a password hash. Allow partial matching only for "at least these fields" contracts.

open as a page

showing 31–36 of 36