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 pageshowhide
explore
- Test Structure and Matchers6 questions
- Mocks and Spies6 questions
- Async Testing6 questions
- Snapshot Testing6 questions
- Setup and Configuration6 questions
- Coverage6 questions
questions
page 2 of 2In 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?
basics
~20 sprojects 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.
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?
basics
~20 sJest 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.
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?
basics
~10 sWrite 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.
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?
basics
~20 sA 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.
How would you set the coverage gate for a Jest 30 monorepo so it catches untested code without the percentage becoming the goal?
basics
~20 sCount 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.
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?
basics
~20 sDefault 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.
showing 31–36 of 36