Comments (9)
We had the exact same issue. Found a solution, but it required a bit of extra code that we have now shared.
https://github.com/spaceviewinc/fetch-mock-forwarder
from fetch-mock.
Hey, I was having similar problems, and I resolved it by importing isomorphic-fetch to the global namespace rather than importing it to a variable. I see in your code that you were importing it to a variable as well, so this might be a solution for you.
I think isomorphic-fetch decorates or patches the already existing global fetch, so importing to a variable causes problems.
from fetch-mock.
From reading your code it looks like you're not interested in analysing calls to fetch, just in returning a mock response - is that right? In that case you could just return new Response(url, opts)
using code similar to https://github.com/wheresrhys/fetch-mock/blob/master/src/fetch-mock.js#L17 and leave the call to fetch
out of your implementation.
I could expose my mockResponse function as a static helper method if that'd be useful
from fetch-mock.
Hi,
Yea thats right.
I managed to get this working by using window.fetch instead of just fetch. From what i thought, fetch (from isomorphic fetch) for the browser would use that from the global window object which it patches if necessary (think i read that in the docs somewhere). It seems that fetch being referenced in my code isnt the same fetch as that of the browser window object (which upon logging in the console returns the mock function).
This just means that i have to annoyingly reference window.fetch until i get my backends up and then change all references to fetch when i dont need the mock response any longer and I need to start rendered things server side.
from fetch-mock.
I'd happily accept PR to document this in the troubleshooting section of the readme
from fetch-mock.
Hi!
I am having a very similar issue, also with a react/redux/webpack application, and am running out of ideas. I try to use fetch-mock in a unit-test, that's supposed to test a function I import from another file, but a real request is being performed (I've also described the problem more exact on StackOverflow). Can anyone please give me a hint on what I am missing?
from fetch-mock.
@SpaceviewMob solution worked for me
from fetch-mock.
Fantastic, it works for me, too! Thank you @marc-romera and @SpaceviewMob !
from fetch-mock.
FWIW I think @SpaceviewMob's solution, while it works, is an anti-pattern. In the browser fetch
is supposed to be a global, as defined by the whatwg standard - any modules you're importing to provide as a fetch
implementation should be considered as temporary polyfills for browsers that lack the native functionality. Assigning fetch
to a variable departs from the standard, which means at some point in the future you will likely have to refactor.
I cannot see any good reason not to use fetch as a global in the browser, or am I missing something???
from fetch-mock.
Related Issues (20)
- Provide RequestInit object to native fetch on Safari HOT 1
- Problem with fetch-mock and HMR HOT 3
- Update node-fetch dependency to v3.x.x HOT 5
- Querystring is deprecated
- Unable to use fetch-mock as ES6 import HOT 10
- fetch-mock: No fallback response defined for GET to https://jsonmock.hackerrank.com/api/stocks/?date=1-January-2002
- [Bug] Using fetch-mock with the decorator function results in internal error HOT 5
- When a matcher fails because a header matches the key but not the value, other matchers aren't considered
- Returning different values for different requests on the same URL HOT 3
- Response always undefined in .calls? HOT 1
- regenerator global Function call throws CSP unsafe-eval error HOT 2
- Typescript: FetchMockSandbox and native fetch have incompatible types HOT 1
- [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" HOT 1
- Response.error() is not being treated as a response HOT 1
- Allow access to `express`-style path parameters in mocked endpoints HOT 2
- done() not recognised matched requests HOT 1
- fetch-mock not working after update to Node v18 HOT 4
- "SyntaxError: Cannot use import statement outside a module" when importing fetchMock from fetch-mock HOT 2
- es5/client-legacy-bundle.js uses es6 syntax resulting in errors for older browsers HOT 1
- does it support import.meta.hot?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fetch-mock.