undici: "ReferenceError: AbortSignal is not defined" during jest execution

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.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 3
  • Comments: 16 (8 by maintainers)

Commits related to this issue

Most upvoted comments

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

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