I'm repeating a cypress testcase with a for loop. Within the testcase I'm trying to use the loop control variable.
As an illustration the following (simplified) example:
for(var i = 0; i < 3; i++) {
it("Loop", () => {
cy.log("Value of i: " + i)
})
}
The following output is generated:
Value of i: 3
Value of i: 3
Value of i: 3
That is not what I expected...
After a little trial and error I found out, that declaring a constant helps:
for(var i = 0; i < 3; i++) {
const loopCount = i
it("Loop", () => {
cy.log("Value of loopCount: " + loopCount)
})
}
This time the following output is generated:
Value of loopCount: 0
Value of loopCount: 1
Value of loopCount: 2
What exactly is happening here? Is using a constant the right way or is there a better approach?