jasmine-ajax
jasmine-ajax copied to clipboard
Support window.fetch
This is the new standard for the web, replacing(?) XHR. Are there plans to support it?
According to MDN, this is still experimental and the official API hasn't been locked down yet, so we don't have a plan for support just yet. Once it is made official, I'd be happy to review a pull request that added support for the new interface.
Once it is made official
It looks like some things that seem relatively stable (to me at least) are still listed as experimental--for example, CSS transform
. Granted, it looks like the whatwg-fetch repo is recently quite active but if the plan is to wait until MDN doesn't list it as experimental, we might be waiting for quite a while. ;) Perhaps @annevk can give us some insight on how stable the API is and if we can proceed with more confidence?
fetch()
is pretty stable and implemented by several browsers now.
Sounds good. I'd be happy to review a pull request that supports fetch
only in browsers that provide the functionality, leaving browsers that don't (IE, Safari) to behave as if no mock was applied.
would love to see support for the fetch api 👍
Just wondering what I'm missing here. I have Jasmine 2.5.3 and Jasmine-Ajax 3.3.1, and the following seems to run fine (and break fine, if I mess with it):
describe("real ajax", function() {
var testJson = {
"one": "two",
"key": "value"
};
it("can use fetch too", function (done) {
var doneFn = jasmine.createSpy("success");
fetch("http://echo.jsontest.com/key/value/one/two").then(function(response) {
expect(response.status).toBe(200);
return response.json(); // returns a promise
}).then(function(returnedValue) {
// won't be the same object reference, so we can stringify to compare:
expect(JSON.stringify(returnedValue)).toBe(JSON.stringify(testJson));
// or we can use our spy, because toHaveBeenCalledWith is more forgiving
doneFn(returnedValue);
expect(doneFn).toHaveBeenCalledWith(testJson);
// either way, we need to call done(), otherwise our test will time out
done();
}).catch(function(err) {
// Error :(
});
});
});
@codingthat it looks like you're not mocking AJAX at all and thus not actually using the functionality that is being discussed here. The Jasmine-Ajax library is meant to mock out calls to external APIs so the real call doesn't happen.
@slackersoft Thanks, and sorry for the intrusion. Not long after that I had realized the distinction, but had a few other things happening and forgot to return to this thread.
In an article https://lazamar.github.io/testing-http-requests-with-jasmine/ @lazamar suggests
window.fetch = undefined;
require('whatwg-fetch')
require('jasmine-ajax')
// Do tests
Which looks like a reasonable work around, but only until jasmine-ajax supports fetch directly.
I happy to review a pull request that adds support for window.fetch
for environments where it is already defined.
I'm suprised this issue is still active today.
I encountered this issue 3 years ago. Finally, I used sinonjs (Actually any test util that supports stub function is fine. You can even write a stub function by yourself. ) to workaround this issue, simply sinon.stub(window, 'fetch')
and mock anything you want.
I think that if this was worth implementing, somebody would've submitted a PR or at least expressed interest in doing so at some point in the last eight years. I agree with @frantic1048: the simplicity of the fetch API lends itself well to lighter-weight mocking techniques. I can imagine reasons why someone might still want to use jasmine-ajax with fetch, but eight years without anyone trying to add fetch support is a strong signal that it's not worthwhile.
Closing.