Skip to content

Jest mock names not honoured in resulting snapshot #106

Closed
@anthonyhastings

Description

@anthonyhastings

I've encountered an issue where taking a snapshot diff doesn't maintain a mock functions name. I've outlined a simplistic example to reproduce below:

// component.js

import React from 'react';
import PropTypes from 'prop-types';

const Component = ({ text, onClick }) =>
  <button onClick={onClick}>{text}</button>;

Component.propTypes = {
  onClick: PropTypes.func,
  text: PropTypes.string
};

Component.defaultProps = {
  text: 'Hello world!'
};

export default Component;
// component.spec.js

import React from 'react';
import renderer from 'react-test-renderer';
import Component from './component';

describe('Component', () => {
  it('renders correctly', () => {
    expect(renderer.create(
      <Component onClick={jest.fn().mockName('onClick')} />
    )).toMatchSnapshot();
  });

  it('accepts text and click functionality', () => {
    expect(
      renderer.create(
        <Component />
      )
    ).toMatchDiffSnapshot(
      renderer.create(
        <Component
          onClick={jest.fn().mockName('onClick')}
          text='Foo bar'
        />
      )
    );
  })
});
// component.spec.js.snap

// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Component accepts text and click functionality 1`] = `
"Snapshot Diff:
- First value
+ Second value

- <button>
-   Hello world!
+ <button
+   onClick={[Function mockConstructor]}
+ >
+   Foo bar
  </button>"
`;

exports[`Component renders correctly 1`] = `
<button
  onClick={[MockFunction onClick]}
>
  Hello world!
</button>
`;

The above example shows that a standard snapshot honours the mock name, but the resulting diff does not. I'm unsure if this is a setup problem on my part but my assumption is that it's not; my understanding is that snapshot-diff uses react-test-renderer under the hood by default, so I'd have expected the mock name to have been honoured.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions