Lifetime of intercept #9395
-
The documentation for
Here are two use cases that I had in mind:
For the second use case, I could create a dynamic intercept that only returns the error on the first try and otherwise passes the real result from the server. But it seems more complicated to me. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 3 replies
-
Here is the solution with the dynamic intercept. The following function can be used to simulate a given number of server errors (by default, one error only). Cypress.Commands.add('simulateNetworkError', (calls = 1, statusCode = 500) => {
cy.intercept(
{ method: 'POST', url: '/graphql' },
(req) => {
if (calls) {
req.reply({ statusCode });
--calls;
}
}
);
}); |
Beta Was this translation helpful? Give feedback.
-
How did you figure this out? I now know why my test set-up wasn't working as expected |
Beta Was this translation helpful? Give feedback.
-
After each test - https://docs.cypress.io/api/commands/intercept.html#Usage
See discussion on this issue: #9302
Not currently, but this is something that would be good to add in the future for advanced use cases. |
Beta Was this translation helpful? Give feedback.
-
Is there a good approach to reducing the time between an intercept getting destroyed and a new intercept getting created? If an entire mock service is setup through intercept, there are a fair amount of instances where an application can make requests to the mocked service between when a given test clears its The issue I'm seeing roughly looks like:
|
Beta Was this translation helpful? Give feedback.
-
Came to dig through the discussions because I too was confused about the lifecycle of intercept.once()
// or
interceptOnce() |
Beta Was this translation helpful? Give feedback.
After each test - https://docs.cypress.io/api/commands/intercept.html#Usage
See discussion on this issue: #9302
Not currently, but this is something that would be good to add in the future for advanced use cases.