Comments (4)
Hello,
Before fixing this it'd be good to know which environment it's happening in as the current implementation should work so I'd like to understand a bit more about why it's not. Where are you getting Request
from? window.Request
or somewhere else?
from fetch-mock.
This is in a node environment, with isomorphic-fetch
loaded beforehand.
This should replicate the issue for you.
require('isomorphic-fetch');
var fetchMock = require('fetch-mock');
fetchMock.mock('http://example.com/', '{"foo": "BAR"}');
var request = new Request('http://www.example.com', { method: 'GET' });
fetch(request).then(function(response) {
return response.text();
}).then(function(responseText) {
console.log(responseText);
});
That final console log outputs the html from example.com, rather than the mocked JSON.
from fetch-mock.
Ah now I see the problem. I guess you're not on npm 3 yet, which means that isomorphic-fetch and fetch-mock don't share an instance of node-fetch unless you call npm prune
.
I'm not sure whether to fix this with your suggested solution or to add a suggestion to run npm prune
when not working in npm 3+. The downsides of your solution as I see it are:
- If fetch is accidentally called with an object or number, say, then, normalize request will return
{url: undefined, options: undefined}
, which could lead to some difficult to debug test failures - The primary use case for this module is to mock fetch in the browser, where the problem you're reporting won't occur
On the other hand, your solution does basically work. Could you run npm prune
and see if that fixes your problem?
from fetch-mock.
Well, npm prune
didn't work. I even nuked npm_modules, reinstalled, and saw the same problem.
Updating npm worked though!
I was on [email protected], now I'm using [email protected]. That's fine for me.
As for the fix, I'm fine with "update npm" being the answer. I totally agree with the loosey-goosey nature of the fix I suggested, and prefer the code as-is.
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.