Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest - Unexpected token import

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

Unexpected token import

Here are my babel presets:

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

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

like image 519
red house 87 Avatar asked Dec 08 '17 19:12

red house 87


1 Answers

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.

like image 167
Andrea Carraro Avatar answered Sep 20 '22 13:09

Andrea Carraro