jefflau / jest-fetch-mock

Jest mock for fetch
MIT License
886 stars 117 forks source link

jest-fetch-mock being over ridden by actual fetch package #51

Closed paultannenbaum closed 6 years ago

paultannenbaum commented 6 years ago

Hi there, I am having an issue with implementing the library, and wondering what the best way to tackle the issue is.

So in my redux project, we have an api file that makes service calls to our backend service, using isomorphic-fetch. This file imports the module and then invokes fetch on the given service urls.

I believe I have my test file setup correctly, the fetch object is the jest-fetch-mock object (has access to the mock methods, etc.) when the test starts up, but when the test passes through the api file it is using the fetch object from the isomorphic-fetch module instead of the mock module. Because I am importing isomorphic-fetch module directly into the file, it is over riding the global fetch object I set in the setupTests file. I have confirmed this by removing the import from the api file, and then the test will use the mock fetch object that was set as a global in the setup file.

So my question is what is wrong about my implementation? Any help would be appreciated.

paultannenbaum commented 6 years ago

So I figured out this myself, and wanted to post my solution in case anyone else is a bonehead like me. Isomorphic fetch sets fetch as a global regardless of evironment. So no need to name the import.

Before my import in api.js looked like this: import fetch from 'isomorphic-fetch

Now it looks like this: import isomorphic-fetch

Everything else stays the same 🙌

jefflau commented 6 years ago

Glad you sorted it out!


From: Paul Tannenbaum notifications@github.com Sent: Thursday, February 15, 2018 6:19:46 AM To: jefflau/jest-fetch-mock Cc: Subscribed Subject: Re: [jefflau/jest-fetch-mock] jest-fetch-mock being over ridden by actual fetch package (#51)

So I figured out this myself, and wanted to post my solution in case anyone else is a bonehead like me. Isomorphic fetch sets fetch as a global regardless of evironment. So no need to name the import.

Before my import in api.js looked like this: import fetch from 'isomorphic-fetch

Now it looks like this: import isomorphic-fetch

Everything else stays the same 🙌

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/jefflau/jest-fetch-mock/issues/51#issuecomment-365763980, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ABKiFWa3iLQ55UsDps9TNYyPOVDp6oiAks5tU1wCgaJpZM4SF_WH.

ryexley commented 3 years ago

I'd like to publicly thank @paultannenbaum for sharing his solution that he discovered. I didn't fully understand what my problem was, but I was experiencing the exact same issue. I wasted 5 hours worth of work yesterday trying to figure out why this wasn't working as documented, and then I stumbled on to this issue, and my problem was solved instantly. Thanks so much for sharing.