Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

348
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!