我正在尝试在 React Modal 中进行简单的单元测试。如果它没有 Prop 安装。这是 test.js 文件。
import React from "react";
import { mount } from "enzyme";
import MyModal from ".";
import "wicg-inert";
import "animate.css";
describe("MyModal component", () => {
describe("When props is missing", () => {
it("should be defined", () => {
expect(() => mount(<MyModal />)).toBeDefined();
});
});
});
错误信息:
Test suite failed to run
TypeError: Cannot redefine property: inert
at Function.defineProperty (<anonymous>)
4 | import classnames from "classnames";
5 | import MyButton, { MyButtonGroup } from "components/MyButton";
> 6 | import "wicg-inert";
7 | import "animate.css";
8 |
9 | import "./MyModal.scss";
at node_modules/wicg-inert/dist/inert.js:794:10
at node_modules/wicg-inert/dist/inert.js:2:66
at Object.<anonymous> (node_modules/wicg-inert/dist/inert.js:5:2)
at Object.<anonymous> (src/components/MyModal/MyModal.jsx:6:1)
at Object.<anonymous> (src/components/MyModal/index.mjs:1:318)
at Object.<anonymous> (src/components/MyModal/MyModal.test.js:3:32)
在节点模块内的 inert.js 文件中。第一行是 792。
var inertManager = new InertManager(document);
Object.defineProperty(Element.prototype, 'inert', {
enumerable: true,
get: function get() {
return this.hasAttribute('inert');
},
set: function set(inert) {
inertManager.setInert(this, inert);
}
});
完全不知道如何解决,找不到任何解释。
最佳答案
在 package.json
中添加以下内容:
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-scripts test --env=jsdom --silent **--no-cache**",
"eject": "react-scripts eject",
"doc": "jsdoc -R src/README.md src/ -r -d doc/"
},
在 test.js
中删除行:
import "wicg-inert";
关于javascript - 开 Jest js : wicg-inert included cannot redefine property inert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54815338/