尝试使用Mocha进行测试时,Babel不会将es6编译为es5

时间:2017-05-22 21:28:33

标签: reactjs redux mocha babeljs

我需要一些帮助。我正在尝试使用Mocha在react / readux应用程序中进行测试。不幸的是,将代码编译为es5似乎存在问题,因为我收到了这个错误:

Unexpected token (13:15)
  11 |   switch (action.type) {
  12 |     case types.ADD_USER:
> 13 |       return { ...state, list: [ ...state.list, action.payload ] };
     |                ^
  14 | 
  15 |     case types.DELETE_USER:
  16 |       let deletedState = state.list.filter(user => user.id !== action.payload);
    at Parser.pp$5.raise 

我密切关注此博客条目以设置测试环境:http://jamesknelson.com/testing-in-es6-with-mocha-and-babel-6/

另外,我用这个条目创建了一个.babelrc:

"presets": ["es2015", "react"]

这是我的package.json:

{
  "name": "react_frontend",
  "version": "0.1.1",
  "private": true,
  "devDependencies": {
    "babel-core": "^6.24.1",
    "babel-polyfill": "^6.22.0",
    "babel-preset-es2015": "^6.22.0",
    "babel-preset-react": "^6.22.0",
    "chai": "^3.5.0",
    "jasmine-core": "^2.5.2",
    "jsdom": "^10.1.0",
    "karma": "^1.3.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.1.0",
    "mocha": "^3.2.0",
    "react-scripts": "0.7.0",
    "react-test-renderer": "^15.4.2",
    "redux-devtools": "^3.3.2",
    "redux-logger": "^2.8.1",
    "sass-loader": "^4.1.1"
  },
  "dependencies": {
    "axios": "^0.15.3",
    "babel": "^6.5.2",
    "bootstrap": "^3.3.7",
    "chart.js": "^2.5.0",
    "circular-json": "^0.3.1",
    "expect.js": "^0.3.1",
    "express": "^4.14.1",
    "faker": "^4.1.0",
    "immutable": "^3.8.1",
    "lodash": "^4.17.4",
    "mocha": "^3.2.0",
    "node-sass": "^4.5.0",
    "react": "^15.4.2",
    "react-bootstrap": "^0.30.7",
    "react-chartjs-2": "^2.1.0",
    "react-collapsible": "^1.3.0",
    "react-dom": "^15.4.1",
    "react-dropzone": "^3.12.0",
    "react-redux": "^4.4.6",
    "react-router": "^3.0.0",
    "react-router-redux": "^4.0.7",
    "react-slick": "^0.14.6",
    "react-tooltip": "^3.3.0",
    "redux": "^3.6.0",
    "redux-form": "^6.5.0",
    "redux-logger": "^2.8.1",
    "redux-promise": "^0.5.3",
    "redux-router": "^2.1.2",
    "redux-thunk": "^2.1.0",
    "sass-loader": "^4.1.1",
    "style-loader": "^0.13.1",
    "superagent": "^3.5.0",
    "webpack": "^1.13.3"
  },
  "scripts": {
    "start": "node server.js",
    "deploy": "npm run build && gcloud app deploy  --project vipfy-148316",
    "start-local": "react-scripts start",
    "build": "react-scripts build",
    "test": "mocha --compilers js:babel-core/register",
    "test:watch": "npm test -- --watch",
    "eject": "react-scripts eject"
  }
}

我绝对不知道我做错了什么。任何意见都非常欢迎。

修改 我在重复的线程中尝试了解决方案并安装了以下包:

齐的jquery 巴贝尔-插件变换对象,其余的扩频 巴别预置级-2

并将我的.babelrc更改为

{
  "presets": ["es2015", "react", "stage-2"],
  "plugins": ["transform-object-rest-spread"]
}

我现在在尝试运行npm test时收到此错误消息:

global.document = _jsdom2.default.jsdom('<!doctype html><html><body></body></html>');
                                  ^

TypeError: _jsdom2.default.jsdom is not a function
    at Object.<anonymous> 

0 个答案:

没有答案