react-native-safe-area-context icon indicating copy to clipboard operation
react-native-safe-area-context copied to clipboard

Missing types for react-native-safe-area-context/jest/mock

Open flo-sch opened this issue 3 years ago • 2 comments

Hello,

When setting up the mock for jest, I face a typescript error:

Could not find a declaration file for module 'react-native-safe-area-context/jest/mock'.

The mock is currently a JS file, would it be possible to rewrite it to TS or provide a definition for it?

flo-sch avatar Sep 03 '21 09:09 flo-sch

I do not know this module too much (RN beginner here), but I managed to workaround with that local definition:

// react-native-safe-area-context.d.ts
declare module 'react-native-safe-area-context/jest/mock' {
  import RNSafeAreaContext, { Metrics, SafeAreaViewProps } from 'react-native-safe-area-context';

  export const initialWindowMetrics: Metrics;
  export const SafeAreaProvider: React.FC<SafeAreaViewProps>;

  export default RNSafeAreaContext;
}

It works for me with TS v4.4, however I am not sure how relevant it is regarding this module. (I just tried to reproduce what the mock is currently doing)

flo-sch avatar Sep 03 '21 09:09 flo-sch

I believe you have to use this file to create your own mock in your Jest environment setup, like this:

jest.mock('react-native-safe-area-context', () => require('react-native-safe-area-context/jest/mock').default);

This way the library is mocked globally in Jest and you shouldn't get any complaints from TS

jshakes avatar Mar 28 '22 19:03 jshakes

Same problem

vladimirevstratov avatar Nov 24 '22 17:11 vladimirevstratov

Please continue discussion https://github.com/th3rdwave/react-native-safe-area-context/pull/231

jacobp100 avatar Jan 19 '23 16:01 jacobp100