0

I have originally started with react-scripts test but Console.log is not behaving properly(it print messages sometimes) so i decided to move on to jest (console.log works with jest,also jest can support more than react-script test).

Now,I am getting below error.

FAIL src/App.test.js ● Test suite failed to run

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".

Here's what you can do:
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html

App.test.js: Unexpected token (11:20)

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

Environment this application is on windows 10 pro

node v10.9.0 npm 6.2.0

I have followed Does Jest support ES6 import/export? but it throws same error,what is stage 0 and why stage 0 is dangerous?

package.json file enter code here

{
"name": "my-ui",
"version": "0.1.0",
"private": true,
"dependencies": {
"node-sass": "^4.12.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^4.3.1",
"react-scripts": "^2.1.8"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"build-dev": "dotenv -e .env.development 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-jest": "^24.8.0",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"dotenv-cli": "^1.4.0",
"jest": "^24.8.0",
"jsx-to-string": "^1.4.0",
"react-test-renderer": "^16.8.6"
},
".babelrc": {
"presets": [
"es2015",
"react"
]
}
}

I have two test case- for this one of them is showing me expected behavious and other is throwing above error.

I have App.test.js file

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

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

//below should print output - working correctly.
it('it is printing', () => {
console.log('Hellllooo');
expect(true).toBe(true);
});
  • I addition, In app.js I am switching between modules,for example i have routes defined, how should I mock it or bypass – Visitor3457 Jul 17 '19 at 16:08
  • curruntly I am not able to start my application- using npm start,if i switch jest with react-script test and remove babel-dependency then it works. – Visitor3457 Jul 17 '19 at 18:00
  • this is resolved with - following link.https://www.codementor.io/vijayst/unit-testing-react-components-jest-or-enzyme-du1087lh8 make sure to have following - "babel": { "presets": [ "@babel/react", "@babel/env" ], "plugins": [ "@babel/plugin-proposal-class-properties" ] } – Visitor3457 Jul 18 '19 at 16:02

0 Answers0