Jest: TypeError: Cannot redefine property: inert

Created on 24 Apr 2019  路  3Comments  路  Source: facebook/jest

馃悰 Bug Report

When importing the WICG Inert Polyfill in a react component that we test with Jest, our Jest tests started failing with the following error message:

TypeError: Cannot redefine property: inert

This seems to be triggered by the WICG Inert polyfill using Object.defineProperty on the Element prototype

I believe this is an issue with JSDOM but is manifesting when using Jest. I tried setting the --no-cache flag when running Jest to no avail.

To Reproduce

I'm working on creating a repo to reproduce this issue.
This should be reproducible by creating a react component that imports WICG Inert Polyfill and then trying to test it with Jest.

Expected behavior

A way to use the inert polyfill with Jest is documented.

Link to repl or repo (highly encouraged)

I'm working on creating a repo to reproduce this issue and will post a link once I get the reduced test case put together.

Run npx envinfo --preset jest

  System:
    OS: macOS Sierra 10.12.6
    CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
  Binaries:
    Node: 8.11.3 - ~/.nvm/versions/node/v8.11.3/bin/node
    Yarn: 1.12.3 - /usr/local/bin/yarn
    npm: 5.6.0 - ~/.nvm/versions/node/v8.11.3/bin/npm
  npmPackages:
    jest: ^23.1.0 => 23.6.0
Bug Report Needs Repro Needs Triage

Most helpful comment

The Element global comes from JSDOM as mentioned in the OP, so I think it should be reported there instead of here. If we're doing weird stuff in Jest (we've been known to), happy to reopen and fix it here!

All 3 comments

I created an issue on the inert package to only define the property if it hasn't been defined. I'm not sure if that's the solution for Jest tests or just something that couldn't hurt.

https://github.com/WICG/inert/issues/122

The Element global comes from JSDOM as mentioned in the OP, so I think it should be reported there instead of here. If we're doing weird stuff in Jest (we've been known to), happy to reopen and fix it here!

Reproduction without Jest:

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

const { window } = new JSDOM("");

function polyfill() {
  Object.defineProperty(window.Element.prototype, "inert", {
    enumerable: true,
    get: function() {},
    set: function() {}
  });
}

polyfill();
polyfill();

Suboptimal that it seems the prototype leaks across tests...

Was this page helpful?
0 / 5 - 0 ratings