Clearing previously intercepted routes
See original GitHub issueWhat would you like?
I’d like to be able to clear previously intercepted routes so that waiting on an aliased intercept will now start from the next interception.
Example:
describe('Interceptions', () => {
it('Resets interceptions', () => {
cy.intercept('GET', '/foo').as('foo')
cy.wait('@foo') // request #1 to /foo
// Get some variable to see current state
// request #2 to #n to /foo // there could potentially be more requests, too
cy.get('button').click()
cy.resetIntercepts('@foo') // would like to have something like this so that request #2 to #n will be forgotten about
// request #n+1 to /foo
cy.wait('@foo') // will yield request #n+1
})
})
Why is this needed?
If the number of requests is unknown beforehand, using cy.wait()
on the same alias is not useful.
For example, I have a request that is called every X seconds, and I want to compare the state after taking some action. There may or may not have been one or more requests between:
- the first request where I record the state that I know won’t change by the time the action is taken, and
- the request after the action was taken
I cannot rely on cy.wait()
being called two times, or three times, etc.
Currently I do the following:
describe('Interceptions', () => {
it('Uses the correct request', () => {
cy.intercept('GET', '/foo').as('foo')
cy.wait('@foo') // request #1 to /foo
// Get some variable to see current state
// request #2 to #n to /foo
cy.get('button').click()
cy.intercept('GET', '/foo').as('bar') // intercept same request, but use a different alias to reference new interceptions without needing to wait on the previous ones
// request #n+1 to /foo
cy.wait('@bar') // will yield request #n+1, but with more complex arguments to cy.intercept could be annoying
})
})
But, as noted, it’s not ideal to created a new cy.intercept()
call and alias for really the same intercept.
Other
#23190 - discussion on the same point
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:6 (1 by maintainers)
Top Results From Across the Web
intercept - Cypress Documentation
All intercepts are automatically cleared before. ... Use cy.wait() with aliasing an intercepted route to wait for the request/response cycle to complete.
Read more >Cypress cy.intercept Problems - Gleb Bahmutov
By the time cy.intercept runs, the call is already in progress, and thus not intercepted. Cypress shows XHR calls by default in its...
Read more >Section 8. Approach Clearance Procedures - FAA
To clear Aircraft 1 to SHANN, ATC must ensure the intercept angle for the intermediate segment at SHANN is not greater than 30...
Read more >A Practical Guide to Intercepting Network Requests in Cypress
route () command that is a predecessor to .intercept() . The previous command was only working with XHR requests, so if your app...
Read more >Cypress Intercept Command - ProgramsBuzz
Intercepts are cleared after every test. ... intercept is used to jam, stub, or spy responses from the route and get answers and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I do believe it’s documented here regarding
cy.wait()
, where it says:So if there were 5 requests matched, no matter when the the 1st use of
cy.wait()
is made, it will wait on the 1st matched request to finish, the 2nd use on the 2nd matched request, etc. And if the nth request already finished before that nth use ofcy.wait()
, the code will immediately continue.It seems that in your case you may have a more determinate behavior where you can wait on that first matched request (albeit it do nothing) as you know there will be just 1 before Trigger 2, while for me the number of matched requests will be unknown. But I do agree that our cases are both similar in that we don’t care about the previous interceptions up until some specific trigger, and adding a feature like this would certainly seem to be useful.
@piotrpalek You are referring to https://github.com/cypress-io/cypress/issues/20397.
The ask in this issue is to clear intercepts mid-test and/or provide a way to verify the intercept call out to verify the requests/responses.