*bug* Changing order of imports leads to SyntaxError
Below code runs fine. It also runs fine when you remove import 'react-native' line. But it breaks when you move around the imports so that the react-native is imported first.
'use strict'
import Realm from 'realm'
import 'react-native'
describe('Realm-RN', () => {
it('should pass', () => {
expect(true).toEqual(true)
})
})
The error is:
SyntaxError: Unexpected token import
at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:284:10)
at Object.<anonymous> (node_modules/realm/lib/index.js:30:24)
I'm not entirely sure if that's Jest problem (or not Babel or Realm-js), so excuse me if it's not.
Related issue for realm-js: https://github.com/realm/realm-js/issues/603
It seems like you need to update your preprocessorIgnorePathPatterns so that realm is also compiled properly. Add realm to the list here: http://facebook.github.io/jest/docs/tutorial-react-native.html#preprocessorignorepatterns-customization
@cpojer that's not the solution.
I am compiling realm already and the problem still exists. Here's my package.json jest section:
"jest": {
"preset": "jest-react-native",
"moduleNameMapper": {
"\\./datepicker$": "<rootDir>/node_modules/tcomb-form-native/lib/templates/bootstrap/datepicker.ios.js"
},
"preprocessorIgnorePatterns": [
"node_modules/(?!react-native|tcomb-form-native|apsl-react-native-button|react|realm)"
],
"testRegex": "(/__tests__/.*|\\.(test))\\.js$",
"collectCoverage": true,
"verbose": true,
"notify": true
},
I have the same problem
SyntaxError: Unexpected token import
at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:284:10)
at Object.<anonymous> (src/containers/App/App.js:13:23)
at Object.<anonymous> (src/containers/index.js:1:156)
"jest": {
"preset": "jest-react-native",
"preprocessorIgnorePatterns": [
"node_modules/(?!react-native|react-native-button)"
]
},
I had a similar "Unexpected token import" error, but but with a different stack trace. Mine was happening inside the 'realm' module (realm.io). However, it's likely related, simply a difference in what code I was running in attempting to run my test.
Making this change to my (react native) project's package.json file fixed it for me:
"jest": {
"preset": "react-native",
"transformIgnorePatterns": ["node_modules/(?!react-native|tcomb-form-native|apsl-react-native-button|react|realm)"]
},
Note that in doing so this also broke the standard index.android.js and index.ios.js files generated by "react native init ..." (which I don't use so don't care about). Don't know why, don't really have any understanding of what's going on here. Just posting a "this helped me" comment.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.