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

unexpected output when using custom toJSON #8098

Closed
goenning opened this issue Mar 10, 2019 · 7 comments
Closed

unexpected output when using custom toJSON #8098

goenning opened this issue Mar 10, 2019 · 7 comments

Comments

@goenning
Copy link

goenning commented Mar 10, 2019

🐛 Bug Report

I got the following report from Jest:

  ● should work

    expect(received).toEqual(expected)

    Expected: "Hello World"
    Received: "Hello World"

This is very confusing as both texts are exactly the same.

To Reproduce

class MyComponent {
  toJSON() {
    return "Hello World";
  }
}

it("should work", () => {
  const comp = new MyComponent();
  expect(comp.toJSON()).toEqual("Hello World") // <-- this works
  expect(comp).toEqual("Hello World") // <-- this doesn't work
});

Expected behavior

I should work the same way as the previous assertion OR if this is expected behavior, the output message should explain what's wrong.

Link to repl or repo (highly encouraged)

https://repl.it/repls/TurboGlassMotion

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: macOS 10.14.3
    CPU: (4) x64 Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz
  Binaries:
    Node: 10.15.2 - /usr/local/bin/node
    npm: 6.4.1 - /usr/local/bin/npm
  npmPackages:
    jest: 24.3.1 => 24.3.1 
@SimenB
Copy link
Member

SimenB commented Mar 10, 2019

@pedrottimark I think "no visual difference" error is gone.

In addition - in this case, could we somehow mark the fact the string came from toJSON to say why they are not considered equal?

@jeysal might have an idea here as well

@jeysal
Copy link
Contributor

jeysal commented Mar 11, 2019

@SimenB I think with the current architecture only pretty-format knows that it was from toJSON so expect can't easily do it - the ability to do something like this might be something to consider for #6184 ?

@pedrottimark
Copy link
Contributor

pedrottimark commented Mar 11, 2019

Yes:

  • @SimenB I think when we responded to request not to display diff for one-line strings, we temporarily lost the no visual difference message.
  • @jeysal Good point, it is another example for data-driven diff.

@goenning The “this doesn't work” assertion compares an object to a string, which fails, and then the custom serialization makes it looks as if it should have passed. So that I can understand the context, did you decide that the “this works” assertion is what you meant to test?

@goenning
Copy link
Author

I didn’t know that toJSON was part of JSON.stringify function. I thought this was a Jest convention. My primary issue was #8097

I understand now that toJSON is just used for presentation and not for assertion.

For me this issue is not affecting me anymore, but it could be confusing for others. Whether you consider to fix it or not is up to you.

Thanks for the explanation.

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the Stale label Feb 25, 2022
@SimenB
Copy link
Member

SimenB commented Feb 27, 2022

Doesn't seem like anybody else has run into this

@SimenB SimenB closed this as completed Feb 27, 2022
@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 Mar 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants