-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Description
What would you like?
I'd like ability to pause intercepted request indefinitely and then resume it at will when I need with response which I need.
Why is this needed?
I'm using @xstate/test for testing. It generates scenarios based on states and events with different paths. Problem I encounter is that when scenario includes race condition, I can't guarantee order of execution inside application. For example, I have two requests: A
and B
. In one scenario A
should settle first, and in another B
should settle first.
Approaches I tried to solve this problem:
- Inspect each scenario beforehand and add delay to every interception, according to their precedence. This works, but only with pretty significant delays (more than a second). This leads to very long time of execution.
- Return promise within intercept callback and resolve this promise outside. For some reason this didn't work as expected at all - requests are still passing through.
Another issue that I have is that there can be multiple responses for request, depending on scenario. Right now I have to inspect each scenario beforehand and stub responses. I'd like to do it on demand when I need to settle response.
If we abstract from model based testing, I'd like to write something like this:
beforeEach(function() {
// intercept request and pause it until resolved
cy.intercept('/api/a').as('a');
cy.intercept('/api/b').as('b');
cy.visit('http://localhost:3000');
});
it('should resolve A first', () => {
// assert loading state
cy.wait('@a').then(/* resolve somehow with specific response */);
// assert `a` result and `b` loading state
cy.wait('@b').then(/* resolve somehow with specific response */);
// assert `b` result
})
it('should resolve B first', () => {
// assert loading state
cy.wait('@b').then(/* resolve somehow with specific response */);
// assert `b` result and `a` loading state
cy.wait('@b').then(/* resolve somehow with specific response */);
// assert `a` result
})