简体   繁体   English

开玩笑-意外的令牌导入

[英]Jest - Unexpected token import

I'm trying to learn how to do unit testing with React and jest. 我正在尝试学习如何使用React和Jest进行单元测试。 I've run into the following error: 我遇到了以下错误:

Unexpected token import 意外的令牌导入

Here are my babel presets: 这是我的babel预设:

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

Here is the file i'm trying to test: 这是我要测试的文件:

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() {} });

and here is my package.json: 这是我的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"
}

Have no idea why its giving me this message? 不知道为什么会给我这个信息吗?

It looks like babel-jest is missing among your dependencies. 您的依赖项中似乎缺少babel-jest That's why jest is not running babel on your ES6+ code before executing tests. 这就是为什么在执行测试之前, jest不会在ES6 +代码上运行babel的原因。

I'm pretty sure you need to add your babel presets to test as well, jest sets the env variable to test. 我敢肯定,您还需要添加babel预设来进行测试,jest将env变量设置为要进行测试。

So... 所以...

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

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

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