React-native-paper: ts-jest throws type error related to `NativeModules` type on ver 2.2.1

Created on 6 Nov 2018  路  5Comments  路  Source: callstack/react-native-paper


Current behaviour

  • Upgrade react-native-paper from 2.1.3 to 2.2.1 in a TypeScript project
  • Run tests using ts-jest
  • Tests fail with message:
Test suite failed to run

    TypeError: Cannot read property 'reactNativeVersion' of undefined

      2 | import { ScrollView, Text, TouchableOpacity, View } from 'react-native'
      3 | import { Navigation } from 'react-native-navigation'
    > 4 | import { Headline, Subheading } from 'react-native-paper'
        | ^
      5 |
      6 | import { Button } from 'components/Button'
      7 | import { SecureTextInput, TextInput } from 'components/TextInput'

      at Object.<anonymous> (node_modules/react-native-paper/src/components/Switch.js:10:60)
      at Object.<anonymous> (node_modules/react-native-paper/src/index.js:36:38)
      at Object.<anonymous> (src/screens/Auth/SignUp/SignUp.tsx:4:1)

Expected behaviour

  • Upgrade react-native-paper from 2.1.3 to 2.2.1
  • run tests with ts-jest and see no type errors

Code sample

Screenshots (if applicable)

What have you tried

No errors are thrown when using version 2.1.3 or 2.2.0. I think this patch may be the cause? (I'm a bit newer to TypeScript)
https://github.com/callstack/react-native-paper/commit/0f52b7c3fb8f48fec861ffaec7faf9b8e133ff9a#diff-aa71c0e947fa44abf821ee560fe149e7R10

ts-jest is having trouble finding NativeModules.PlatformConstants type definition. The tsc compiler, on the other hand, is not throwing any errors.

Your Environment

| software | version
| --------------------- | -------
| ios or android | Mac OS Sierra
| react-native | 0.57
| react-native-paper | 2.2.1 or 2.2.2
| node | 11.0.0
| npm or yarn | 6.4.1

Most helpful comment

You'll need to mock NativeModules for now. Maybe send a PR to React Native to add it? I'll add a check for now so you don't need a mock.

All 5 comments

It's happening to me too after the changes made by @satya164. I am not using TS but the error is the same.

Don't know if mocking that could be a solution or it should work without mocking...

@stella-yc While there is no fix for this, you can mock Paper's Switch component and avoid this error. Something like this should do the job

jest.mock('react-native-paper/src/components/Switch', () => 'Switch');

You'll need to mock NativeModules for now. Maybe send a PR to React Native to add it? I'll add a check for now so you don't need a mock.

Thanks @satya164 for the quick reply and fix!

Thanks @satya164 for that quick fix! 馃挴

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ButuzGOL picture ButuzGOL  路  3Comments

alikazemkhanloo picture alikazemkhanloo  路  4Comments

ButuzGOL picture ButuzGOL  路  4Comments

sritharanpalani picture sritharanpalani  路  4Comments

ferrannp picture ferrannp  路  4Comments