Jest - 意外的令牌导入

时间:2017-12-08 19:38:03

标签: javascript reactjs jestjs

我正在尝试学习如何使用React和jest进行单元测试。我遇到了以下错误:

  

意外的令牌导入

以下是我的babel预设:

{
  "presets": ["es2015", "stage-0", "react"],  
  "env": {
    "test": {
        "plugins": ["transform-es2015-modules-commonjs"]
    }
}
}

以下是我试图测试的文件:

import jest from 'jest';
import React from 'react'; 
import { shallow } from 'enzyme';
import Step from '../src/components/step.js';

const wrapper = shallow(<Step />);
wrapper.find('a').simulate('click', { preventDefault() {} });

这是我的package.json:

{
  "name": "react-simple-boilerplate",
  "version": "1.0.0",
  "description": "Simple and lightweight Boilerplate for ReactJS projects",
  "scripts": {
    "lint": "eslint src",
    "start": "node server.js",
    "test": "jest"
  },
  "devDependencies": {
    "babel-core": "^6.24.1",
    "babel-loader": "^7.0.0",
    "babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "css-loader": "^0.28.0",
    "enzyme": "^3.2.0",
    "enzyme-adapter-react-16": "^1.1.0",
    "eslint": "^3.19.0",
    "eslint-plugin-react": "^6.10.3",
    "jest": "^21.2.1",
    "node-sass": "^4.5.2",
    "react-addons-test-utils": "^15.6.2",
    "sass-loader": "^6.0.3",
    "style-loader": "^0.16.1",
    "webpack": "^2.4.1",
    "webpack-dev-server": "^2.4.4"
  },
  "dependencies": {
    "babel-loader": "^7.0.0",
    "react": "^15.5.4",
    "react-dom": "^15.5.4"
  },
  "main": "server.js"
}

不知道为什么给我这个消息?

2 个答案:

答案 0 :(得分:5)

您的依赖项中似乎缺少babel-jest。这就是jest在执行测试之前未在ES6 +代码上运行babel的原因。

答案 1 :(得分:0)

我很确定你需要添加你的babel预设来测试,jest设置env变量进行测试。

因此...

"test": {
    "plugins": ["transform-es2015-modules-commonjs"],        
    "presets": ["es2015", "react", "stage-0"]
}