javascript - 开 Jest js : wicg-inert included cannot redefine property inert

标签 javascript reactjs jestjs enzyme

我正在尝试在 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/

相关文章:

javascript - 使用 jest.unmock 测试 Promise

vue.js - 如何使用 jest 使测试覆盖显示 Vue-cli 3 中的所有 vue 文件

react-native - 使用 react-native-testing-library 在 Jest 中未呈现 NativeBase 内容

javascript - 搜索并用 jQuery 替换

reactjs - 当父状态改变时组件卸载

javascript - 使用 Javascript 检查输入值的特定格式

javascript - 为什么我的组件的这一部分会在间隔调用时重新渲染?

javascript - 如何将带有字符串数组的 FormData 属性从 React Client 发送到 Django + Django Rest Framework API

javascript - Kml 文件显示高程剖面

javascript - Angular 6 : async await some variable got it variable