ts-jest throws type error related to `NativeModules` type on ver 2.2.1
See original GitHub issueCurrent behaviour
- Upgrade
react-native-paperfrom2.1.3to2.2.1in 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-paperfrom2.1.3to2.2.1 - run tests with
ts-jestand 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 |
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Jest is throwing type errors from dependent node_modules
, Abstract.jsx is coming from dependent node_modules. The module should come with compiled .js files. It seems that it doesn't. Options.
Read more >Mocking RN modules | React Made Native Easy
If you install any other modules which aren't automatically mocked, say a react-native module which has a dependency on NativeModules, you would need...
Read more >TypeScript Jest: Unexpected Token Export - Reddit
Ok so now your test project is using common JS, but node sees your dependency as an ES Module, so it throws an...
Read more >ts-jest - npm
ts -jest is a TypeScript preprocessor with source map support for Jest that lets you use Jest to test projects written in TypeScript....
Read more >How I do configure Jest to test my Angular 8 Project - ITNEXT
npm install -D jest jest-preset-angular @types/jest. Then create setupJest.ts file in the same root with package.json or inside src of main workspace.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

You’ll need to mock
NativeModulesfor 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!