array’s sorting method doesn’t work outside cypress but it works inside

Cypress commands are asynchronous. To sort the array after the each command, you have to use a then callback:

var likes = []
  cy.get('.blogs').each(($el, index) => {
    const bcy = cy.wrap($el)
    const like =  Math.floor(Math.random() * 10)
    likes[index] = like
    for(let i=like;i--; )
  }).then(() => {
    likes.sort((a, b) => b - a)

Wheny you call the sort method out of a then, it’s executed before any cypress command and you have an empty array at that moment. See it in debugger:enter image description here

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top