-
Notifications
You must be signed in to change notification settings - Fork 457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
React Native - SyntaxError: Unexpected token import #604
Comments
I am having the same issue. Started happening when I tried to upgrade to RN 0.56 |
What version of Babel are you using? Babel 7 now handles |
Also see my comment in jestjs/jest#6229 |
@huafu, @kulshekhar this is blocking me use You can reproduce it on Upgrading
I assume this is because something in
So I then add the following to the
Still get the same error. I've tried all kinds of different transform patterns and other fruitless investigation. Not sure if this is a problem with |
Thank @mattyclarkson for the investigation. Here is some quick info I can give you know: First of all, when you try something else, be sure to run For babel, if you install babel 7, ts-jest will use it. Else it'll use the 6. For the repository link you gave, it's pointing to a no more existing hash. If the issue is in the master of this repo as well, lemme know and I'll try to investigate when I'll have a bit more time. |
Ooo, thanks for the tip. That helps. I've updated the link in the above comment, it was suppose to just point at |
I wrote step by step what I did, maybe it could help you investigate later some other bugs:
My time on this is over sorry, I'll let you follow here, maybe tomorrow or another day if you still stuck I'll continue to drop an eye, I hope these last few steps will help you |
Oh @mattyclarkson look at this facebook/react-native#19859 (comment) Also something to note: removing all |
Ahhhhh I must be too tired, going back to my project I realized I made a mistake in the ignore pattern...
no, it was wrong! So I fixed it: transformIgnorePatterns: [
"/node_modules/(?!react-native)/.+"
] ...because yeah, we want ti to NOT ignore processing the Now it complains about some React thing, so there you go that's not for me :-D
@mattyclarkson if you find out it's related to ts-jest, please re-open this issue. For others landing here, you might be interested in: |
Finally works fine! Jest config in package.json: {
"devDependencies": {
"babel-jest": "^23.6.0",
"jest": "^23.6.0",
"ts-jest": "^23.1.4",
"typescript": "^3.2.1"
...
},
"jest": {
preset: "react-native",
transform: {
"^.+\\.jsx?$": "<rootDir>/node_modules/react-native/jest/preprocessor.js",
"^.+\\.tsx?$": "ts-jest"
},
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
globals: {
"ts-jest": {
"tsConfigFile": "tsconfig.jest.json"
}
}
}
...
} TS config in tsconfig.json: {
"compilerOptions": {
// https://blogs.msdn.microsoft.com/typescript/2018/08/27/typescript-and-babel-7/
"target": "esnext",
"moduleResolution": "node",
"allowJs": true,
"noEmit": true,
"isolatedModules": true,
"esModuleInterop": true,
"noImplicitAny": true,
"allowSyntheticDefaultImports": true,
"jsx": "react-native",
"lib": [
"esnext"
],
"types": [ // fix declaration collision between react-native and ts-jest
"react",
"react-native",
"react-redux",
"redux"
...
]
},
"include": [
"src"
]
} TS config in tsconfig.jest.json: {
"extends": "./tsconfig",
"compilerOptions": {
"jsx": "react",
"module": "commonjs"
}
} Babel config in .babelrc: {
"presets": [
"module:metro-react-native-babel-preset"
],
"env": {
"test": {
"presets": [
"react-native",
[
"@babel/preset-env"
]
]
}
}
} Thanks 🙏 |
This answer saved my life!:
|
Issue
SyntaxError: Unexpected token import
Jest throw an error when he try to parse React Navigation package who has ES2015 imports.
Expected behavior
Following dedicated React Native documentation and ES2015 documentation I expect that
.js
files under node_modules to be processed bybabel-jest
.Jest config
Babel config
Error
Example
You can find an example with a simple test.
Thanks !
The text was updated successfully, but these errors were encountered: