cy.intercept() not intercepting fetch requests every 2nd or 3rd request
See original GitHub issueCurrent behavior
I noticed that while most of the time fetch requests were being intercepted, other times they were not, causing my tests to fail when wait timed out.
I ended up taking one of the tests and copying it many times to see if there was any pattern.
Each test has 2 fetch requests (only 1 of which is attempted to be intercepted), and at least every 3rd tests fails, meaning no more than 2 tests run successfully one after the other.
Running the test 22 times: 14 passed, 8 failed with Timed out retrying after 5000ms: cy.wait() timed out waiting 5000ms for the 1st request to the route: foo. No request ever occurred.
even though the logs show the request was in fact made
Desired behavior
cy.intercept()
should intercept all matched routes from fetch requests
Test code to reproduce
// api.js
module.exports = {
create: () => {
return fetch('/foo', {
method: 'POST',
body: JSON.stringify({
qux: 'qux'
}),
headers: {
'Content-Type': 'application/json'
}
}
},
delete: ({ id } ) => {
return fetch(`/foo/${id}`, {
method: 'DELETE',
}
}
}
describe('Intercept fetch', () => {
it('Test intercept fetch', () => {
cy.intercept('POST', '/foo').as('foo')
api.create()
// do some awesome test
cy.wait('@foo').then(interception => {
api.delete({ id: interception.response.body.id })
})
})
// repeat test at least 2 more times
})
Cypress Version
10.3.0
Other
No response
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
To your point, I do not think this is well documented. FWIW we usually encourage things like this to be chained off of the existing command. I’ll talk with the team to see if this is something we want to be more of a known behavior.
I was just confused because you were saying to use
cy.then()
which I thought was invalid since according to the documentation:I see an older issue (#466) where it was planned to convert
.then
into a child command, but this never seemed to have actually happened.So either the documentation is wrong or
cy.then()
is invalid, but in either case, the documentation should be updated to reflect that.