nodejs / undici

An HTTP/1.1 client, written from scratch for Node.js
https://nodejs.github.io/undici
MIT License
6.21k stars 541 forks source link

"ReferenceError: AbortSignal is not defined" during jest execution #1557

Open JacopoPatroclo opened 2 years ago

JacopoPatroclo commented 2 years ago

Bug Description

When I try to run with jest a test suite that uses a file that import undici I get this error

ReferenceError: AbortSignal is not defined

  at Object.<anonymous> (../node_modules/undici/lib/fetch/request.js:839:3)
  at Object.<anonymous> (../node_modules/undici/index.js:94:28)

Reproducible By

Create a basic jest configuration with testEnvironment: 'node', Create a js file that imports the undici library Create a spec file to test somthing exported from that file Run jest

Expected Behavior

To not throw that exception

Environment

Node v16.14.0 Jest v27.2.1

Additional context

Googling the error I found out that another lib had to solve a similar issue. They think it's related on how jest is mocking AbortSignal and AbortController.

ronag commented 2 years ago

Feels more like a jest issue

mcollina commented 2 years ago

We are also running some jest tests to verify compatibility, so this is a bit unexpected. There is likely something else at play.

Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.

JacopoPatroclo commented 2 years ago

@mcollina I think you are right. I've tried to reproduce it in a separate (simpler) setup and the issue does not seem to be present. Could it be that there is some other package that pollutes the global state, deleting AbortSignal? Given your experience, how can I debug an issue like this?

Anyway I'm closing this due to the fact that the error isn't caused by undici. Thanks for the quick response and for this amazing lib.

For reference, if someone stumble into this issue an easy workaround is to mock undici as a module using the moduleNameMapper node in the jest config. See here the docs.

watson commented 2 years ago

Odd, I'm also getting this issue in a just test suite while upgrading to the newest version of undici. I know it's not the fault of undici, but just thought I'd mention it here in case anybody had found the culprit since this issue was closed.

feugy commented 2 years ago

In case this helps, the regression started with version 5.6.0. Working with 5.5.1 works. Trying to assemble a reproduction case.

mcollina commented 2 years ago

A PR would be helpful.

LaurentZuijdwijk commented 2 years ago

Still no solution for this? I imagine this breaking a lot of projects and it prevents adoption within my organisation.

mcollina commented 2 years ago

@LaurentZuijdwijk would you like to send a PR?

feugy commented 2 years ago

I couldn't come up with an easy reproduction. The mono-repo where it's happening is quite big, and there are surely side-effects which result in this error. What I can tell for sure, is that undici and fetch are working absolutely fine on a ts/js projects with ESM.

mcollina commented 2 years ago

A PR that fixes it here will be highly welcomed!

noel-dolan commented 2 years ago

Also seeing this by simply adding @fastify/http-proxy to project, jest test then start failing:

ReferenceError: AbortSignal is not defined at Object. (/.../node_modules/undici/lib/fetch/request.js:890:3) at Runtime._execModule (/.../node_modules/jest-runtime/build/index.js:1299:24)

version info: node: v16.17.1 package.json bits ... @fastify/autoload: "^5.0.0", @fastify/http-proxy: "7.0.0", axios: "^0.27.2", fastify: "^4.2.0", jest: "^26.6.3 ...

yarn.lock

"@fastify/http-proxy@7.0.0": version "7.0.0" resolved "https://registry.yarnpkg.com/@fastify/http-proxy/-/http-proxy-7.0.0.tgz#f21bda45657faca4557c86f29509eb2b6e1e2abd" integrity sha512-nkx8NedKDNrnoxwzFsKUjexi2b5Tgw8IMaTKUK/BtZDop5eM86j3kEjecnIT25qTpxymvIFDsuTLk4iVmt2G3A== dependencies: "@fastify/reply-from" "^7.0.0" ws "^8.4.2"

"@fastify/reply-from@^7.0.0": version "7.0.1" resolved "https://registry.yarnpkg.com/@fastify/reply-from/-/reply-from-7.0.1.tgz#ae3798f307bfa1d14b83878cd442f5f7083fabb5" integrity sha512-ikp6GpmEJ7AVxcDdSVE9MhpUtC9KnImQDegc5ePZ+H7QZcraIjotP7YndwT/fP8lYj2Qr1h4RtuFNU8Wdwleuw== dependencies: end-of-stream "^1.4.4" fastify-plugin "^3.0.0" http-errors "^2.0.0" pump "^3.0.0" semver "^7.3.5" tiny-lru "^8.0.1" undici "^5.0.0"

undici@^5.0.0: version "5.12.0" resolved "https://registry.yarnpkg.com/undici/-/undici-5.12.0.tgz#c758ffa704fbcd40d506e4948860ccaf4099f531" integrity sha512-zMLamCG62PGjd9HHMpo05bSLvvwWOZgGeiWlN/vlqu3+lRo3elxktVGEyLMX+IO7c2eflLjcW74AlkhEZm15mg== dependencies: busboy "^1.6.0"

noel-dolan commented 2 years ago

As mentioned, similar issue seems to have been fixed in another library.

For reference, see: https://github.com/isaacs/node-lru-cache/issues/239

dios-david commented 1 year ago

fyi I updated to latest jest (29) and undici works fine with the node test environment

jltxwesley commented 1 year ago

fyi I updated to latest jest (29) and undici works fine with the node test environment

@dios-david No luck with this, having the latest jest (29.3.1) & undici (5.14.0) still throws the ReferenceError, which version do you use?

dios-david commented 1 year ago

@jltxwesley versions I'm using (from lock file) are:

In the jest config I use testEnvironment: 'node'

cefeboru commented 1 year ago

Any workaround for this?

I tried assigning the global AbortSignal on a setup file but more errors keep showing:

ReferenceError: Event is not defined

      at Object.<anonymous> (node_modules/undici/lib/fileapi/progressevent.js:10:29)
      at Object.<anonymous> (node_modules/undici/lib/fileapi/util.js:10:27)
      at Object.<anonymous> (node_modules/undici/lib/fileapi/filereader.js:7:5)
      at Object.<anonymous> (node_modules/undici/index.js:118:31)

And If try to define Event, then:

ReferenceError: EventTarget is not defined

      at Object.<anonymous> (node_modules/undici/lib/fileapi/filereader.js:18:26)
      at Object.<anonymous> (node_modules/undici/index.js:118:31)
      at Object.<anonymous> (node_modules/@elastic/transport/src/connection/UndiciConnection.ts:36:1)
      at Object.<anonymous> (node_modules/@elastic/transport/src/connection/index.ts:22:1)\

I'm using jest 26.2.2 and I can't really upgrade to version 29 as there are hundreds of unit tests and a lot of them break after upgrading.

Edit:

We were having this issue due to using the latest elasticsearch client, and since we didn't really use the undici library directly, we ended up mocking it completely to fix this error.

jest.mock('undici')
zabdalimov commented 4 months ago

Had the same issue when using jest v26, resolved it by upgrading jest to v27.