I m trying to run a react app, created using create-react-app. App runs successfully, but when I try to run JEST it give me the following error. I have looked and tried various options but could not fix it. Here is my package.json and jest.config.json
please suggest on what I might be doing wrong
Thank you
A
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
Details:
SyntaxError: C:\Test\counter-app\src\App.test.js: Unexpected token (7:18)
5 | it('renders without crashing', () => {
6 | const div = document.createElement('div');
> 7 | ReactDOM.render(<App />, div);
| ^
8 | ReactDOM.unmountComponentAtNode(div);
9 | });
package.json
{
"name": "counter-app",
"version": "0.1.0",
"private": true,
"jest": {
"verbose": true,
"moduleDirectories": ["node_modules", "src"],
"transformIgnorePatterns": [
"node_modules/(?!(react-native|react-native-cookies)/)"
]
},
"dependencies": {
"bootstrap": "^4.3.1",
"react": "^16.11.0",
"react-dom": "^16.11.0",
"react-scripts": "3.2.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "jest --env=jsdom",
"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"
]
}
}
jest.config.js
const {defaults} = require('jest-config');
module.exports = {
"verbose": true,
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.(css|scss|less)$": "jest-css-modules",
".+\\.(css|styl|less|sass|scss)$": "<rootDir>/node_modules/jest-css-modules-transform"
},
"globals": {
"NODE_ENV": "test"
},
moduleFileExtensions: [...defaults.moduleFileExtensions, 'js', 'jsx'],
"moduleDirectories": [
"node_modules",
"src/frontend",
"src/shared"
]
};
Modified Package.json as follows:
{
"name": "foo",
"version": "0.1.0",
"private": true,
"dependencies": {
"jest-junit": "^9.0.0",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-scripts": "3.2.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"reporters": [ "default", "jest-junit" ],
"test": "react-scripts test --watchAll=false",
"testResultsProcessor": "jest-junit",
"eject": "react-scripts eject"
},
"jest-junit": {
"suiteName": "jest tests",
"outputDirectory": ".",
"outputName": "junit.xml",
"uniqueOutputName": "false",
"classNameTemplate": "{classname}-{hello}",
"titleTemplate": "{classname}-{helloTests}",
"ancestorSeparator": " › ",
"usePathForSuiteName": "true"
},
"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"
]
}
}
Try removing jest.config.js file completely and let it work with default config.
Related
I'm trying to build my react application, using Visual Studio Code, by tyiping
yarn run build
But the error that I get is the following
ERROR in ./src/index.js 8:4
Module parse failed: Unexpected token (8:4)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|
| ReactDOM.render(
> <App />,
| document.getElementById('root')
| );
As requested in the answer, here follows the code of my package.json (I removed some of the dependencies otherwise there was too much code for stack overflow in the question)
{
"name": "react-portfolio",
"homepage": "https://StonesCutter.github.io/react-portfolio",
"version": "0.1.0",
"private": true,
"dependencies": {
...
"yarn": "^1.22.19"
...
},
"scripts": {
"start": "react-scripts start",
"test": "react-scripts test",
"eject": "react-scripts eject",
"predeploy": "yarn run build",
"deploy": "gh-pages -d build",
"build": "webpack"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"gh-pages": "^4.0.0",
"webpack-cli": "^4.10.0"
}
}
Do you know how could I fix this problem?
Failed to compile
./src/index.js 1:39
Module parse failed: Unexpected token (1:39)
File was processed with these loaders:
./node_modules/#pmmmwh/react-refresh-webpack-plugin/loader/index.js
./node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
$RefreshRuntime$ = require('C:/Users/I'm-cx0140/Desktop/myapp/node_modules/react-refresh/runtime.js');
| $RefreshSetup$(module.id);
|
This error occurred during the build time and cannot be dismissed.
my package.json file
{
"name": "myapp",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.13.0",
"#testing-library/react": "^11.2.7",
"#testing-library/user-event": "^12.8.3",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
enter code here
Try downgrading react scripts-
Go into the package.json and changed the react-scripts to 4.0.1 and type npm install(terminal) and re-run it.
I am learning in React JS and facing below issue.
I am getting Unexpected token error after npm start. I have simple array in state object. I checked eslintConfig setting exists in package.json then still why this error?
error -
below is package.json settings -
{
"name": "react-app-1",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.12.0",
"#testing-library/react": "^11.2.6",
"#testing-library/user-event": "^12.8.3",
"bootstrap": "^4.1.1",
"i": "^0.3.6",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
it's pretty simple
modify the
this =>
let state = {
tags = ['tag1', 'tag2']
}
to =>
let state = {
tags : ['tag1', 'tag2']
}
You must use : inside an object for declaration
state = {
tags: ['tag1','tag2']
}
I am trying to make unit tests with Jest for my reactjs project.
When I want to test the rendering of a component, I am getting the following error :
SyntaxError: /Users/jk/EpitechProjects/T-WEB-700/CountOfMoney_18/front/src/tests/components/Navbar.test.js:
Support for the experimental syntax 'jsx' isn't currently enabled (9:29):
7 |
8 | test('Navbar : should render navbar', ()=>{
> 9 | const wrapper = shallow(<Navbar />);
| ^
10 | expect(wrapper.find('nav').length).toBe(1);
Add #babel/preset-react (https://git.io/JfeDR) to the 'presets' section of
your Babel config to enable transformation. If you want to leave it as-is,
add #babel/plugin-syntax-jsx (https://git.io/vb4yA) to the 'plugins' section to
enable parsing.
However, it does not seem to recognize the JSX syntax.
I have tried both #babel/preset-react and #babel/plugin-syntax-jsx suggestions , but the same error remains.
Here are a few of my config files :
jest.config.json
{
"setupFiles": [
"raf/polyfill",
"<rootDir>/src/setupTests.js"
],
"transform": {
"^.+\\.(js?)$": "babel-jest"
}
}
babel.config.js
module.exports = {
presets: [['#babel/preset-env', {targets: {node: 'current'}}]],
};
setupTests.js
//import '#testing-library/jest-dom';
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({
adapter: new Adapter()
});
package.json
{
"name": "front",
"version": "0.1.0",
"private": true,
"dependencies": {
...
"#testing-library/jest-dom": "^5.11.6",
"#testing-library/user-event": "^12.2.2",
...
"babel": "^6.23.0",
...
"react": "^17.0.1",
"react-dom": "^17.0.1",
...
"react-scripts": "4.0.0",
"react-test-renderer": "^17.0.1",
...
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"testJ": "jest --config=jest.config.json"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#babel/plugin-syntax-jsx": "^7.12.1",
"#babel/preset-react": "^7.12.10",
"#testing-library/react": "^11.2.2",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.5",
"jest": "^26.6.3"
The culprit at the moment seems to be your babel.config.js, you'll need to get it working with the #babel/present-react. So update as follows:
module.exports = {
presets: [
['#babel/preset-env', {targets: {node: 'current'}}],
['#babel/preset-react', {targets: {node: 'current'}}] // add this
]
};
I am new with ReactJS. I tried VSC and Sublime Text3 as my IDE. My OS is Windows 10, I installed NodeJS, Babel and added {"type": "module"} in package.json file. I cannot run or build JSX syntax. Below is a simple JSX file.
import React from 'react';
const Footer = () => {
return(
<footer>
this is my footer
</footer>
);
}
export default Footer;
When I run(build with node in sublime) this I receive an error, please help setup my IDE.
ExperimentalWarning: The ESM module loader is experimental.
file:///D:/myreact/src/components/Footer.js:6
<footer>
^
SyntaxError: Unexpected token '<'
at Loader.moduleStrategy (internal/modules/esm/translators.js:88:18)
at async link (internal/modules/esm/module_job.js:41:21)
Below is my package.json
{
"name": "myreact",
"version": "0.1.0",
"private": true,
"type": "module",
"dependencies": {
"cra-template": "1.0.3",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1"
},
"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"
]
}
}
Here is a screenshot of my sublime