Skip to content

Jest mock names not honoured in resulting snapshot #106

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

Closed
anthonyhastings opened this issue Dec 18, 2019 · 5 comments · Fixed by #122
Closed

Jest mock names not honoured in resulting snapshot #106

anthonyhastings opened this issue Dec 18, 2019 · 5 comments · Fixed by #122

Comments

@anthonyhastings
Copy link

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.

@thymikee
Copy link
Member

This lib uses packages from Jest:

"jest-diff": "^24.0.0",
"jest-snapshot": "^24.0.0",
"pretty-format": "^24.0.0",

Maybe you need to make sure they're on the same version? (e.g. by Yarn resolutions or by regenerating the lock file). I'd suspect pretty-format to not be in line specifically.

@anthonyhastings
Copy link
Author

Hi! I've used npm as the dependency management system here and from what I can tell, jest and snapshot-diff are both using the same instances of the packages. There's not multiple versions of each package's dependencies installed.

@thymikee
Copy link
Member

Interesting. Does it work as expected when doing snapshotDiff(c1, c2).toMatchSnapshot()? Maybe we need to tweak how we call snapshot diffing serializer, but I don't recall what could change there. cc @pedrottimark

@anthonyhastings
Copy link
Author

anthonyhastings commented Dec 18, 2019

Based on your last message about calling the snapshotDiff method directly, here's an even smaller reproduction case:

Command:

console.log(
  snapshotDiff(
    renderer.create(<button onClick={jest.fn().mockName('hello')}>Hello</button>),
    renderer.create(<button onClick={jest.fn().mockName('world')}>World</button>)
  )
);

Result:

Snapshot Diff:
- First value
+ Second value

  <button
    onClick={[Function mockConstructor]}
  >
-   Hello
+   World
  </button>

@alistairjcbrown
Copy link
Contributor

Did a little digging - it looks like the displaying the mock name is a custom serializer of jest-snapshot
https://github.com/facebook/jest/blob/e760ec40f04042d46c58bf7fd2c8049cbad8abb0/packages/jest-snapshot/src/mock_serializer.ts

Out of the box, the ReactElement plugin of pretty-format (used by jest-diff) doesn't grab the name for jest mock functions - https://github.com/facebook/jest/blob/fa4cbbf87cb3c97fe44f1fee30663715a9dbba71/packages/pretty-format/src/plugins/ReactElement.ts#L37

To support this, we'd need to try and wire in the custom serializer from jest-snapshot/build/mock_serializer

alistairjcbrown added a commit to alistairjcbrown/snapshot-diff that referenced this issue Apr 26, 2020
Use same serializers as jest-snapshot when serializing react components
rendered by react-test-renderer.

Fixes jest-community#106
thymikee pushed a commit that referenced this issue May 6, 2020
* Output jest mock function names in snapshot diff

Use same serializers as jest-snapshot when serializing react components
rendered by react-test-renderer.

Fixes #106

* Add test to show output for React fragments

With the React serializer updated to use `prettyFormat`, this now
supports diffing React fragments when comparing a single element to
multiple elements returned. Adding test to confirm output.

Fixes #111

* Use jest-snapshot serializers for diff annotations

Makes use of the same serializers used for printing, in the react
serializer diff options which set up the A and B diff annotations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants