I've been working with jest snapshots, and have some components which use the react-native.Platform. I need to have the Version and the OS set.
I am able to set Platform.OS
manually just using Platform.OS='android'
or Platform.OS='ios'
but when I try and set Platform.Version=21
it does not work. I looked a little deeper and found its using a getter for this. Is there anyway I'd be able to set Platform.Version
similarly to how I am setting Platform.OS
.
But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist jest.mock calls to the top of the module (before any imports).
Mocking native modulesMake sure that the path to the file in setupFiles is correct. Jest will run these files before running your tests, so it's the best place to put your global mocks. If you're not using Jest, then you'll need to mock these modules according to the test framework you are using.
android - Android Studio project preconfigured with React Native support. ios - Xcode project preconfigured with React Native support. node_modules - A folder containing React Native framework and other Javascript libraries.
The following trick works for me:
Object.defineProperty(Platform, 'Version', {
get: () => 21
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With