1

I am trying to assert the value's in an array. At this moment I made it to assert just the length:

cy.get('@UrlAndAppendices')
  .its('request.body.correctionInstructionAppendices')
  .should('have.length', 2)

What is the best way to compare this? I can make an deep equal with a fixture. But I dont think that this would be the cleanest solution for assertin just 2 value's in a array.

Result in cypress

Ferando
  • 141
  • 8
E. E. Ozkan
  • 149
  • 10
  • Does this answer your question - https://stackoverflow.com/questions/58110351/how-can-i-compare-arrays-in-cypress – user9847788 Oct 28 '22 at 11:36

2 Answers2

2

For something as simple as two items, best readability (more concise code) is to assert inline.

Length of 2 is implied in the deep.eq check (i.e 1 item would fail, and 3 items would fail).

cy.get('@UrlAndAppendices')
  .its('request.body.correctionInstructionAppendices')
  .should('deep.eq', ['abc', '123'])                     

or this way

cy.get('@UrlAndAppendices')
  .its('request.body.correctionInstructionAppendices')
  .should('include', 'abc')                     
  .and('include', '123')                     
Ferando
  • 141
  • 8
0

If the values will not change in the array then you can create a variable to check against.

const arrValues = [2, 3]
cy.get('@UrlAndAppendices')
  .its('request.body.correctionInstructionAppendices')
  .should('have.length', 2)
  .and('deep.equal', arrValues)

Here is an example with using a fixture instead.

jjhelguero
  • 2,281
  • 5
  • 13