Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

React Native - unit test fails because of Native Module

$
0
0

I am getting a weird anomaly when I declare a Native Java Module the new code doesn't pass unit test. Basically whatever I import from NativeModules in React Native lacks a definition: So the Unit test fails because TypeError: Cannot read property 'HelloWorld' of undefined

Steps to reproduce:

import { NativeModules } from 'react-native';const Thing = NativeModules.SomeModule;export const helloWorld = (addedText: string) => {  return Thing.HelloWorld(addedText);};export default Thing;

but the error is

  TypeError: Cannot read property 'HelloWorld' of undefined  4 |  5 | export const helloWorld = (addedText: string) => {> 6 |   return Thing.HelloWorld(addedText);    |                ^  7 | };  8 |  9 | export default Thing;

the actual Java is

public class SomeModule extends ReactContextBaseJavaModule  {  SomeModule(ReactApplicationContext context) {    super(context);  }  @Override  public String getName() {    return "SomeModule";  }  void HelloWorld(String addedText){     try {       Log.w("HELLO_WORLD", addedText);     }     catch (Exception e) {       Log.e("DEVICE_MODULE_HELLO_WORLD_FAILED", "HelloWorld() Failed");     }  }}

and this runs fine (when you run it) but the unit tests hate it. The issue seems to be that the React Native test lacks an awareness of the structure of const helloWorld = (addedText: string) => { return Thing.HelloWorld(addedText); }; so I dutifully made this d.ts to help

declare namespace Thing {   function helloWorld(addedText: string): void;}

but it still fails when at unit test, it feels wrong for me to simply assume this has been overlooked, so I am assuming I must have implemented something incorrectly. Can anyone shed light on the correct way of doing this?


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>