React-native-router-flux: Jest testing problem: Invariant Violation: Native module cannot be null.

Created on 7 Mar 2017  路  1Comment  路  Source: aksonov/react-native-router-flux

Version

Tell us which versions you are using:

  • react-native-router-flux v3.38.0
  • react-native v0.42.0
  • jest 19.0.2

Expected behaviour

Test suite should NOT fail to run

Actual behaviour

Test suite failed to run

    Invariant Violation: Native module cannot be null.

      at invariant (node_modules/fbjs/lib/invariant.js:44:15)
      at Linking.NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:32:1)
      at new Linking (node_modules/react-native/Libraries/Linking/Linking.js:119:141)
      at Object.<anonymous> (node_modules/react-native/Libraries/Linking/Linking.js:191:16)
      at Object.get Linking [as Linking] (node_modules/react-native/Libraries/react-native/react-native.js:90:22)
      at Object.<anonymous> (node_modules/react-native-experimental-navigation/NavigationRootContainer.js:15:36)

Explicit line breaking test: import { Actions } from 'react-native-router-flux' (when deleted and replaced with arrow function - doesn't happen:
<TouchableNativeFeedback onPress={Actions.SecondScene}>
into
<TouchableNativeFeedback onPress={() => {}}>

Test itself:

import 'react-native'
import React from 'react'
import FirstScene from '../index'

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer'

it('renders correctly', () => {
  const tree = renderer.create(
    <FirstScene />
  )

  expect(tree).toBeDefined()
})

Steps to reproduce

  1. Create standard react-native component,
  2. Import Actions,
  3. run jest or npm run test

Thank you for awesome library :) Reporting this issue because I care about growth and well-being of it.

Most helpful comment

For all struggling with this issue, here's the solution:

  • Create __mock__ folder in ur app directory.
  • Inside of it, create file called react-native-router-flux.js
  • (Basic) inside of it type: export const Actions = {} and save.

Jest will use provided mock and this will resolve the issue, therefore it will be sufficient for all cases where you imported Actions - in any child etc.

I don't know Jest very well but I image this could be potentially the place for stubs and spys :)

>All comments

For all struggling with this issue, here's the solution:

  • Create __mock__ folder in ur app directory.
  • Inside of it, create file called react-native-router-flux.js
  • (Basic) inside of it type: export const Actions = {} and save.

Jest will use provided mock and this will resolve the issue, therefore it will be sufficient for all cases where you imported Actions - in any child etc.

I don't know Jest very well but I image this could be potentially the place for stubs and spys :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vinayr picture vinayr  路  3Comments

VictorK1902 picture VictorK1902  路  3Comments

sylvainbaronnet picture sylvainbaronnet  路  3Comments

basdvries picture basdvries  路  3Comments

wootwoot1234 picture wootwoot1234  路  3Comments