Snapshot test fails when using React Native Picker and Picker.Item
See original GitHub issueI have seen similar problems with other components, but couldn’t find the right mock implementation for Picker.
My error:
TypeError: Cannot read property '_tag' of undefined
My component:
import React, { Component } from 'react';
import {
Picker,
} from 'react-native';
export default class TestComponent extends Component {
render() {
return (
<Picker
selectedValue={this.props.asset.type}
onValueChange={this.props.onTypeChange}>
<Picker.Item label="Type of asset" value="default" />
<Picker.Item label="Car" value="car" />
<Picker.Item label="Boat" value="boat" />
<Picker.Item label="Ship" value="ship" />
</Picker>
);
}
}
My test:
import 'react-native';
import React from 'react';
import TestComponent from '../testExample/TestComponent';
import renderer from 'react-test-renderer';
describe('TestComponent', () => {
const asset = {
type: 'car',
}
it('renders correctly', () => {
const comp = renderer.create(
<TestComponent
asset={asset} />
)
const tree = comp.toJSON();
expect(tree).toMatchSnapshot();
});
})
Tried mocking it some way but didn’t help. I could make it work with Picker.Item-s commented out.
jest.mock('Picker', () => 'Picker'); // Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. Check the render method of `TestComponent`.
Issue Analytics
- State:
- Created 7 years ago
- Comments:12 (4 by maintainers)
Top Results From Across the Web
How to mock Picker and Picker.Item with jest in React-Native?
Created a github issue as well and here is a working answer: jest.mock('Picker', () => { const Picker = class extends Component {...
Read more >Snapshot Testing - Jest
Snapshot tests are a very useful tool whenever you want to make sure your UI does not change unexpectedly.
Read more >What's wrong with snapshot tests. Since snapshot ... - Medium
A little change in a Button component may lead to a failure of hundreds of snapshots, even when everything works fine. A Button...
Read more >Testing your Changes · React Native - API Manual
A common type of integration test is the snapshot test. These tests render a component, and verify snapshots of the screen against reference...
Read more >API | React Native Testing Library - Open Source
Latest render result is kept in screen variable that can be imported from @testing-library/react-native package. Using screen instead of destructuring ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@WilliamIPark is right, you should use this in the newest versions:
you should instead do this, actually: