question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Snapshot test fails when using React Native Picker and Picker.Item

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
LuisUrrutiacommented, May 17, 2018

@WilliamIPark is right, you should use this in the newest versions:

jest
  .mock('Picker', () => {
    // eslint-disable-next-line import/no-unresolved
    const React = require('React');
    const PropTypes = require('prop-types');
    return class MockPicker extends React.Component {
      static Item = props => React.createElement('Item', props, props.children);
      static propTypes = { children: PropTypes.any };
      static defaultProps = { children: '' };

      render() {
        return React.createElement('Picker', this.props, this.props.children);
      }
    };
  });
5reactions
cpojercommented, Nov 3, 2016

you should instead do this, actually:

jest.mock('Picker', () => {
  const React = require('React');
  return class MockPicker extends Component {
    static Item = props => React.createElement('Item', props, props.children);
    static propTypes = { children: React.PropTypes.any };

    render() {
      return React.createElement('Picker', this.props, this.props.children);
    }
  }
});
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found