Reputation: 1334
I am running jest test in react native and it fails giving "SyntaxError: Unexpected token import"
Here is my trimmer import code: -
import React, { Component } from 'react';
import { Container,
Content,
Text,
Button,
Icon,
Footer,
FooterTab,
Item,
Input,
View,
Picker
} from 'native-base';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
Here is error:
FAIL tests/home-page-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://facebook.github.io/jest/docs/en/configuration.html
Details:
/Users/myuser/sampleApp/node_modules/native-base-shoutem-theme/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import connectStyle from "./src/connectStyle";
^^^^^^
SyntaxError: Unexpected token import
1 | import React, { Component } from 'react';
> 2 | import {
| ^
3 | Header,
4 | Title,
5 | Button,
Not sure what I am doing wrong in import. Any tips are most welcomed.
Upvotes: 6
Views: 2862
Reputation: 7581
In two projects I upgraded from Angular 12 to 14 and 15. Both projects failed with the error message of the "unexpected token".
Did many jest config changes according to step-by-step guides. With transform ignore stuff as well.
Finally solved ALL this in both projects by just upgrading the 'rxjs' to a more recent version. The transform rules were not needed at all.
Both projects run well again!
My package dependencies are:
"@types/jest": "^29.2.5",
"jest": "^28.1.3",
"jest-preset-angular": "^12.2.3",
"ng-mocks": "^14.11.0",
"rxjs": "~7.5.0",
"tslib": "^2.3.0",
Upvotes: 0
Reputation: 397
I kwnow it's late, but for people that are still looking for a solution here is described how to fix this
Just add this snippet to your jest config file:
"jest": {
"preset": "react-native",
"transformIgnorePatterns": [
"node_modules/(?!react-native|native-base-shoutem-theme|@shoutem/animation|@shoutem/ui|tcomb-form-native)"
]
},
Upvotes: 0