1

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,...

image

Paolo
  • 3,530
  • 7
  • 21

1 Answers1

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