Home > front end >  Cypress assert values in array
Cypress assert values in array

Time:10-29

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

CodePudding user response:

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')                     

CodePudding user response:

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.

  • Related