Home > Software engineering >  Jest expected and received deep equality on array of object fails
Jest expected and received deep equality on array of object fails

Time:12-30

I am using Jest in order to test my code but I cannot seem to get the expected object to equal the receiving object. I do not know why everything seems to be correct:

The output:

    Expected: ArrayContaining [{"buildingScores": ObjectContaining {"coast": Any<Number>, "fluvial": Any<Number>, "pluvial": Any<Number>}, "buildings": ArrayContaining [ObjectContaining {"id": Any<Number>, "score": Any<Number>, "scores": ObjectContaining {"coast": Any<Number>, "fluvial": Any<Number>, "pluvial": Any<Number>}}], "builidngScore": Any<Number>, "capakey": Any<String>, "createdAt": StringMatching /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/, "owners": ArrayContaining [Any<String>], "plotScore": Any<Number>, "plotScores": ObjectContaining {"coast": Any<Number>, "fluvial": Any<Number>, "pluvial": Any<Number>}, "updatedAt": StringMatching /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/, "zones": ObjectContaining {"bank": Any<Boolean>, "risk": Any<Boolean>, "signalArea": Any<Boolean>}}]
    Received: [{"buildingScores": {"coast": 0, "fluvial": 0, "pluvial": 1}, "buildings": [{"id": 5723528, "score": 1, "scores": {"coast": 0, "fluvial": 0, "pluvial": 1}}, {"id": 6113163, "score": 0, "scores": {"coast": 0, "fluvial": 0, "pluvial": 0}}], "builidngScore": 1, "capakey": "44017A0860/00G002", "createdAt": 2022-12-28T12:58:12.319Z, "owners": "{\"WATERING OUDE KALE EN MEIREBEEK\"}", "plotScore": 2, "plotScores": {"coast": 0, "fluvial": 0, "pluvial": 2}, "updatedAt": 2022-12-28T12:58:12.319Z, "zones": {"bank": false, "risk": false, "signalArea": false}}]

The assertion:

expect(scores).toEqual(expect.arrayContaining([expectedFloodScoreObject]))

export const expectedFloodScoreObject = {
  createdAt: expect.stringMatching(DATE_REGEX),
  updatedAt: expect.stringMatching(DATE_REGEX),
  capakey: expect.any(String),
  owners: expect.arrayContaining([expect.any(String)]),
  zones: expect.objectContaining({
    risk: expect.any(Boolean),
    bank: expect.any(Boolean),
    signalArea: expect.any(Boolean)
  }),
  plotScore: expect.any(Number),
  plotScores: expect.objectContaining({
    pluvial: expect.any(Number),
    fluvial: expect.any(Number),
    coast: expect.any(Number)
  }),
  builidngScore: expect.any(Number),
  buildingScores: expect.objectContaining({
    pluvial: expect.any(Number),
    fluvial: expect.any(Number),
    coast: expect.any(Number)
  }),
  buildings: expect.arrayContaining([
    expect.objectContaining({
      id: expect.any(Number),
      score: expect.any(Number),
      scores: expect.objectContaining({
        pluvial: expect.any(Number),
        fluvial: expect.any(Number),
        coast: expect.any(Number)
      })
    })
  ])
}
export const DATE_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/

CodePudding user response:

You are expecting an array of owners with format ['any owner'] but you are receiving "{\"WATERING OUDE KALE EN MEIREBEEK\"}"

  • Related