question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

unexpected output when using custom toJSON

See original GitHub issue

🐛 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 

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
pedrottimarkcommented, 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?

0reactions
github-actions[bot]commented, Mar 30, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Unexpected output using reduce to create json - Stack ...
I'm working with apps script. I have an array of objects 'sendableRows' that I would like to turn to json and email. an...
Read more >
12: 9.15. JSON Functions and Operators - PostgreSQL
The || operator concatenates two JSON objects by generating an object containing the union of their keys, taking the second object's value when...
Read more >
Jackson Exceptions - Problems and Solutions - Baeldung
Control your JSON output with Jackson 2 by using a Custom Serializer. ... Use Jackson to map custom JSON to any java entity...
Read more >
JSON Node Unexpected characters error | Data360 Analyze
Hello, I have noticed this error occurred couple of times with our Data360 workflow. JSON node gets failed because of unexpected characters ...
Read more >
Solved: Parsing Json File Giving Unexpected Results
Solved: I am trying to import a .json file using the Power BI .json connector and am running into some issues. Everytime I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found