Skip to content

toMatchObject diff displays Received from react-test-renderer as Object instead of JSX #2351

@pedrottimark

Description

@pedrottimark

Do you want to request a feature or report a bug?

Report a bug in Jest 18.0.0 running on Node 7.2.1 with npm 3.10.9

In all of the following examples, notice that the findMatchObject helper nicely omits the irrelevant onClick property from the received object in a detailed diff when when toMatchObject assertion fails, as expected. Thank you for this super attention to detail!

But the temporary object derived from received for the diff does not always have a $$typeof property for the ReactTestComponent plugin to pretty-format it as JSX.

What is the current behavior?

EDIT: JSX-versus-Object diff when expected $$typeof is non-enumerable

-<li
-  style={
-    Object {
-      "textDecoration": "none",
-    }
-  }>
-  Install Jest
-</li>
+Object {
+  "children": Array [
+    "Install Jest",
+  ],
+  "props": Object {
+    "style": Object {
+      "textDecoration": "line-through",
+    },
+  },
+  "type": "li",
+}
import React from 'react';
import renderer from 'react-test-renderer';

test('diff displays Object instead of JSX', () => {
  const onClick = jest.fn();
  const text = 'Install Jest';

  const expected = renderer.create(
    <li style={{textDecoration: 'none'}}>{text}</li>
  ).toJSON();
  const received = renderer.create(
    <li style={{textDecoration: 'line-through'}} onClick={onClick}>{text}</li>
  ).toJSON();

  expect(received).toMatchObject(expected);
});

What is the expected behavior?

EDIT: JSX diff when expected $$typeof is enumerable

 <li
   style={
     Object {
-      "textDecoration": "none",
+      "textDecoration": "line-through",
     }
   }>
   Install Jest
 </li>
import React from 'react';
import {shallow} from 'enzyme';
import enzymeToJSON from 'enzyme-to-json';

test('diff displays JSX', () => {
  const onClick = jest.fn();
  const text = 'Install Jest';

  const expected = enzymeToJSON(shallow(
    <li style={{textDecoration: 'none'}}>{text}</li>
  ));
  const received = enzymeToJSON(shallow(
    <li style={{textDecoration: 'line-through'}} onClick={onClick}>{text}</li>
  ));

  expect(received).toMatchObject(expected);
});

Analysis

Object.keys and Object.prototype.hasOwnProperty copy the relevant enumerable properties in: https://github.com/facebook/jest/blob/master/packages/jest-matchers/src/matchers.js#L623-L624

Conclusion

The inconsistency seems important to fix, but not an emergency, because toMatchObject is brand new and the reason I found it is a discussion-in-progress: #2202 (comment)

Creating a derived object with transformed properties from a source which has non-enumerable properties seems like a generic problem, but the subject is new to me, so I will leave to y’all who have more experience to suggest how to solve it :)

If you need, I am glad to create a test repo on Monday.

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