1. What is Jest?
BeginnerAnswer: 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.
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 bankAnswer: 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.
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().
Answer: Configuration file for Jest settings
jest.config.js configures Jest. Define testEnvironment, coverageThreshold, setupFiles, moduleNameMapper, transform, etc. Export configuration object.
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().
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.
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.
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.
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.
Answer: expect(mockFn).toHaveBeenCalled()
Check calls with toHaveBeenCalled(). Also: toHaveBeenCalledTimes(n), toHaveBeenCalledWith(args), toHaveBeenLastCalledWith(args).
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.
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.
Answer: Mocks entire module
jest.mock('module') mocks entire module. Auto-mocks all exports. Manual mock: jest.mock('module', () => ({...})). Hoisted to top of file.
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.
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.
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.
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.
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.
Answer: Creates assertion with matchers
expect() creates an assertion. Chain with matchers to test values: expect(value).toBe(expected). Core of Jest assertions.
Answer: Enforces minimum coverage percentages
coverageThreshold enforces minimum coverage. Set global or per-directory. Fails if below threshold. Example: { global: { statements: 80 } }.
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.
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.
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.
Answer: User-defined matcher extending expect
Custom matchers extend expect. Define in setupFilesAfterEnv: expect.extend({ toBeWithinRange() {...} }). Create reusable, domain-specific assertions.
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.
The full Jest bank has 90 questions across 3 difficulty levels — timed, shuffled, and scored.
Take the Jest quiz