Skip to content

globalThis is not defined on Node 10 #2961

@ianschmitz

Description

@ianschmitz

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions