jasmine-ajax icon indicating copy to clipboard operation
jasmine-ajax copied to clipboard

Support window.fetch

Open onlywei opened this issue 9 years ago • 11 comments

This is the new standard for the web, replacing(?) XHR. Are there plans to support it?

onlywei avatar Sep 30 '15 19:09 onlywei

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.

slackersoft avatar Oct 01 '15 16:10 slackersoft

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?

lencioni avatar Dec 01 '15 00:12 lencioni

fetch() is pretty stable and implemented by several browsers now.

annevk avatar Dec 07 '15 16:12 annevk

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.

slackersoft avatar Jan 06 '16 23:01 slackersoft

would love to see support for the fetch api 👍

jaredrada avatar Dec 21 '16 21:12 jaredrada

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 avatar Apr 22 '17 14:04 codingthat

@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 avatar Apr 24 '17 21:04 slackersoft

@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.

codingthat avatar Apr 25 '17 07:04 codingthat

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.

jraller avatar Feb 09 '19 16:02 jraller

I happy to review a pull request that adds support for window.fetch for environments where it is already defined.

slackersoft avatar Feb 19 '19 19:02 slackersoft

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.

frantic1048 avatar Feb 27 '19 10:02 frantic1048

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.

sgravrock avatar Sep 23 '23 17:09 sgravrock