Jest - 意外的令牌导入 [英] Jest - Unexpected token import

查看:17
本文介绍了Jest - 意外的令牌导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to learn how to do unit testing with React and jest. I've run into the following error:

意外的令牌导入

这是我的 babel 预设:

Here are my babel presets:

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

and here is my 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?

推荐答案

看起来像 babel-jest 在您的依赖项中丢失.这就是为什么 jest 在执行测试之前没有在 ES6+ 代码上运行 babel 的原因.

It looks like babel-jest is missing among your dependencies. That's why jest is not running babel on your ES6+ code before executing tests.

这篇关于Jest - 意外的令牌导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆