Jest JavaScript Testing Interview
Curated 20+ questions covering Jest matchers, async code, snapshot updates, lifecycle hooks, mock utilities, and config options.
Difficulty Filters
Tools & Options
What is Jest, and what are its key advantages?
Jest is a delightful JavaScript testing framework maintained by Meta, designed with a focus on simplicity. Key advantages include: zero configuration out-of-the-box, built-in assertion library, built-in test runner, built-in mocking utilities, snapshot testing, parallel test execution, and fast watch mode.
Explain the difference between `toBe()` and `toEqual()` matchers in Jest.
`toBe()` checks for strict identity equality using `Object.is` (compares memory references, like `===`). `toEqual()` checks for value/structural equality, recursively comparing all properties of objects or elements in arrays.
const obj1 = { value: 1 };
const obj2 = { value: 1 };
expect(obj1).toEqual(obj2); // Passes! Structural match
expect(obj1).toBe(obj2); // Fails! Different memory referencesHow do you test asynchronous code in Jest?
Jest supports testing async code using three main patterns:
1. Async/Await (Preferred): Write test with `async` and `await` promises.
2. Returning a Promise: Return the promise from the test body; Jest waits for resolution.
3. Using the `done` callback parameter: Call `done()` manually when the async code finishes.
// Async/Await example
test('fetches data', async () => {
const data = await fetchData();
expect(data).toBe('success');
});What is Snapshot Testing, and when should you use it?
Explain the difference between `jest.fn()`, `jest.spyOn()`, and `jest.mock()`.
How do you mock an external module (e.g., `axios`) in Jest?
What are Jest setup and teardown lifecycle hooks?
Explain how to check if a mock function was called.
What is `jest.useFakeTimers()`, and when is it used?
What is a mock implementation vs a mock return value?
How do you test that a function throws an error in Jest?
What is the Jest configuration file, and what is its default name?
What is the difference between `jest.runOnlyPendingTimers()` and `jest.runAllTimers()`?
What is the difference between `test()` and `describe()` in Jest?
Explain `jest.clearMocks()`, `jest.resetMocks()`, and `jest.restoreMocks()`.
What is the difference between Jest's `node` and `jsdom` test environments?
Explain code coverage configuration in Jest.
How do you run a specific single test file in Jest?
What is the `mockFn.mock.calls` property?
How do you mock modules located in `__mocks__` directory?
Technical Interview Mastery Blueprint
Proven methodologies and frameworks to succeed in technical and architecture rounds.
1. The Engineering STAR Technique
When responding to architectural or situational questions, structure your answer using Situation, Technical Constraint, Action, and Measurable Result. Rather than jumping straight to syntax, articulate the business problem, why certain patterns (e.g. caching, queuing, immutability) were selected over alternatives, and what latency or throughput improvements were achieved.
2. Defending Architectural Trade-Offs
Senior interviewers evaluate how you weigh trade-offs. Always explain why you chose an approach: e.g., memory overhead vs CPU cycle speed, consistency vs availability (CAP theorem), or normalized schemas vs denormalized fast reads. Stating the drawbacks of your own proposal demonstrates maturity and production experience.
3. Clean Code & Edge-Case Proactivity
In live-coding rounds, communicate your thinking aloud before typing. State edge cases explicitly: null or undefined inputs, extreme boundary values, concurrency races, and network timeouts. Writing modular, well-named functions with deterministic unit tests separates junior candidates from staff-level engineers.
4. Antipatterns to Avoid
Avoid reciting memorized definitions without practical context. Never dismiss non-functional requirements such as security (CSRF, injection, token rotation), observability (logging, metrics), and accessibility (ARIA, semantic standards). Always ask clarifying scope questions before writing complex implementations.
Explore Related Tools
Hand-picked utilities and calculators related to this tool.
