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

TypeError: Expected signal to be an instanceof AbortSignal #470

Closed
frankandrobot opened this issue May 10, 2022 · 7 comments
Closed

TypeError: Expected signal to be an instanceof AbortSignal #470

frankandrobot opened this issue May 10, 2022 · 7 comments
Labels
bug Something isn't working

Comments

@frankandrobot
Copy link

frankandrobot commented May 10, 2022

Summary: happy-dom/jest-environment does not work with MSW and rtk-query

Environment:

  • @happy-dom/jest-environment: 3.1.1
  • @testing-library/react: 12.1.0
  • @reduxjs/toolkit: 1.7.1
  • msw: 0.38.1
  • react: 16.14.0
  • react-redux: 7.2.6
  • whatwg-fetch: 3.6.2

Sample repo: https://gitlab.com/robotandkid/the-sandbox/-/tree/mre/jest-happy-dom/happy-dom-test

Steps to repo:

  • Add an rtk-query endpoint and a component that consumes this endpoint
  • Add a working test using react-testing-library and MSW. (You will need to add a fetch polyfill to make tests run)
  • Switch to happy-dom/jest-environment
  • Result:
    An unhandled error occured processing a request for the endpoint "getPokemons".
    In the case of an unhandled error, no tags will be "provided" or "invalidated". TypeError: Expected signal to be an instanceof AbortSignal
        at new Request (/Users/uriel.avalos/Documents/the-sandbox/happy-dom-test/node_modules/node-fetch/lib/index.js:1244:10)
        at new Request (/Users/uriel.avalos/Documents/the-sandbox/happy-dom-test/node_modules/happy-dom/src/fetch/Request.ts:9:1)
        at /Users/uriel.avalos/Documents/the-sandbox/happy-dom-test/node_modules/@reduxjs/toolkit/src/query/fetchBaseQuery.ts:235:21
        at step (/Users/uriel.avalos/Documents/the-sandbox/happy-dom-test/node_modules/@reduxjs/toolkit/dist/query/rtk-query.cjs.development.js:23:23)
        at Object.next (/Users/uriel.avalos/Documents/the-sandbox/happy-dom-test/node_modules/@reduxjs/toolkit/dist/query/rtk-query.cjs.development.js:4:53)
        at fulfilled (/Users/uriel.avalos/Documents/the-sandbox/happy-dom-test/node_modules/@reduxjs/toolkit/dist/query/rtk-query.cjs.development.js:95:32)
        at processTicksAndRejections (internal/process/task_queues.js:95:5)

Analysis:

  • my guess is that the polyfill doesn't add itself to the happy-dom global.
  • Is there a way to manually do this?
@capricorn86 capricorn86 added the bug Something isn't working label May 20, 2022
@frankandrobot
Copy link
Author

frankandrobot commented Jun 3, 2022

* It seems that a workaround is to upgrade node-fetch to 3.X. See https://github.com/node-fetch/node-fetch/issues/784 * however, that is a major refactor that would require changing happy-dom, rtk-query, plus whatever uses node-fetch under the hood * But happy-dom *does* work with vitest, so it may worth revisiting to see what vitest does that makes it work.

Update:

@capricorn86
Copy link
Owner

@frankandrobot I am not sure we are ready to close this one. Maybe we should add the polyfill to Happy DOM? 🙂

@capricorn86 capricorn86 reopened this Jun 16, 2022
@frankandrobot
Copy link
Author

yea you're right @capricorn86 . Technically I just found a workaround but it would be nice if fetch "just works" . There are three things that we need to do:

  1. polyfill XMLHttpRequest
  2. polyfill AbortController
  3. this.global.document.defaultView.location.assign("http://localhost"); (allow relative URLs to work)

Note: in our code base, we actually ended up using the classes JSDOM provides :-) (The reason is because we originally thought we needed way more polyfills. ) Most likely the public polyfill packages should work 🤞

Also, I can actually totally help out on this one. :-)

@hornta
Copy link

hornta commented Jun 30, 2022

@frankandrobot What did you do to make this work for you? I'm getting the exact same error. I'm running on happy-dom@6 so it might've been working <6?

@frankandrobot
Copy link
Author

frankandrobot commented Jun 30, 2022 via email

@hornta
Copy link

hornta commented Jul 1, 2022

It still fails for me even though we don't use relative urls and I think I set it up exactly as you've. I hope this will get fixed in this package since there are no other good working solution.

The error I'm getting now is:

An unhandled error occured processing a request for the endpoint "getDataLayerStatus".
In the case of an unhandled error, no tags will be "provided" or "invalidated". TypeError: Request is not a constructor

How it's mocked:

import _fetch from "node-fetch";
import {
  AbortController,
  abortableFetch,
} from "abortcontroller-polyfill/dist/cjs-ponyfill";

const { fetch, Request } = abortableFetch(_fetch);

global.fetch = fetch;
global.Request = Request;
// @ts-ignore
global.AbortController = AbortController;

@capricorn86
Copy link
Owner

This has finally been fixed.

You can read about the release here:
https://github.com/capricorn86/happy-dom/releases/tag/v9.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants