[Test][error]serializes to the same string


デバイステスト中にエラーが発生しました.
describe('utils.js 파일', () => {
  describe('parseQueryIntoObject 함수 테스트', () => {
    test('파라미터가 check_in=2022-02-13&check_out=2022-02-23이면 { check_in: 2022-02-13, check_out: 2022-02-23 }', () => {
      expect(parseQueryIntoObject("?check_in=2022-02-13&check_out=2022-02-23")).toEqual({ check_in: '2022-02-13', check_out: '2022-02-23' })
    })
  })
この状態でテストを行い、serializes toという同じstringというエラーが発生しました.

どういう意味ですか.Jest公式ハブの問題ラベルをチェックしました.
回答の1つを参考にしました.
toBe does physical comparison (referential identity, ie. that they are the same, not just look the same) on composite values like objects and arrays, and variants in most cases compile down to a JS array as it does here. It's therefore often better to use toEqual in bs-jest since that does structural comparison and the type system protects against the problems with using non-strict equality in JS.

toBeとtoEqual


toBeはオブジェクトの参照値を比較し、toEqualはオブジェクトの内部の値を比較します.
上記のコードでは,戻り値が1つのオブジェクトであるため,toBeでは正確に判別できない.単純な文字自体は同じですが、新しく作成されたオブジェクトは参照値が異なるため、テストに合格しませんでした.この場合、参照値が同一オブジェクトではなく同一オブジェクトであるか否かを判別する必要があるので、toEqualを用いてオブジェクト内部の値を比較することができる.

ToEqualテストに変えて、テストはよく合格しました.
tobe比較対象の参考値を使っているかどうか気になったので探してみましたが、対象の不変性をチェックするためだそうです.
オブジェクトの不変性を維持してこそ、不要なレンダリングは発生しないので、レンダリングの最適化の観点からも重要なテストのようです.
この部分は次回必ず理解してください.

Reference

  • https://github.com/glennsl/rescript-jest/issues/53
  • https://hoony-gunputer.tistory.com/entry/jest-%EC%97%AC%EB%9F%AC%EA%B0%80%EC%A7%80-totoBe-toEqual-toStrictEqual-%EC%82%AC%EC%9A%A9%ED%95%B4%EB%B3%B4%EA%B8%B0