All interview guides

Jest Interview Questions and Answers

24 questions that come up in Jest technical interviews, each with the answer and an explanation of why it is right.

Test yourself — 90 question bank

1. What is Jest?

Beginner

Answer: JavaScript testing framework

Jest is a delightful JavaScript testing framework with a focus on simplicity. Created by Facebook, it works with React, Vue, Angular, Node.js, and more.

2. What is mocking in Jest?

Intermediate

Answer: Replacing real implementations with test versions

Mocking replaces real implementations with controllable test versions. Isolate code under test, control dependencies, verify interactions. Use jest.fn(), jest.mock().

3. What is jest.config.js?

Advanced

Answer: Configuration file for Jest settings

jest.config.js configures Jest. Define testEnvironment, coverageThreshold, setupFiles, moduleNameMapper, transform, etc. Export configuration object.

4. What is jest.fn()?

Intermediate

Answer: Creates mock function

jest.fn() creates mock function. Track calls, arguments, return values. Can provide implementation. Example: const mockFn = jest.fn(); mockFn(); expect(mockFn).toHaveBeenCalled().

5. What is testEnvironment?

Advanced

Answer: Execution environment (node or jsdom)

testEnvironment sets execution environment. 'node' for Node.js APIs, 'jsdom' for browser-like DOM. Configure in jest.config.js or per-file with @jest-environment.

6. How do you install Jest?

Beginner

Answer: Both a and b

Install Jest as dev dependency: npm install --save-dev jest or npm install jest --save-dev. Both commands are equivalent.

7. What is a test in Jest?

Beginner

Answer: Function checking if code behaves as expected

A test is a function that checks if code behaves as expected. Uses test() or it() function with description and test function.

8. What is setupFilesAfterEnv?

Advanced

Answer: Runs setup code after test framework installed

setupFilesAfterEnv runs setup code after Jest installed. Configure global matchers, extend expect, setup test utilities. Array of file paths in jest.config.js.

9. How do you check if mock was called?

Intermediate

Answer: expect(mockFn).toHaveBeenCalled()

Check calls with toHaveBeenCalled(). Also: toHaveBeenCalledTimes(n), toHaveBeenCalledWith(args), toHaveBeenLastCalledWith(args).

10. What is moduleNameMapper?

Advanced

Answer: Maps module paths to mocks or aliases

moduleNameMapper maps module imports. Handle CSS modules, images, path aliases. Use regex: '^@/(.*)$': '<rootDir>/src/$1'. Mock non-JS imports.

11. How do you write a basic test?

Beginner

Answer: test('description', () => { expect... })

Write test with test() or it(): test('adds 1 + 2 to equal 3', () => { expect(1 + 2).toBe(3); }). Use expect() for assertions.

12. What is jest.mock()?

Intermediate

Answer: Mocks entire module

jest.mock('module') mocks entire module. Auto-mocks all exports. Manual mock: jest.mock('module', () => ({...})). Hoisted to top of file.

13. What is the difference between test() and it()?

Beginner

Answer: No difference, aliases for same function

test() and it() are aliases - completely interchangeable. it() comes from BDD (Behavior Driven Development) style. Choose based on team preference.

14. What is jest.spyOn()?

Intermediate

Answer: Mocks method while keeping original implementation accessible

jest.spyOn(object, 'method') creates spy on existing method. Can mock implementation or call through. Restore with mockRestore(). Non-destructive mocking.

15. What is transform?

Advanced

Answer: Transforms files before tests using preprocessor

transform specifies file transformers. Default babel-jest for JS. TypeScript: ts-jest. Configure: {'^.+\\.tsx?$': 'ts-jest'}. Handles compilation.

16. How do you test async code with promises?

Intermediate

Answer: Return promise or use async/await

Test promises by returning promise or using async/await. test('async', async () => { await expect(promise).resolves.toBe(value); }). Must return or await.

17. What is collectCoverageFrom?

Advanced

Answer: Specifies files to include in coverage

collectCoverageFrom specifies files for coverage. Glob patterns: ['src/**/*.js', '!**/*.test.js']. Excludes test files. Configure in jest.config.js.

18. What is expect()?

Beginner

Answer: Creates assertion with matchers

expect() creates an assertion. Chain with matchers to test values: expect(value).toBe(expected). Core of Jest assertions.

19. What is coverageThreshold?

Advanced

Answer: Enforces minimum coverage percentages

coverageThreshold enforces minimum coverage. Set global or per-directory. Fails if below threshold. Example: { global: { statements: 80 } }.

20. What is toBe() matcher?

Beginner

Answer: Checks exact equality using ===

toBe() uses Object.is for exact equality (===). For primitives and object references. expect(2 + 2).toBe(4). Use toEqual() for deep equality.

21. What is resolves matcher?

Intermediate

Answer: Tests resolved value of promise

resolves unwraps promise and tests resolved value. await expect(promise).resolves.toBe(value). Cleaner than .then(). Works with any matcher.

22. What is toEqual() matcher?

Beginner

Answer: Deep equality check for objects/arrays

toEqual() checks deep equality. Recursively checks object/array contents. expect({a: 1}).toEqual({a: 1}). Use for objects, arrays. toBe() checks reference.

23. What is custom matcher?

Advanced

Answer: User-defined matcher extending expect

Custom matchers extend expect. Define in setupFilesAfterEnv: expect.extend({ toBeWithinRange() {...} }). Create reusable, domain-specific assertions.

24. What is rejects matcher?

Intermediate

Answer: Tests rejected value of promise

rejects tests promise rejection. await expect(promise).rejects.toThrow(). Tests promise rejects with specific error. Must use await or return.

Ready to test yourself?

The full Jest bank has 90 questions across 3 difficulty levels — timed, shuffled, and scored.

Take the Jest quiz