Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected import token - testing React Native with Jest

I'm getting a weird import error when I try to test my react-native app with jest. I have babel-jest, babel-preset-react-native, jest, jest-react-native and react-test-render all installed but get this error message when I run npm test.

● Test suite failed to run

/Users/maftalion/www/stars20/kiosk/node_modules/native-base/index.js:4
import Drawer from './Components/vendor/react-native-drawer';
^^^^^^
**SyntaxError: Unexpected token import**

  at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:316:10)
  at Object.<anonymous> (src/routes/Identification.js:3:17)
  at Object.<anonymous> (src/routes/router.js:4:21)

Test Suites: 1 failed, 1 passed, 2 total Tests: 1 passed, 1 total Snapshots: 1 passed, 1 total Time: 1.011s

like image 635
Matt Aft Avatar asked Nov 10 '16 17:11

Matt Aft


2 Answers

Figured it out, basically throw any node modules that use es6 syntax in transformIgnorePatterns.

"transformIgnorePatterns": [
   "node_modules/(?!react-native|native-base|react-clone-referenced-element)"
],
like image 145
Matt Aft Avatar answered Oct 26 '22 17:10

Matt Aft


Try adding transformIgnorePatterns to your package.json:

{
  "name": "MyAwesomeApp",
  ...
  "jest": {
    "transformIgnorePatterns": ["/node_modules/"]
  }
}

For me Jest works out of the box, however:

$ react-native init MyAwesomeApp
$ cd MyAwesomeApp
$ npm test
...
Tests:       2 passed

I'm using React Native v0.37.0.

like image 38
Martin Konicek Avatar answered Oct 26 '22 17:10

Martin Konicek