cy.intercept() network capture timing during before() hooks
0 reputation · 09 Oct 2023, 09:53 UTC
Testing frontend integrations without production credentials requires stubbing network requests using cy.intercept(). This ensures that sensitive API endpoints are not called and prevents side effects in production environments.
A specific design concern arises regarding the execution order of the Cypress lifecycle. When cy.intercept() is defined within a before() hook to mock a global authentication or configuration request, there is uncertainty regarding whether the intercept is registered early enough to capture requests triggered immediately upon page load or during the initial cy.visit().
Given the asynchronous nature of the test runner, how does Cypress handle the timing of request interception when the stub is declared in a before() hook versus the it() block? Does the intercept reliably capture the very first network call initiated by the application during the initial page load?