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.
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.
A way to use the inert polyfill with Jest is documented.
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.
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
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.
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...
Most helpful comment
The
Elementglobal 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!