Skip to content

cy.wait on an array of the same alias does not resolve in expected order #27337

Description

@mike-plummer

Current behavior

cy.wait accepts an array of aliases to await - this behavior works as expected when different aliases as supplied (cy.wait(['@a', '@b'])), however when the same alias is supplied multiple times the resolved subjects are not ordered from first-to-last as one would expect.

For example:

cy.intercept('url').as('@req')

//...multiple calls are made to the intercepted endpoint...

cy.wait(['@req', '@req', '@req']).then((subjects) => {
    // I would expect `subjects` to be in order of incidence (first intercepted request, second, third...)
    // order of `subjects` appears to be arbitrary
	// subjects[0] does not always correspond to the first interception
	// ditto with subjects[1], [2], etc
})

Desired behavior

Users can currently await serially to get items in order:

cy.wait('@req').then((req) => ...do your assertions on the first interception...)
cy.wait('@req').then((req) => ...do your assertions on the second interception...)
cy.wait('@req').then((req) => ...do your assertions on the third interception...)

but this prevents easy comparison of multiple awaited subjects. One workaround that been identified is to wait serially and combine into an array, but this relies on variables and state outside of the Cypress command chain which has inherent risks:

const waitedRequests = []
for (let i = 0; i < 3; i++) {
  cy.wait('@req').then((req) => {
    waitedRequests.push(req)
  })
}

cy.then(() => {
 // `waitedRequests` should contain all three requests in order here
})

Test code to reproduce

https://github.com/mike-plummer/cypress-wait-alias-array

Cypress Version

12.15.0

Node version

16.17.1

Operating System

macOS 13.4.1

Debug Logs

No response

Other

Based off of cypress-io/cypress-support-internal/issues/13

Metadata

Metadata

Labels

E2EIssue related to end-to-end testingReproducibleCan be reproducedTriagedIssue has been routed to backlog. This is not a commitment to have it prioritized by the team.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions