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

Code assumes clearImmediate #2398

Closed
1 of 2 tasks
bpinto opened this issue Sep 13, 2022 · 9 comments
Closed
1 of 2 tasks

Code assumes clearImmediate #2398

bpinto opened this issue Sep 13, 2022 · 9 comments
Labels

Comments

@bpinto
Copy link

bpinto commented Sep 13, 2022

Please avoid duplicates

Reproducible test case

https://runkit.com/bpinto/nock-nock-2398

Nock Version

13.2.9

Node Version

16.10.0

TypeScript Version

No response

What happened?

Calling nock.abortPendingRequests() does a call to clearImmediate internally, but this is not always available so an error is thrown.

Error: Uncaught [ReferenceError: clearImmediate is not defined]

See:

https://developer.mozilla.org/en-US/docs/Web/API/Window/clearImmediate
testing-library/dom-testing-library#914

P.S.: Reproducible test case is hacky since the runkit environment does contain clearImmediate function and I had to delete it manually.

Would you be interested in contributing a fix?

  • yes
@bpinto bpinto added the bug label Sep 13, 2022
@gr2m
Copy link
Member

gr2m commented Sep 13, 2022

The MDN page says it's implemented in Node.js 0.10+. I'm pretty sure it exists in 16.10.0. Not sure what's happening?

@bpinto
Copy link
Author

bpinto commented Sep 13, 2022

I think it's because tests are running inside jsdom and not node and jsdom doesn't implement it. I'll see if I can create a reproducible scenario.

@bpinto
Copy link
Author

bpinto commented Sep 13, 2022

Tested the following code on: https://npm.runkit.com/jsdom

const jsdom = require("jsdom");
const { JSDOM } = jsdom;

const { window } = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
console.log({ clearImmediate: window.clearImmediate})
console.log({ setInterval: window.setInterval})

Output:

Object {clearImmediate: undefined}
Object {setInterval: function()}

@gr2m
Copy link
Member

gr2m commented Sep 13, 2022

yeah it would be great to get a failing test that we can build upon. But it seems that should be fixed in jsdom instead of nock?

@bpinto
Copy link
Author

bpinto commented Sep 13, 2022

It does not seem like this method is/will be available on any modern browser and from what I've seen on jsdom repository, I do not believe they would add support for a deprecated feature, below is a note from MDN.

Note: This method is not expected to become standard, and is only implemented by recent builds of Internet Explorer and Node.js 0.10+. It meets resistance both from Gecko (Firefox) and Webkit (Google/Apple).

If you are okay with a function existence check, I could work on a PR.

@pastelsky
Copy link

pastelsky commented Dec 14, 2022

But it seems that should be fixed in jsdom instead of nock

This was explicitly removed from JSDom because it isn't a browser standard. Most of the other libraries that have been affected by this have used an alternative to setImmediate

@niels-van-den-broeck
Copy link

jestjs/jest#11222

Running into the same issue, related to the pr above. Not sure why overriding node globals for a process running in Node was chosen but here we are.

@de-perotti
Copy link

a workaround I'm using is to set:

global.setImmediate = setTimeout as any;
global.clearImmediate = clearTimeout as any;

@mikicho
Copy link
Contributor

mikicho commented Feb 6, 2024

As written in the README file, nock is currently specific for Node.js, it may change in the future (#2517).
Feel free to repoen.

@mikicho mikicho closed this as completed Feb 6, 2024
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

6 participants