Reputation: 4158
I'm trying to write a Cypress test that validates that a specific request is resolved before another. Here is my code:
it('Module 1 should load before Module 2', () => {
// Delay Module 1 by 5 seconds
cy.intercept('module1.js', (req) => {
req.on('response', (res) => {
res.setThrottle(5000);
});
}).as('module1');
cy.intercept('module2.js').as('module2');
cy.visit('/');
// Module 1 should load before Module 2 even though it's delayed
cy.wait('@module1');
cy.wait('@module2');
});
This test is predicated on cy.wait()
s resolving in order, but that does not appear to be the case. If I switch the two cy.wait()
s to the following, the test still passes.
cy.wait('@module2');
cy.wait('@module1');
I've even tried having the second wait occur in the then()
block of the first, but it still doesn't seem to care about order.
cy.wait('@module2').then(() => {
cy.wait('@module1');
});
How can I test that two requests are resolved in a specific order?
Upvotes: 0
Views: 243
Reputation: 31944
Perhaps timestamp the responses and wait for them together
it('Module 1 should load before Module 2', () => {
// Delay Module 1 by 5 seconds
cy.intercept('module1.js', (req) => {
req.on('response', (res) => {
res.setThrottle(5000);
res.timestamp = Date.now()
});
}).as('module1');
cy.intercept('module2.js', (req) => {
req.on('response', (res) => {
res.timestamp = Date.now()
});
}).as('module2');
cy.visit('/');
// Module 1 should load before Module 2 even though it's delayed
cy.wait(['@module1', '@module2').then(interceptions => {
expect(interceptions[0].timestamp).to.be.lt(interceptions[1].timestamp)
})
});
Upvotes: 3