Nikita Ryanov
Nikita Ryanov

Reputation: 1570

React app: Jest encountered an unexpected token

I try to test my app created via create-react-app. There is only one test file generated automatically:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

it('renders without crashing', () => {
  const div = document.createElement('div');
  ReactDOM.render(<App />, div);
  ReactDOM.unmountComponentAtNode(div);
});

This is my package.json (also i have package-lock.json (~ 15k rows)):

   {
      "name": "project",
      "version": "0.1.0",
      "private": true,
      "dependencies": {
        "avro-js": "^1.9.1",
        "axios": "^0.19.0",
        "bootstrap": "^4.3.1",
        "jquery": "^3.4.1",
        "prop-types": "^15.7.2",
        "react": "^16.10.2",
        "react-dom": "^16.10.2",
        "react-redux": "^7.1.1",
        "react-scripts": "3.2.0",
        "react-syntax-highlighter": "^11.0.2",
        "reactstrap": "^8.0.1",
        "redux": "^4.0.4",
        "redux-thunk": "^2.3.0"
      },
      "scripts": {
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject"
      },
      "eslintConfig": {
        "extends": "react-app"
      },
      "browserslist": {
        "production": [
          ">0.2%",
          "not dead",
          "not op_mini all"
        ],
        "development": [
          "last 1 chrome version",
          "last 1 firefox version",
          "last 1 safari version"
        ]
      }
    }

When i try to test it using test script i get this error:

Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Details:

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export

{ default as a11yDark } from './a11y-dark';

Also, i don't have .babelrc file. I tried to add .babelrc but it didn't help (or maybe i did it incorrectly)

Upvotes: 3

Views: 1954

Answers (2)

Corckole
Corckole

Reputation: 921

If you are getting this error when trying to import the javascript styles for the component, make sure you are targetting the

react-syntax-highlighter/dist/cjs/...

instead of the

react-syntax-highlighter/dist/esm/...

Jest should be able to parse that.

Upvotes: 4

Nikita Ryanov
Nikita Ryanov

Reputation: 1570

So, finally i've solved this. My final configs are:

babel.config.js:

module.exports = {
    presets: ['@babel/preset-env', '@babel/preset-react'],
};

package.json:

{
  "name": "project",
  "version": "1.0",
  "private": true,
  "dependencies": {
    "avro-js": "^1.9.1",
    "axios": "^0.19.0",
    "bootstrap": "^4.3.1",
    "jquery": "^3.4.1",
    "prop-types": "^15.7.2",
    "react": "^16.10.2",
    "react-dom": "^16.10.2",
    "react-redux": "^7.1.1",
    "react-scripts": "3.2.0",
    "react-syntax-highlighter": "^11.0.2",
    "reactstrap": "^8.0.1",
    "redux": "^4.0.4",
    "redux-thunk": "^2.3.0"
  },
  "jest": {
    "transformIgnorePatterns": [
      "/node_modules/(?!(react-syntax-highlighter)/)"
    ]
  },
  "devDependencies": {
    "@babel/cli": "^7.0.0",
    "@babel/core": "^7.0.0",
    "@babel/preset-env": "^7.0.0",
    "@babel/preset-react": "^7.0.0",
    "babel-core": "^7.0.0-bridge.0",
    "babel-eslint": "^10.0.3",
    "babel-jest": "^24.9.0",
    "babel-loader": "^8.0.0",
    "enzyme": "^3.10.0",
    "enzyme-adapter-react-16": "^1.15.1",
    "eslint": "^6.7.1",
    "jest": "^24.9.0",
    "jest-cli": "^24.9.0",
    "react-test-renderer": "^16.12.0",
    "redux-mock-store": "^1.5.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

I'm not sure that this is the best solution, but at least it works

Upvotes: 2

Related Questions