@@ -8,6 +8,8 @@ type RNTestGlobal = typeof globalThis & {
88 } ;
99} ;
1010
11+ type TurboProxy = NonNullable < RNTestGlobal [ "__turboModuleProxy" ] > ;
12+
1113const originalProxy = ( globalThis as RNTestGlobal ) . __turboModuleProxy ;
1214
1315const createNativeMock = ( ) : Spec => ( {
@@ -20,24 +22,29 @@ afterEach(() => {
2022 ( globalThis as RNTestGlobal ) . __turboModuleProxy = originalProxy ;
2123 jest . resetModules ( ) ;
2224 jest . resetAllMocks ( ) ;
23- jest . dontMock ( "react-native" ) ;
2425} ) ;
2526
2627describe ( "Native module resolution" , ( ) => {
2728 it ( "uses TurboModuleRegistry when the TurboModule proxy is present" , ( ) => {
2829 const fakeNative = createNativeMock ( ) ;
29- const getMock = jest . fn ( ( ) => fakeNative ) ;
30+ const getMock = jest . fn ( ( ) => fakeNative ) as unknown as TurboProxy [ "get" ] ;
31+
32+ const getEnforcingMock = jest . fn ( ) as unknown as TurboProxy [ "getEnforcing" ] ;
3033
3134 ( globalThis as RNTestGlobal ) . __turboModuleProxy = {
3235 get : getMock ,
33- getEnforcing : jest . fn ( ) ,
36+ getEnforcing : getEnforcingMock ,
3437 } ;
3538
36- jest . doMock ( "react-native" , ( ) => ( {
37- TurboModuleRegistry : { get : getMock , getEnforcing : jest . fn ( ) } ,
38- NativeModules : { } ,
39- Platform : { select : jest . fn ( ( ) => undefined ) } ,
40- } ) ) ;
39+ jest . doMock (
40+ "react-native" ,
41+ ( ) => ( {
42+ TurboModuleRegistry : { get : getMock , getEnforcing : getEnforcingMock } ,
43+ NativeModules : { } ,
44+ Platform : { select : jest . fn ( ( ) => undefined ) } ,
45+ } ) ,
46+ { virtual : true }
47+ ) ;
4148
4249 jest . isolateModules ( ( ) => {
4350 const Native = require ( "../src/specs/NativeContactsLastUpdated" ) . default as Spec ;
@@ -50,29 +57,37 @@ describe("Native module resolution", () => {
5057 delete ( globalThis as RNTestGlobal ) . __turboModuleProxy ;
5158
5259 const fakeNative = createNativeMock ( ) ;
53- const getMock = jest . fn ( ( ) => undefined ) ;
60+ const getMock = jest . fn ( ( ) => undefined ) as unknown as TurboProxy [ "get" ] ;
5461
55- jest . doMock ( "react-native" , ( ) => ( {
56- TurboModuleRegistry : { get : getMock , getEnforcing : jest . fn ( ) } ,
57- NativeModules : { ContactsLastUpdated : fakeNative } ,
58- Platform : { select : jest . fn ( ( ) => undefined ) } ,
59- } ) ) ;
62+ jest . doMock (
63+ "react-native" ,
64+ ( ) => ( {
65+ TurboModuleRegistry : { get : getMock , getEnforcing : jest . fn ( ) } ,
66+ NativeModules : { ContactsLastUpdated : fakeNative } ,
67+ Platform : { select : jest . fn ( ( ) => undefined ) } ,
68+ } ) ,
69+ { virtual : true }
70+ ) ;
6071
6172 jest . isolateModules ( ( ) => {
6273 const Native = require ( "../src/specs/NativeContactsLastUpdated" ) . default as Spec ;
6374 expect ( Native ) . toBe ( fakeNative ) ;
64- expect ( getMock ) . toHaveBeenCalledWith ( "ContactsLastUpdated" ) ;
75+ expect ( getMock ) . not . toHaveBeenCalled ( ) ;
6576 } ) ;
6677 } ) ;
6778
6879 it ( "throws a helpful error when the native module is unavailable" , ( ) => {
6980 delete ( globalThis as RNTestGlobal ) . __turboModuleProxy ;
7081
71- jest . doMock ( "react-native" , ( ) => ( {
72- TurboModuleRegistry : { get : jest . fn ( ( ) => undefined ) , getEnforcing : jest . fn ( ) } ,
73- NativeModules : { } ,
74- Platform : { select : jest . fn ( ( ) => undefined ) } ,
75- } ) ) ;
82+ jest . doMock (
83+ "react-native" ,
84+ ( ) => ( {
85+ TurboModuleRegistry : { get : jest . fn ( ( ) => undefined ) , getEnforcing : jest . fn ( ) } ,
86+ NativeModules : { } ,
87+ Platform : { select : jest . fn ( ( ) => undefined ) } ,
88+ } ) ,
89+ { virtual : true }
90+ ) ;
7691
7792 expect ( ( ) =>
7893 jest . isolateModules ( ( ) => {
0 commit comments