Comments (6)
I'm inclined to be quite dogmatic about only supporting Promise
defined as a global because that's what the spec says it should be (and encourage users to polyfill it globally if it's not natively available).
Is there some reason you can't/don't want to do this?
from fetch-mock.
OK, it looks like I was operating under some false assumptions.
It does look like isomorphic-fetch's author wants to explicitly support the pattern I described above, but it is not in the library yet (so that approach does not work)
matthew-andrews/isomorphic-fetch#43
I am currently patching Promise globally which resolves my issue.
I would suggest that if isomorphic-fetch
decides to merge in that PR, it would make sense to revisit this, since you explicitly support mocking that library.
In any case, that thank you for your extremely useful tool!
from fetch-mock.
I would use this. We're using fetch-mock to mock our promises, but moving from isomorphic-fetch to fetch-ponyfill at the moment, looking using the new fetch-mock sandbox functionality.
We're using bluebird promises (both for performance and all the extra features), and fetch-ponyfill takes that promise implementation as an option when it's loaded. That implementation all works fine, giving us fetch and Promise working everywhere without touching any globals at all, but we can't easily test it because of this issue.
@wheresrhys Any enthusiasm for reopening this, especially given that you're now looking at supporting the fetch-ponyfill use case?
from fetch-mock.
I'll consider accepting a PR, but don't have any particular enthusiasm for implementing it myself. There are lots of points in the code and the tests where native Promise is assumed, so adding support isn't straightforward and will arguably clutter the code base.
Given that bluebird implements the Promise spec, would it be an option for you to force using native Promise instead of bluebird in your tests?
from fetch-mock.
Given that bluebird implements the Promise spec, would it be an option for you to force using native Promise instead of bluebird in your tests?
I don't think so, we use bluebird methods (like .timeout
) internally, assuming that fetch returns a bluebird promise. We could manually convert it, but with fetch-ponyfill we can guarantee that we don't normally need to at runtime, so it feels messy to have to do that just for the tests.
I've had a look, doesn't seem that tricky I think. I need somewhere to pass in the promise implementation - I'm thinking that actually doing this as part of the sandbox setup is probably sensible, since they're a very similar use case? If you want to do everything with globals you just use fetch-mock
as is, if you don't you call sandbox, provide the promises you want locally, and get a working fetch back, with no globals. E.g.
var fetchMock = require('fetch-mock').sandbox({ Promise: bluebirdPromise })
Does that sound ok? I'll put in a PR a little later today if so.
from fetch-mock.
Yep, good call making it part of the sandbox setup. I'd go for just accepting Promise
as the first parameter. Less future proof, but more user friendly. I'll probably regret saying this, but I can't see there ever being a need to pass in any other config to sandbox()
, or, more seriously, duck-typing coudl be used to deal with that if it ever happens
from fetch-mock.
Related Issues (20)
- regexp matchers cannot always be overwritten HOT 2
- fetch-mock does not forward requests correctly when jsdom-global is in effect
- fetch-mock Response.body is not always a stream in Node.js HOT 3
- Is there a reason for partial package.json in dist directory?
- Jest v27 requires syntax that docs state is not needed for `reset()` method. HOT 1
- 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
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.