Skip to content

"TypeError: Cannot redefine property: window" (v18.6.0) #47563

Description

Version

v18.16.0

Platform

21.6.0 Darwin Kernel Version 21.6.0: Mon Aug 22 20:17:10 PDT 2022; root:xnu-8020.140.49~2/RELEASE_X86_64 x86_64

Subsystem

No response

What steps will reproduce the bug?

const jsdom = new JSDOM('<!doctype html><html><body></body></html>');
global.window = jsdom.window as any;

How often does it reproduce? Is there a required condition?

Error is consistent. This error did not occur when using Node 18.15.0, and also does not occur when using Node 19.9.0.

What is the expected behavior? Why is that the expected behavior?

Expected behavior is no error.

What do you see instead?

TypeError: Cannot redefine property: window

Additional information

Same issue as: #47310

Similar to that issue, I've run into this error when running tests that use JSDom:

  1. Save the following as "test.js":
/**
 * @jest-environment jsdom
 */

"use strict"
global.window = {};

describe("test", () => {
    it("runs", () => {
        expect(1 + 1).toEqual(2);
    });
});
  1. Run npm i jest jest-environment-jsdom
  2. Run npx jest

Activity

  1. bnoordhuis commented on Apr 15, 2023

    @bnoordhuis
    Member

    That bug was fixed by #46615 but it hasn't been back-ported to v18.x yet. That happens automatically after some time so it'll likely show up in a release in the next few weeks. You can follow the linked pull request to get notified.

  2. added a commit that references this issue on Apr 27, 2023
  3. pre commented on May 4, 2023

    @pre

    That bug was fixed by #46615 but it hasn't been back-ported to v18.x yet. That happens automatically after some time so it'll likely show up in a release in the next few weeks. You can follow the linked pull request to get notified.

    @bnoordhuis are you aware whether the backport has already happened?

  4. bnoordhuis commented on May 4, 2023

    @bnoordhuis
    Member

    It's been back-ported to v19.x but not to v18.x yet. Give it a few more weeks.

  5. schinery commented on May 4, 2023

    @schinery

    It's been back-ported to v19.x but not to v18.x yet. Give it a few more weeks.

    Is odd as I've just tried bumping from 18.15.0 to 18.16.0 and now get this TypeError: Cannot redefine property: window in a test.

    Will try again when another v18.x is released.

  6. 14 remaining items

  7. Enet4 commented on Jun 27, 2023

    @Enet4

    The patch has already been staged for an upcoming 18.x release (comment):

    I landed this on v18.x-staging in fb90b6b

    Until then, sticking to Node.js v18.15.0 should work.

  8. Daniel8564 commented on Jun 29, 2023

    @Daniel8564

    @Daniel8564 I'm curious why do you need to do it? isn't window available?

    we have done like above to mock addEventListener:

    global.window = global;
    window.addEventListener = () => {};
    
  9. added a commit that references this issue on Jul 3, 2023
  10. arggrande commented on Jul 21, 2023

    @arggrande

    Hey folks, I can confirm this seems to have been fixed with the release of v18.17.0.

    I was in the process (ha) of doing an upgrade of our Node environments to 18.16.0, and hit this issue.

    the nullish coalescing operator (the ??=) workaround did work for me however after checking what the latest LTS was (being 18.17.0) I upgraded accordingly, removed the nullish coalescing operator from my tests, and it seems to work perfectly now.

    I'll update this comment if anything changes, but I think v18.17.0+ we're good to go!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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