JavaScript & TypeScript / Quiz
jest.fn, spyOn, and the Three Resets
mockReturnValue vs mockResolvedValue, spies that call through, and the clear/reset/restore trinity that everyone confuses at least once.
- Difficulty
- Medium
- Format
- Quiz
- Points
- 150
- Estimate
- 10 min
// MISSION BRIEF
Your Mission
Test doubles are where unit tests earn their isolation, and where suites quietly rot: a spy that leaks into the next test, a stub returning the wrong shape, a reset that did less than you thought.
This quiz drills Jest's mock function API: jest.fn, jest.spyOn, return-value helpers, call assertions, and exactly what mockClear, mockReset, and mockRestore each do.
Speed bonus active.
// FIRST CONTACT
Battle teaser
Your code awaits api.load(). To stub it with a mock that works under await, you use:
- Ajest.fn().mockResolvedValue({ items: [] }), it wraps the value in a resolved promise; mockReturnValue would hand back a plain object that await treats as already-resolved data but breaks .then chains typed against a promise
- Bjest.fn().mockReturnValue({ items: [] }) is required; promises cannot be mocked
- Cjest.fn(async () => {}).mockReturnValue(Promise)
- Djest.mockPromise({ items: [] })
Answers, scoring, hints, and the full battle stay sealed.
// SKILL TAGS
javascriptjestmockingspiesunit-testing