Jest Received: serializes to the same string

In jest for some reason you get something like

expected: "test"
received: serializes to the same string

if you do .toContainEqual

expected: "test"
received: "test"

this seems to only occur when using mongoose with jest, but I think the issue has to do with uriEncoding and decoding

Answer

If you’re testing the response from a request then try

expected = decodeURI(encodeURI("test"))
result = [...] // equals "test"

expect(result).toEqual(expected)

This may also work but sometimes has issues because of JSON string parsing

expected = <some object>
result = <object that serializes to the same string>
expect(result.toString()).toEqual(expect.toString())

If you’re only comparing the result of a document versus an object or output from an aggregation then try

expect(result).toEqual(expected)