Reputation: 497
My testing with jest failing when I add state to the component. Im trying to get s snaphot, and without state everything works so I think probles in reading latest react "style" of declaring the state. I think Im missing some kind of babel library or something.
Jest:
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 you r 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
Console:
> [email protected] test C:\test
> jest
FAIL src/App.test.js
? Test suite failed to run
C:/test/src/App.js: Unexpected token (5:6)
Jest encountered an unexpected token
Details:
3 |
4 | class App extends Component {
> 5 | state={
| ^
6 | value: 'value'
7 | }
8 | render() {
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 8.307s
Ran all test suites.
npm ERR! Test failed. See above for more details.
package.json
{
"name": "boilertest",
"version": "0.1.0",
"private": true,
"babel": {
"presets": [
"es2015",
"react"
]
},
"jest": {
"snapshotSerializers": [
"<rootDir>/node_modules/enzyme-to-json/serializer"
],
"moduleNameMapper": {
"^.+\\.(css|scss)$": "identity-obj-proxy"
}
},
"dependencies": {
"axios": "^0.18.0",
"bootstrap": "^4.1.3",
"react": "^16.6.0",
"react-dom": "^16.6.0",
"react-router-dom": "^4.3.1",
"react-scripts": "2.0.5"
},
"devDependencies": {
"babel-core": "^6.26.3",
"babel-jest": "^23.6.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"enzyme": "^3.7.0",
"enzyme-adapter-react-16": "^1.6.0",
"enzyme-to-json": "^3.3.4",
"identity-obj-proxy": "^3.0.0",
"jest": "^23.6.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "jest --watch",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
Thank you!
Upvotes: 0
Views: 965
Reputation: 187
You need to add what danmakenoise suggested, npm i babel-plugin-transform-class-properties --save
. This plugin transforms the experimental state = {}
into constructor(){}
syntax, prior to running Jest.
Once you've added this plugin, you need to update your .babelrc file to include the plugin: "plugins": ["transform-class-properties"]
.
Upvotes: 0
Reputation: 294
Looks like you just need to install https://www.npmjs.com/package/babel-plugin-transform-class-properties
Defining the state object like that is not in the current standard, and this will transpile it for you!
Upvotes: 1