【问题标题】:Jest fails to run when node modules have es6 syntax (create-react-app)当节点模块具有 es6 语法(create-react-app)时,Jest 无法运行
【发布时间】:2018-12-05 01:23:41
【问题描述】:

我有一个create-react-app 项目并尝试使用JestEnzymeoffice-ui-fabric-react 组件进行单元测试。

最新版本的office-ui-fabric-react 使用es6 语法和jest 无法运行测试。

import { mount } from "enzyme";
import React from "react";
import { MessageBar, MessageBarType } from "office-ui-fabric-react/lib/MessageBar";

describe("<MessageBar />", () => {
    it("renders message bar correctly", () => {
        const wrapper = mount(<MessageBar messageBarType={MessageBarType.success} />);
        expect(wrapper.find('.ms-MessageBar--success').length).toEqual(1);
    });
});

这是来自 create-react-app 的 package.json 文件,添加了一些内容

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "office-ui-fabric-react": "^6.110.1",
    "react": "^16.6.3",
    "react-dom": "^16.6.3",
    "react-scripts": "2.1.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "devDependencies": {
    "@types/enzyme": "3.1.5",
    "@types/enzyme-adapter-react-16": "1.0.1",
    "@types/jest": "23.0.0",
    "@types/react": "16.3.16",
    "@types/react-dom": "16.0.5",
    "@types/react-test-renderer": "^16.0.0",
    "enzyme": "^3.7.0",
    "enzyme-adapter-react-16": "^1.7.0",
    "jest": "^23.6.0"
  }
}

错误

create-react-app 不允许我在不弹出的情况下在package.json 中为jest 指定ani 选项。

【问题讨论】:

标签: javascript reactjs npm jestjs enzyme


【解决方案1】:

在查看了上面 cmets 中的建议后,以下设置对我有用:

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "office-ui-fabric-react": "^6.110.1",
    "react": "^16.6.3",
    "react-dom": "^16.6.3",
    "react-scripts": "2.1.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "devDependencies": {
    "babel-core": "^7.0.0-bridge.0",
    "babel-jest": "^23.6.0",
    "enzyme": "^3.7.0",
    "enzyme-adapter-react-16": "^1.7.0",
    "jest": "^23.6.0"
  }
}

在我的测试文件sample-tests.jsx

import Enzyme from "enzyme";
import { mount } from "enzyme";
import React from "react";
import { MessageBar, MessageBarType } from "office-ui-fabric-react/lib-commonjs/MessageBar";

import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });

describe("<MessageBar />", () => {
    it("renders message bar correctly", () => {
        const wrapper = mount(<MessageBar messageBarType={MessageBarType.success} />);
        expect(wrapper.find('.ms-MessageBar--success').length).toEqual(1);
    });
});

【讨论】:

  • 如果使用create-react-app,这些都不是必需的。它支持开箱即用的 es 模块的玩笑。您根本不应该在自己的 package.json 中添加 jest 作为依赖项;它已经由 react-scripts 完成。不支持或不推荐覆盖,并且很可能会破坏默认行为:stackoverflow.com/a/39932457/1057157
  • 您的示例有效,因为您明确导入了包的 commonjs 版本(“office-ui-fabric-react/lib-commonjs/MessageBar”)。如果您import { MessageBar } from "office-ui-fabric-react",那么您可能会遇到同样的错误。
猜你喜欢
  • 2018-01-14
  • 1970-01-01
  • 2018-04-01
  • 2020-04-11
  • 2020-09-23
  • 2022-10-17
  • 1970-01-01
  • 2021-08-06
  • 2021-05-09
相关资源
最近更新 更多