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

globalThis is not defined on Node 10 #2961

Closed
ianschmitz opened this issue May 6, 2020 · 4 comments
Closed

globalThis is not defined on Node 10 #2961

ianschmitz opened this issue May 6, 2020 · 4 comments

Comments

@ianschmitz
Copy link

We're currently running into issues upgrading Create React App to use the latest versions of Jest and jsdom (26 and 16 respectively). When running our test suite on Node 10.20.1, we receive this error:

    ReferenceError: globalThis is not defined

      at evalmachine.<anonymous>:1:1
      at setupWindow (node_modules/jsdom/lib/jsdom/browser/Window.js:51:55)
      at new Window (node_modules/jsdom/lib/jsdom/browser/Window.js:107:3)
      at exports.createWindow (node_modules/jsdom/lib/jsdom/browser/Window.js:38:10)
      at new JSDOM (node_modules/jsdom/lib/api.js:36:20)

After digging in with @SimenB, we discovered that it appears to potentially be caused by a core-js polyfill of globalThis (we're still investigating if we can work around in some way).

Here is a repro as copied from @SimenB's comment here: #2795 (comment) (thanks for digging into the issue with me Simen!)

FWIW, this reproduces the issue:

// polyfill globalThis for older nodes
globalThis = global;
const { JSDOM } = require('jsdom');
new JSDOM('', {runScripts: 'dangerously'});
Setting globalThis to anything at all will reproduce, I just picked global to be somewhat realistic. An even more realistic polyfill might be require('core-js'); which will also break, and might (I haven't verified) be inserted by @babel/preset-env and/or @babel/runtime.

Not sure it's necessarily something that needs to be fixed/changed in JSDOM, but it might be made more robust*? Anyways, hopes this helps folks hitting this issue to track it down. It's not Jest itself that does this.

*) e.g. filtering on vm.runInContext('this', windowInstance) rather than global in the outer context. This might not be feasible, I haven't played with it.

poteirard pushed a commit to ZopaPublic/react-components that referenced this issue May 13, 2020
@ianschmitz
Copy link
Author

Update: I've found what is polyfilling global.globalThis in our case: https://github.com/microsoft/TypeScript/blob/27616dd523279a5d699d2154edb4de6abc8234ad/src/services/globalThisShim.ts#L21

yhatt added a commit to marp-team/marp-core that referenced this issue Jun 8, 2020
Use Jest built-in jsdom environment if jsdom/jsdom#2961 was fixed.
yhatt added a commit to marp-team/marp-core that referenced this issue Jun 8, 2020
* Upgrade dependent packages to the latest version

* Bump @marp-team/marpit to v1.6.2

* Update cache key for CircleCI

* Setup globalThis shim

* Revert "Setup globalThis shim"

This reverts commit 805ed16.

* Apply workaround for JSDOM with Node 10

* Re-run `yarn upgrade --latest`

* Downgrade JSDOM for Jest environment to 15

Use Jest built-in jsdom environment if jsdom/jsdom#2961 was fixed.

* [ci skip] Update CHANGELOG.md

* [ci skip] Update CHANGELOG.md
vltansky added a commit to vltansky/browser-hrtime that referenced this issue Jun 18, 2020
@s0l0c0ding
Copy link

got the same issue on my angular blog was using "jsdom": "^16.2.2" and node 10, all went smoothly after updating node to 12

bjoluc added a commit to bjoluc/next-redux-cookie-wrapper that referenced this issue Jul 14, 2020
Jest 26 dropped support for Node 8. While Node 10 is still supported,
there's a bug with jsdom (jsdom/jsdom#2961).
urish added a commit to wokwi/avr8js that referenced this issue Aug 1, 2020
otherwise, node 10 is broken on CI. see jsdom/jsdom#2961 for details.
bors bot added a commit to meilisearch/meilisearch-js that referenced this issue Jan 20, 2021
755: Temp fix for failing node v10 r=bidoubiwa a=bidoubiwa

This fixes the failing tests until this has changed jsdom/jsdom#2961 

Tests were failing because of the latest version of `JSdom` that introduced this bug: jsdom/jsdom#2961. We only have the bug now as we have updated jest to its latest version. 

Multiple solutions are considerable: 
- Adding a resolution to a previous version of jsdom which works with yarn but not npm
- Add a polyfill for globalThis
- Remove node 10 from test until resolved: loilo/livy@83de8b1
- Change the JsDom version used by explicitly telling so in jest config (which is kinda what `create-react-app` (85k stars) did but [without using an external library](https://github.com/facebook/create-react-app/pull/8955/files#diff-2f231dbdc363c929e899c94ae0d999f9886fdc6e33fb88d498a6b101a4bf9f68R62))

I went for the last option.


Another thing to consider is that [Node 10 is almost EOL](https://github.com/nodejs/Release) (end of life)

We might consider dropping support in the futur
<img width="986" alt="Screenshot 2021-01-19 at 16 57 25" src="https://user-images.githubusercontent.com/33010418/105059332-7413e100-5a77-11eb-8845-48edc3c24382.png">






Co-authored-by: Charlotte Vermandel <charlottevermandel@gmail.com>
@bidoubiwa
Copy link

The solution to this is to use this library https://www.npmjs.com/package/jest-environment-jsdom which creates a jest jsdom environment with the version jsdom from any jest version.

For example, by using jest-environment-jsdom version 25, you will use the jsdom from this version. And in jest 25, the jsdom version was still compatible with node 10

// package.jsom
    "jest-environment-jsdom": "25.5",
// jest config
      displayName: 'dom',
      testEnvironment: 'jest-environment-jsdom',

@domenic
Copy link
Member

domenic commented Sep 7, 2023

Node v10 is no longer supported by jsdom, so closing.

@domenic domenic closed this as completed Sep 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants