简体   繁体   English

当节点模块使用es6语法(create-react-app)时,Jest无法运行

[英]Jest fails to run when node modules have es6 syntax (create-react-app)

I have a create-react-app project and trying to unit test office-ui-fabric-react component using Jest and Enzyme . 我有一个create-react-app项目,并尝试使用JestEnzyme office-ui-fabric-react组件进行单元测试。

The latest version of office-ui-fabric-react use es6 syntax and jest is failing to run the test. 最新版本的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);
    });
});

This is the package.json file coming from create-react-app with few additions 这是来自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"
  }
}

Error 错误 开玩笑的错误

create-react-app is not allowing me to specify ani options for jest in package.json without ejecting. create-react-app不允许我在package.jsonjest指定ani选项而不弹出。

After looking at the suggestions in the comments above, the following set worked for me: 在查看了以上评论中的建议之后,以下设置对我有用:

{
  "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"
  }
}

In my test file sample-tests.jsx 在我的测试文件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);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM