Mock Module
Question
What does jest.mock() do?
Hint
jest.mock automocks the entire module.
Explanation
jest.mock("moduleName") replaces the imported module with an auto-generated mock (all functions become jest.fn()). jest.mock("moduleName", () => ({ fn: jest.fn() })) provides a custom mock implementation. jest.mock is hoisted to the top of the file (before imports). Use jest.unmock to restore. jest.mock is useful for: 1) Mocking API clients (axios, fetch). 2) Mocking database modules. 3) Mocking external services. 4) Mocking configuration. Access the mock with require("moduleName") after mocking. jest.fn().mockResolvedValue(data) mocks async functions. Be specific — over-mocking can hide real bugs.
Related Resources
Related Lessons
- Mocking
Learn mocking.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.