Mock Return Value
Predict Output Easy
Question
What does this test code print?
Code
test("ret", () => {
const fn = jest.fn().mockReturnValue(42);
expect(fn()).toBe(42);
});
// Result?
Hint
mockReturnValue makes fn return 42.
Explanation
mockReturnValue(42) configures the mock to always return 42. Calling fn() returns 42. expect(42).toBe(42) passes.
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.