I'm not sure if I am doing something wrong, but I can't find any information on where to look or what to do to fix this. It may just be that I should not expect to use the router in Jest and should be mocking it with a MemoryRouter, but the error makes it seem like there's something wrong with transpiling ES6 module syntax for React Native.
react-router-native 4.0.0
react-native init MyApp
cd MyApp
yarn add react-router-native
<NativeRouter/> componentyarn testTests pass
yarn test v0.21.3
$ jest
FAIL __tests__/index.android.js
● Test suite failed to run
/Volumes/Projects/MyApp/node_modules/react-router-native/main.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from 'react-router'
^^^^^^
SyntaxError: Unexpected token export
at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:320:12)
at Object.<anonymous> (index.android.js:15:24)
at Object.<anonymous> (__tests__/index.android.js:3:19)
FAIL __tests__/index.ios.js
● Test suite failed to run
/Volumes/Projects/MyApp/node_modules/react-router-native/main.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from 'react-router'
^^^^^^
SyntaxError: Unexpected token export
at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:320:12)
at Object.<anonymous> (index.ios.js:15:24)
at Object.<anonymous> (__tests__/index.ios.js:3:15)
Test Suites: 2 failed, 2 total
Tests: 0 total
Snapshots: 0 total
Time: 1.038s, estimated 2s
Ran all test suites.
error Command failed with exit code 1.
As is typical, as soon as I filed this I figured out what to do to get around the issue. I needed to add react-router-native to Jest's transformIgnorePatterns configuration:
"jest": {
"preset": "react-native",
"transformIgnorePatterns": [
"node_modules/(?!react-native|react-router-native)/"
]
}
Leaving this open in case this should need to be documented, or maintainers may know more about whether there is an underlying issue that could be addressed to prevent needing this config change.
That's odd, because we don't have to do that in the library. Sounds like some other configuration might need to be changed. That would be outside the scope of this repo though.
Most helpful comment
As is typical, as soon as I filed this I figured out what to do to get around the issue. I needed to add
react-router-nativeto Jest'stransformIgnorePatternsconfiguration:Leaving this open in case this should need to be documented, or maintainers may know more about whether there is an underlying issue that could be addressed to prevent needing this config change.