-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set defaultPlatform for specific test #1370
Comments
To extend this issue: There is not just the problem with platform specific files. If you have code like this: import { StyleSheet, Platform } from 'react-native';
export default StyleSheet.create({
container: {
paddingTop: Platform.OS === 'ios' ? 30 : 10,
}
});
|
Just to make sure it won't go unnoticed, |
FWIW, I've been able to work around this by setting up jest mocks to point to the different files. It's pretty janky, so a built-in solution would be really nice :) Something like:
Then in your test assign Platform.OS to your desired platform before requiring in your subject under test:
|
Little bit late to the party but might be of value: I have a 'general helpers' file in my app which exports an 'onAndroid' function with the implementation as such:
This gives me the opportunity to easily make platform-specific code in components like this:
and, why I'm typing this, to test components on a specific platform by mocking the 'onAndroid' function like so:
|
Platform.select can be mocked to return android branch:
Platform.OS can be specified easily by:
|
Anyone have any idea on how to deal with creating snapshots with platform-specific modules? When testing a component that relies on
|
I've been using this approach to switch to using an android version of Platform selet: jest.mock('react-native', function() {
const reactNative = require.requireActual('react-native');
jest
.spyOn(reactNative.Platform, 'select')
.mockImplementation(obj => obj.android || obj.default);
return reactNative;
}); Not sure if it's the best approach |
Any update on this? I can't find an elegant approach to solve this :( |
@gogumai I did this on my code and it actually works.
I'm using Jest v21.2.1. |
@gogumai After spending many hours, found this solution:
|
Hey @grigored, that solution from #4455 is a step in the right direction, but any idea how to use snapshots when you want to test on multiple platforms? When testing on one platform, the snapshots don't match up with the output from the other platform. Per #1650, there seems to be no way to configure Android snapshots to go in one place while iOS snapshots go to another. |
Hm... I guess you could write a short bash script to save the snapshots in snapshots_ios after the test is run, and back when you run the tests. something like this might work
|
Any latest solution to this? |
No other than setting |
Non of the above worked for me. // styles.js import styled from "styled-components";
export const Heading = styled.View`
flex-direction: row;
justify-content: space-between;
align-items: flex-start;
padding-top: ${props => props.platform && props.platform === 'ios' ? 30 : 20};
padding-left: 28;
padding-right: 28;
min-height: 70;
`; // styles.test.js import React from "react";
import { shallow } from "enzyme";
import toJson from "enzyme-to-json";
import { Heading } from "../styles";
describe("Heading Component", () => {
it("Should render without issues", () => {
const component = shallow(<Heading/>);
expect(component.length).toBe(1);
expect(toJson(component)).toMatchSnapshot();
});
it("Should render without issues on ios", () => {
const component = shallow(<Heading platform='ios'/>);
expect(component.length).toBe(1);
expect(toJson(component)).toMatchSnapshot();
});
it("Should render without issues on android", () => {
const component = shallow(<Heading platform='android'/>);
expect(component.length).toBe(1);
expect(toJson(component)).toMatchSnapshot();
});
}); // CommonHeader/index.js import PropTypes from "prop-types";
import React from "react";
import styled from "styled-components";
import { Platform } from 'react-native';
import { BackButton } from "../BackButton";
import { CloseButton } from "../CloseButton";
import { Heading } from "./styles";
export class CommonHeader extends React.Component {
render() {
const { componentId } = this.props;
const logo = this.props.logo
? this.props.logo
: require("@/images/Logos/rouge.png");
return (
<Heading platform={Platform.OS}>
<BackButton componentId={componentId} />
<LogoContainer>
<Logo source={logo} />
</LogoContainer>
<CloseButton componentId={componentId} />
</Heading>
);
}
}
CommonHeader.propTypes = {
componentId: PropTypes.string.isRequired
};
const LogoContainer = styled.View`
flex-direction: column;
justify-content: flex-start;
`;
const Logo = styled.Image``; |
Hi ! Any update on this? I've started a project two weeks ago with an updated react-native and jest version and the issue is still present. |
Looks like https://jestjs.io/docs/en/configuration.html#snapshotresolver-string could be leveraged to store the snapshots in different paths based on the config. |
In react-native we use Platform check a lot and it's a shame that there is no easy way to mock Platform to our tests. |
Currently, the best way to do this is by using @expo/universal approach from Expo team. If you use Expo you could use @expo/universal directly but if you use bare react-native, you can copy the approach and edit it to work with your project. |
Absolutely brilliant approach works wonderfully. |
Bumped on the same problem. It seems like |
i have the same issue too. |
I would like to do snapshot tests for both iOS and Android, so the haste settings in the config file are not working for me. |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
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. |
In react-native we have
.android.js
,.ios.js
,.web.js
,.windows.js
, etc. In order to test components on different platforms easily, it would be helpful to be able to specify what platform a specific test is for and have it automatically include the correct files based on the extension. As discussed on messenger @cpojerThe text was updated successfully, but these errors were encountered: