Skip to content

Comparing two different types of values. Expected array but received object #111

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
balzdur opened this issue Jan 24, 2020 · 2 comments · Fixed by #122
Closed

Comparing two different types of values. Expected array but received object #111

balzdur opened this issue Jan 24, 2020 · 2 comments · Fixed by #122

Comments

@balzdur
Copy link

balzdur commented Jan 24, 2020

I've encountered an issue where taking a snapshot diff doesn't produce the expected output. I've outlined a simplistic example to reproduce below:

// component.js

import React from 'react';
import { View } from 'react-native';

const Component = ({withSeparator}) =>
  <>
    <View/>
    {withSeparator ? <View /> : null }
  </>

export default Component;
// component.test.js

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

it('should show separator, () => {
    expect(<Component />).toMatchDiffSnapshot(<Component withSeparator />);
});
// component.test.js.snap

exports[`should show separator 1`] = `
Snapshot Diff:
  Comparing two different types of values. Expected array but received object.
`;

The above error is produced by diffStrings and occured when we compare an array with an object.
In this case, the call is :

difference = diffStrings(print(valueA, identity), print(valueB, identity), ...);

// print(valueA, identity) = { type: 'View', props: {}, children: null }
// print(valueB, identity) = [ { type: 'View', props: {}, children: null },
      { type: 'View', props: {}, children: null } ]

A solution to this issue could be to wrap the object in an array.

@thymikee
Copy link
Member

thymikee commented Feb 4, 2020

cc @pedrottimark

@michaelmang
Copy link

I am also experiencing this issue. It happens whenever you are using snapshotDiff with elements that are fragments.

alistairjcbrown added a commit to alistairjcbrown/snapshot-diff that referenced this issue Apr 26, 2020
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 jest-community#111
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