Closed1
Jestで「Received: serializes to the same string」
![marusho](https://res.cloudinary.com/zenn/image/fetch/s--teLBTxNG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_70/https://storage.googleapis.com/zenn-user-upload/avatar/158e0af88c.jpeg)
事象
Jestで返り値のオブジェクトを比較しようとしてエラーが出る
expect(received).resolves.toBe(expected) // Object.is equality
If it should pass with deep equality, replace "toBe" with "toStrictEqual"
Expected: {"id": "1", "content": "testContent"}
Received: serializes to the same string
解決方法
エラーメッセージに書いてある通り、.toStrictEqual
を使うと解決できました
原因
toBe(value)
は比較対象がプリミティブ型(string, numberなど)のみです。
今回のようにオブジェクトを比較するときは使えません。
ちなみに浮動小数点を扱うときは.toBeCloseTo()
を使う方がよい
このスクラップは2023/01/15にクローズされました