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)
- Mock two consecutive calls with different responses without explicit access to the calling context. HOT 3
- Remove support for passing in a `matcher` property in
- migrate docs to docusaurus
- Add support for all the types of body in the fetch spec
- Remove top level done and flush methods
- Rename functionMatcher to func
- Export types perfectly from @fetch-mock/core
- Cannot consume CJS build with TypeScript HOT 7
- Combine callLog and normalizedRequest?
- access to query parameters in CallLog
- host: and protocol: url matchers
- Implement spec compliant errors HOT 2
- Export more types from @fetch-mock/core HOT 8
- Rethink URL normalization behaviour HOT 1
- Types broken in 10.x for ESM HOT 7
- `.../es5/client-bundle.js` no longer exist HOT 9
- `globrex` does not support browser HOT 5
- browser tests aren't actually running in a browser any more
- @fetch-mock/vitest todo HOT 1
- fetch-mock -> @fetch-mock/core codemod
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.