Closed turnipdabeets closed 7 years ago
Unfortunately mocking Platform isn't the only thing that requires changing. The default is probably iOS from the preset, but you can provide your own config for the "haste" field that overwrites the default platform: See https://github.com/facebook/react-native/blob/master/jest-preset.json#L3
Try:
"haste": {
"defaultPlatform": "android",
"platforms": ["android", "ios", "native"],
"providesModuleNodeModules": [
"react-native"
]
}
In your Android config.
Please note that there is currently no good way to toggle between two configs. You could however use the multi project runner :)
Based on @cpojer 's suggestion, I'm using this solution, works great.
Having the same issue, I have an utility function like:
const mockPlatform = (OS, version) => {
jest.resetModules();
jest.doMock("Platform", () => ({
OS,
select: objs => objs[OS],
Version: version || undefined
}));
};
then I do
mockPlatform("android", 23);
inside the test but when I console.log Platform
I get:
{ OS: 'ios',
Version: [Getter],
isPad: [Getter],
isTVOS: [Getter],
isTV: [Getter],
isTesting: [Getter],
select: [Function: select] }
and then
{ OS: 'android', select: [Function: select], Version: 23 }
after
Fixed by doing
beforeEach(() => {
jest.restoreAllMocks();
mockPlatform("android", 24);
});
Just set Platform.OS in your tests, I did not need a mock, and I like to stay away from mocking frameworks personally.
Bumped on the same problem. It seems like haste
for now is the only way to solve this.
Created a dummy test project to have a possibility to run 2 set of tests (iOS and Android):
haste
still works but it prints warning on the console
● Validation Warning:
Unknown option "haste.providesModuleNodeModules" with value ["react-native"] was found.
This is probably a typing mistake. Fixing it will remove this message.
Configuration Documentation:
https://jestjs.io/docs/configuration.html
● Validation Warning:
Unknown option "haste.providesModuleNodeModules" with value ["react-native"] was found.
This is probably a typing mistake. Fixing it will remove this message.
Configuration Documentation:
https://jestjs.io/docs/configuration.html
Unfortunately mocking Platform isn't the only thing that requires changing. The default is probably iOS from the preset, but you can provide your own config for the "haste" field that overwrites the default platform: See https://github.com/facebook/react-native/blob/master/jest-preset.json#L3
Try:
"haste": { "defaultPlatform": "android", "platforms": ["android", "ios", "native"], "providesModuleNodeModules": [ "react-native" ] }
In your Android config.
Please note that there is currently no good way to toggle between two configs. You could however use the multi project runner :)
hey @cpojer anything new for platform setting?
Alright I get going on this: found this: https://github.com/facebook/react-native/blob/master/jest-preset.js
and removed providesModuleNodeModules. My tests are shined again ✨
@Kjaer So after you set the haste in config, what do you do?
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.
Below I am testing a component to have a red background in iOS and a blue background in Android. However, the jest snapshot always defaults to iOS if your component uses react native's method:
Platform.select
.component.js
component.spec.js
component.js.snap
If I change component.js to the following (removing Platform.select with an alternative option):
npm run test
displays the following error showing jest's snapshot is now displaying the desirable blue background for android.