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:
- Created 5 years ago
- Comments:7 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes:
@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?
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.