开玩笑的js:包含的wicg-inert无法重新定义属性惰性

时间:2019-02-21 20:02:23

标签: javascript reactjs jestjs enzyme

我正在尝试在React Modal中进行简单的单元测试。如果安装时没有任何道具。这是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);
    }
 });

绝对不知道如何解决,找不到任何解释。

1 个答案:

答案 0 :(得分:0)

在package.json中添加:

“脚本”:{     “ start”:“ react-app-wired start”,     “ build”:“ react-app-rewired构建”,     “ test”:“反应脚本测试--env = jsdom --silent -无缓存”,     “ eject”:“反应脚本弹出”,     “ doc”:“ jsdoc -R src / README.md src / -r -d doc /”   }

在test.js中删除以下行:

import "wicg-inert";