How to mock fetch when testing a React app?
I would like to test a small React web app where I use the global fetch method. I tried to mock fetch in this way: global.fetch = jest.spyOn(global, 'fetch').mockImplementation(endpoint => Promise.resolve({ json: () => Promise.resolve(mockResponse) }) ); ... but the mock seems to be ignored, while the built-in fetch seems to be used: Error: connect ECONNREFUSED 127.0.0.1:80 ... looks like a failed call to the built-in fetch. I then tried to use jest.fn instead of jest.spyOn: global.fetch = jest.fn(endpoint => Promise.resolve({ json: () => Promise.resolve(mockResponse) }) ); ... and was surpris
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.