react-router-native with Jest

Created on 23 Mar 2017  ·  2Comments  ·  Source: ReactTraining/react-router

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.

Version

react-router-native 4.0.0

Steps to reproduce

  1. Create React Native app:
    react-native init MyApp cd MyApp yarn add react-router-native
  2. Wrap the default application component with <NativeRouter/> component
  3. Run yarn test

Expected Behavior

Tests pass

Actual Behavior

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.

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-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.

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davetgreen picture davetgreen  ·  3Comments

stnwk picture stnwk  ·  3Comments

wzup picture wzup  ·  3Comments

yormi picture yormi  ·  3Comments

andrewpillar picture andrewpillar  ·  3Comments