msn0 / fake-fetch Goto Github PK
View Code? Open in Web Editor NEWfake window.fetch for tests
fake window.fetch for tests
Hello there!
I think it would very useful to be able to access the Promise returning from fetch
.
For instance, if my code is something like this:
export function functionToBeTested() {
fetch('url', options).then((res) => {
someLogicIWantToTest(res);
} )
With the actual API I'm not able to test that someLogicIWantToTest
has been called with the correct args.
My proposed solution is to return the Promise from respondWith
module.exports.respondWith = function (data, options) {
var response = Promise.resolve(new Response(data, options));
window.fetch.returns(response);
return response;
};
so I would be able to write a test like:
it('tests some logic', function (done) {
var mockedResponse = fakeFetch.respondWith({ok: true});
functionToBeTested();
mockedResponse.then(() => {
expect(someLogicIWantToTest).toHaveBeenCalledWith(res);
done();
})
})
Let me know if something is not clear!
Is it possible that I use this library in JSFiddle?
I wrote some sample code in this link, and I am not sure how to use this library in it:
http://jsfiddle.net/maryam_saeidi/yredb06m/7/
The code published to NPM is commonjs, which can't be run in the browser without transformations.
It would be great if you could provide an output using standard es modules, so that this library can run in the browser directly. As an example, sinon uses rollup to generate an esm bundle (https://github.com/sinonjs/sinon).
If you use the Request (https://developer.mozilla.org/en-US/docs/Web/API/Request) object as the only argument for your fetch implementation, methods such as getUrl , getMethod, etc. stop working correctly.
fake-fetch is always expecting two arguments from fetch, a URL and options. Can you add support for a single Request object argument?
You can add a conditional to check arguments
if (arg instanceof Request)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.