In Jest, describe creates a block that groups related tests, often representing a unit or feature, while it (an alias of test) defines an individual test case with an assertion. Nesting describe blocks organizes a suite hierarchically and improves readable output. Each it block typically follows an arrange-act-assert structure and uses expect() matchers. Lifecycle hooks like beforeEach can share setup. This structure keeps test files self-documenting and makes failures easy to locate.
2 / 5
What is a mock in Jest?
A mock is a stand-in that replaces a real dependency so a test runs in isolation and deterministically. With jest.fn() you create a mock function whose return values and implementations you control, and jest.mock() can replace whole modules. Mocks let you avoid slow or unpredictable resources like networks or databases, and they record how they were called so you can assert on arguments and call counts. Proper mocking keeps unit tests fast, focused, and independent of external systems.
3 / 5
What is a spy in Jest?
A spy wraps an existing function to observe how it is called, recording arguments, call counts, and return values, while optionally still invoking the original implementation. You create one with jest.spyOn(object, method). Spies are ideal when you want to verify that a method was called without fully replacing it. You can also override its behavior with mockImplementation when needed, and restore the original afterward. Spies bridge the gap between full mocks and leaving real code untouched.
4 / 5
What is snapshot testing in Jest?
Snapshot testing serializes a value, such as a rendered component or a data structure, and stores it in a snapshot file the first time a test runs. On subsequent runs Jest compares the new output against the saved snapshot and fails if they differ. This catches unintended changes in output. When a change is expected, you update the snapshot with jest --updateSnapshot. Snapshots are powerful for UI regression checks but require review discipline, since blindly updating them defeats their purpose.
5 / 5
What does test coverage measure?
Coverage measures how much of your source code is exercised by the test suite, typically reported as percentages for statements, branches, functions, and lines. Jest can produce a coverage report with the --coverage flag, highlighting code paths no test reaches. High coverage indicates thorough execution but does not guarantee correctness, since lines can run without meaningful assertions. Teams often set coverage thresholds in configuration to enforce a baseline, treating coverage as one signal among several rather than an end in itself.