A practical guide to mocking dependencies in Jest using three approaches: overriding methods with jest.fn, using jest.spyOn, and mocking entire modules with jest.mock. Demonstrates each technique with a simple weather app example, including how to mock async functions that return promises. The author recommends the module-level

5m read timeFrom playfulprogramming.com
Post cover image
Table of contents
The app Copy link Link copied!Writing the test Copy link Link copied!How to fake the weatherAPI methods? Copy link Link copied!Override functions with jest.fn Copy link Link copied!Use jest.spyOn Copy link Link copied!Mock the module Copy link Link copied!Testing async functions Copy link Link copied!Final Copy link Link copied!

Sort: