Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest encountered an unexpected token ReactJS

I have set up the following environment in my react project. Following is the package.json file:-

 {
  "name": "testing",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.5.2",
    "react-dom": "^16.5.2",
    "react-scripts": "2.0.5"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "jest",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-jest": "^23.6.0",
    "babel-loader": "^8.0.4",
    "babel-polyfill": "^6.26.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "enzyme": "^3.7.0",
    "enzyme-adapter-react-16": "^1.6.0",
    "jest": "^23.6.0",
    "react-test-renderer": "^16.5.2"
  },
  "jest": {
    "setupTestFrameworkScriptFile": "<rootDir>/src/setupTest.js",
    "transform": {
      "\\.js$": "babel-jest"
    },
    "verbose": true,
    "moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/client/assetsTransformer.js"
    },
    "moduleFileExtensions": [
      "js",
      "jsx"
    ],
    "moduleDirectories": [
      "node_modules"
    ]
  },
  "babel": {
    "presets": [
      "react",
      "es2015"
    ]
  }
}

Whenever I run npm test command it throws the following error

enter image description here

The component file which I am working on looks like the following:-

import React, { Component } from 'react';

class Increment extends Component {
    constructor() {
        super();
        this.state = {
          count: 0,
        }
      }

    makeIncrementer = amount => () =>
    this.setState(prevState => ({
        count: prevState.count + amount,
    }));

    increment = this.makeIncrementer(1);
    decrement = this.makeIncrementer(-1);

      render() {
        return (
          <div>
            <p>Count: {this.state.count}</p>
            <button className="increment" onClick={this.increment}>Increment count</button>
            <button className="decrement" onClick={this.decrement}>Decrement count</button>
          </div>
        )
      }
    }

export default Increment;

and the test case which I wrote is following:-

import React from 'react';
import { shallow } from 'enzyme';
import Increment from './increment';

describe('App component', () => {

    it('starts with a count of 0', () => {
        const wrapper = shallow(<Increment />);
        const text = wrapper.find('p').text();
        expect(text).toEqual('Count: 0');
    });
});

Could you please help me sort this problem out. I tried all the solutions on StackOverflow related to this particular topic but couldn't resolve it.

like image 350
Shwetank Avatar asked Oct 16 '22 11:10

Shwetank


2 Answers

I have the same problem for a few days and tried different solutions but they doesn't work for me. However mine was fixed by just modifying package.json to have "test": "react-scripts test" or replacing "jest" with "react-scripts test" under scripts just like this: package.json:

"scripts": {
    "test": "react-scripts test",
}
like image 123
Caleants Avatar answered Oct 27 '22 10:10

Caleants


The above problem can be solved by installing babel-plugin-syntax-class-properties

Followed by installing babel-plugin-transform-class-properties

Combining these two plugins helped me run the test cases successfully.

like image 22
Shwetank Avatar answered Oct 27 '22 11:10

Shwetank