Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

349
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda