'cypress test asserting a filtering operation on array of objects expecting undefined

I have a cypress test over an API. When I do

cy.getUnits(u.email)

I'm expecting a 200 OK response similar to this in the body of the response

{
  “units”: [
 {
      “store: “US_1234567890”,
      “role”: “user”,
      “email”: “[email protected]”
    },
   {
     “store: “CA_865890X”,
      “role”: “admin”,
     “email”: “[email protected]”
   }
  ]
}

I'm expecting that list of units doesn't contain the unit "u" below

const u =    {
     “store: ” US_56890765",
      “role”: “admin”,
      “email”: “[email protected]”
   }

So in cypress, I did

cy.getUnits(u.email)
 .should(response => expect(response.status).to.equal(200))
 .its(‘body’)
 .should(units => {
  expect(units.length).to.equal(2)
  units.filter(
   unit =>
    unit.email === u.email &&
    unit.role === u.role &&
    unit.store === u.store,
  ).should.be.undefined
 })

That is not working

units.filter(
       unit =>
        unit.email === u.email &&
        unit.role === u.role &&
        unit.store === u.store,
      ).should.be.undefined

I have tried different variations to no avail.

Could someone please help me find a solution to this?

I really appreciate any help you can provide.



Solution 1:[1]

You want to check the filtered array length

.should(units => {
  expect(units.length).to.equal(2)
  const filtered = units.filter(unit =>
    unit.email === u.email &&
    unit.role === u.role &&
    unit.store === u.store
  )
  expect(filtered).to.have.length(0)
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Fody