Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GlobalFetch API - Headers, Request, Response #1724

Open
piuccio opened this issue Jan 31, 2017 · 23 comments
Open

Add GlobalFetch API - Headers, Request, Response #1724

piuccio opened this issue Jan 31, 2017 · 23 comments
Labels

Comments

@piuccio
Copy link

piuccio commented Jan 31, 2017

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
https://developer.mozilla.org/en-US/docs/Web/API/Headers
https://developer.mozilla.org/en-US/docs/Web/API/Request
https://developer.mozilla.org/en-US/docs/Web/API/Response

@jods4
Copy link

jods4 commented Mar 12, 2017

+1 would be nice as General availability in browsers means more web apps use it and fail in JSDOM.
Given that JSDOM implements XMLHttpRequest, it could be as simple as running whatwg-fetch polyfill at startup, right?

@domenic
Copy link
Member

domenic commented Mar 12, 2017

No, unfortunately the whatwg-fetch polyfill is not very spec compliant. Someone needs to write an implementation of the fetch algorithm in the standard: https://fetch.spec.whatwg.org/#fetching

That said, in your own usage of jsdom you're welcome to include <script src="fetch-polyfill.js"> in the HTML you pass to it, and that should work, if you're willing to accept the non-compliant polyfill semantics. We just can't bake them into jsdom.

@jods4
Copy link

jods4 commented Mar 12, 2017

Yes, for now I'm using the whatwg polyfill in JSDOM.
I'm not sure how much "non-compliant" it is, but it seems to work well enough for my case.

It would be nice to see something built-in, though. Especially since the Fetch API has become commonplace.

@domenic
Copy link
Member

domenic commented Mar 12, 2017

Yeah, it definitely would be nice! I hope someone is able to pull-request an implementation of the spec.

@KyleGalvin
Copy link

KyleGalvin commented Apr 8, 2018

My application tests run in node rather than the browser. The lack of server-side fetch API (and typescript bindings to match) has become a show-stopper.

This issue looks like the most promising solution.

Does anyone else know of a workaround for the mean-time? I can use fetch in the browser. I can use node-fetch for tests, but I can't switch on the two packages dynamically. One environment or the other will always fail.

Edit: whatwg-fetch seems to be the answer. Install the package, dont install the typings. import it only in the test runner so the browser application does not have conflicting definitions

@ghostd
Copy link

ghostd commented Mar 11, 2020

Is this issue still relevant since v15 supports fetch?

@domenic
Copy link
Member

domenic commented Mar 11, 2020

v15 does not support fetch.

@ghostd
Copy link

ghostd commented Mar 11, 2020

Sorry, i misread the Changelog about the 15.1, only Headers was provided

@thiscompsciguy

This comment has been minimized.

@Dscheksn
Copy link

Does anyone else know of a workaround for the mean-time?

My workaround looks like this:

    const firstTodo: Todo = { title: 'Todo 1' };
    const secondTodo: Todo = { title: 'Todo 2' };
    const todos: Todo[] = [firstTodo, secondTodo];

    global['fetch'] = jest.fn().mockImplementation(() =>
      Promise.resolve({
        json: () => Promise.resolve(todos),
      })
    );

    await act(async () => {
      render(<App />, container);
    });

@alexfromapex
Copy link

but in all seriousness, I have an easy workaround:

First, npm install --save-dev whatwg-fetch

.. then ..

jest.config.js:

...
  setupFiles: ['<rootDir>/setupJest.js'],
...

setupJest.js:

...
import 'whatwg-fetch';
...

and then you'll automagically have all of the fetch related globals defined in your test code

tnajdek added a commit to zotero/zoterobib that referenced this issue May 26, 2023
Jest does not include Node's fetch:
jsdom/jsdom#1724

Previously we had cross-fetch included by a library but that was removed
in f594a70 so a workaround is needed.
tnajdek added a commit to tnajdek/zoterobib that referenced this issue May 26, 2023
Jest does not include Node's fetch:
jsdom/jsdom#1724

Previously we had cross-fetch included by a library but that was removed
in f594a70 so a workaround is needed.
tnajdek added a commit to tnajdek/zoterobib that referenced this issue May 26, 2023
Jest does not include Node's fetch:
jsdom/jsdom#1724

Previously we had cross-fetch included by a library but that was removed
in f594a70 so a workaround is needed.
@luchillo17
Copy link

I've fixed this by using the import 'cross-fetch/polyfill'; on the test file (my library has a mixed environment, half of it is meant for node, the other half for jsdom, SSR and RSC in Next.js makes it hard to know what environment it should be using in tests).

avernet added a commit to orbeon/orbeon-forms that referenced this issue Jun 25, 2023
- It is needed by JSDOM, which doesn't support fetch
  (jsdom/jsdom#1724).
- TODO: only include the fetch polyfill for the replication test.
avernet added a commit to orbeon/orbeon-forms that referenced this issue Jun 25, 2023
- It is needed by JSDOM, which doesn't support fetch
  jsdom/jsdom#1724
- TODO: only include the fetch polyfill for the replication test
@darrinmn9
Copy link

I just want to add on to the suggested workaround of patching jest-environment-jsdom, you might also need AbortController in order to abort fetch requests.

this.global.AbortController = AbortController

AlbertoHdezCerezo added a commit to Alberto-Writes-Typescript/a-remarkable-js-sdk that referenced this issue Feb 22, 2024
- jest-environment-jsdom: Not included by default in Jest. It emulates a web browser environment to test our code.
- whatwg-fetch: (more info in here: jsdom/jsdom#1724) It seems JSDom environment does not have support for globalThis.fetch method. Since this is what we want to test, I found in this issue a workaround so we can invoke the method in our tests.
AlbertoHdezCerezo added a commit to Alberto-Writes-Typescript/a-remarkable-js-sdk that referenced this issue Feb 22, 2024
- jest-environment-jsdom: Not included by default in Jest. It emulates a web browser environment to test our code.
- whatwg-fetch: (more info in here: jsdom/jsdom#1724) It seems JSDom environment does not have support for globalThis.fetch method. Since this is what we want to test, I found in this issue a workaround so we can invoke the method in our tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests