I'm looking for a way to expect a canceled request with cypress. Any idea? :) I cancel the previous requests if the user sends too many requests and I want to test it. I tried to intercept POST requests with the alias, counted requests,...
Asked
Active
Viewed 228 times
1
-
It would be nice to post the code as text. – Vitalizzare Jun 10 '22 at 16:37
1 Answers
2
Commands are running on a queue which works asynchronously from the javascript.
To assert the counter value after the queue has run, wrap the assertion in a .then()
.
let counter;
When('I click on the refresh button', () => {
cy.wait(2000);
cy.window().then(window => window.msw.worker.stop());
cy.intercept('POST', 'myApi', {
body: {
items: []
}
}).as('search');
client.SearchBar.refresh();
cy.wait('@search').then(interceptor > {
counter++;
console.log('Interceptor', interceptor.request,
'Response', interceptor?.response,
'\nCounter', counter)
});
cy.then(() => {
expect(counter).to.be.not.eq(2);
})
...
})

Paolo
- 3,530
- 7
- 21