Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

347
Vistas
array sort method doesn't work outside cypress each but works inside

Here is the program piece where you can see that:

  1. var likes = [] is defined before cy.get:
  2. likes is populated inside the each () method I want to sort likes array as in the below code. But that is not getting sorted. cy is cypress The problem is that I like to sort this likes array outside cy.get(..).each() as next instruction not inside. But that is not working and I have no clue as to why. Please could you help in here ?

    var likes = []
    
    cy.get('.blogs').each(($el, index) => {
        const bcy = cy.wrap($el)
        bcy.get('#viewBlog').click()
        const like =  Math.floor(Math.random() * 10)
        likes[index] = like
        for(let i=like;i--; )
          bcy.get('.blogsDetail').eq(index).contains('likes').click()
        
      })

     likes.sort((a, b) => b - a)  //Sorting at this place doesn't work

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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)
    bcy.get('#viewBlog').click()
    const like =  Math.floor(Math.random() * 10)
    likes[index] = like
    for(let i=like;i--; )
      bcy.get('.blogsDetail').eq(index).contains('likes').click()
  }).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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda