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

143
Views
How to iterate array with vivewports with it tests and beforeeach

I run into issue with viewport issue and I didn't find the solution yet.

describe('Tests', function () {
beforeEach(function () {
});

viewports.forEach((viewport) => {
    it.only(`Test: ${viewport}`, function () {
        cy.viewportAdjust(viewport);
    })})})

I used cy.viewportAdjust(viewport);in my test and it works, however I wish to move cy.viewportAdjust(viewport); to beforeEach section but for tests it uses last value of viewportsArray

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

I'm not entirely sure what cy.viewportAdjust() does, as that looks to be a custom function. If the function simply calls cy.viewport() or updates the viewport, the following will work. Additionally, with not knowing what the values in viewport are, I can't give an exact complete answer, but the general idea is that you can pass in your viewportWidth or viewportHeight in the it() block.

viewports.forEach((viewport) => {
    it.only(`Test: ${viewport}`, { viewportWidth: viewport }, function () {
        cy.viewportAdjust(viewport);
    })
})
about 4 years ago · Santiago Gelvez Report

0

Im going to answer this question exactly how you asked. But I would not suggest doing it this way though without seeing everything that you are trying to do I don't have 100% confidence in this statement.

describe('Tests Viewports', () => {
  const viewports = ['iphone-6', 'ipad-2', 'ipad-mini'] //insert viewport you wish to test here
  let x = 0
  beforeEach( () => {
    cy.viewport(viewports[x])
    //Do a thing
    x += 1
  })
  viewports.forEach( (viewport) => {
    it(`Tests: ${viewport}`, () => {
      cy.log(viewport)
    })
  })
})

I would instead suggest removing the beforeEach block and putting your code within the it block.

about 4 years ago · Santiago Gelvez 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!