Mock Purpose
Question
What is a mock in testing?
Hint
Mocks isolate the code under test from external dependencies.
Explanation
A mock is a fake object that simulates the behavior of a real dependency, allowing tests to run in isolation. Mocks replace: database calls, API requests, filesystem operations, external services. Benefits: 1) Speed — no network/database latency. 2) Determinism — controlled responses. 3) Isolation — test only the unit, not its dependencies. 4) Verification — assert the mock was called with expected arguments. In Jest: jest.fn() creates a mock function, jest.mock() mocks a module. Mocks can return values, throw errors, or return promises. Over-mocking can make tests brittle and not test real behavior.
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.