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);
});