Skip to content
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

Visual diff for functions is broken in conjunction with typed arrays #8947

Closed
leoselig opened this issue Sep 12, 2019 · 4 comments · Fixed by #9005
Closed

Visual diff for functions is broken in conjunction with typed arrays #8947

leoselig opened this issue Sep 12, 2019 · 4 comments · Fixed by #9005

Comments

@leoselig
Copy link

🐛 Bug Report

The visual diff between two objects shows a wrong diff when the objects contain a differing function on one field and identical typed arrays on another field. In that case, the visual diff indicates a mismatch of the typed array field but not of the two functions.

To Reproduce

Steps to reproduce the behavior:

Perform an assertion via toMatchObject() or toEqual() as done here: https://repl.it/repls/KindSoftOutcome

const actual = {
  typedArray: new Uint8Array(16),
  foo: () => {}
};
const expected = {
  typedArray: new Uint8Array(16),
  foo: () => {}
};
expect(actual).toMatchObject(expected); // Fails correctly but shows wrong diff
expect(actual).toEqual(expected);       // Fails correctly but shows wrong diff

Expected behavior

Both expect() assertions shoudl fail (as they do) but show "Received: serializes to the same string for the field foo.

Link to repl or repo (highly encouraged)

https://repl.it/repls/KindSoftOutcome

envinfo

System:
    OS: macOS 10.14.5
    CPU: (8) x64 Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
  Binaries:
    Node: 8.11.1 - ~/.nvm/versions/node/v8.11.1/bin/node
    Yarn: 1.16.0 - /usr/local/bin/yarn
    npm: 6.9.0 - ~/.nvm/versions/node/v8.11.1/bin/npm
@thymikee
Copy link
Collaborator

Does it happen on the latest Jest? cc @pedrottimark

@pedrottimark
Copy link
Contributor

@leoselig Thank you for the link to the replication! Sorry for the delay to reply.

There are two problems:

  1. The getObjectSubset helper function, which omits irrelevant properties from received objects, copies relevant properties into a literal object. When the expected subset is a subclass or typed array, that causes an incorrect type difference.

  2. There is an inconsistency:

    • deep equality assertions like toEqual, toHaveProperty, and toMatchObject compare different function instances same as toBe referential identity (which most people, including me, do not consider intuitive)
    • snapshot assertions compare them as equal because [Function] is the serialization

If deep-equality assertion fails only because of a function value, then the report displays Received: serializes to the same string

A work-around for problem 2 is expect.any(Function) for value of the expected subset property

@leoselig
Copy link
Author

leoselig commented Oct 4, 2019

Thanks for the in-depth explanation @pedrottimark! And no worries about the delay. :)

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants