0

I need some help. I am trying to use Mocha for testing in a react/readux application. Unfortunately there seems to be a problem with compiling the code to es5, because I get this error:

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 

I followed this blog entry closely to set-up the test environment: http://jamesknelson.com/testing-in-es6-with-mocha-and-babel-6/

Also, I created a .babelrc with this entries:

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

This is my 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"
  }
}

I absolutely have no idea what I did wrong. Any input is very welcome.

EDIT: I tried the solutions in the duplicate thread and installed the following packages:

chai-jquery babel-plugin-transform-object-rest-spread babel-preset-stage-2

and changed my .babelrc to

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

I now get this error message when trying to run npm test:

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

TypeError: _jsdom2.default.jsdom is not a function
    at Object.<anonymous> 
Gh05d
  • 7,923
  • 7
  • 33
  • 64

0 Answers0