- offline-spec.cy.js simulates an offline network connection and tests how the application handles it
A browser going offline changes two separate things, and a test has to simulate both:
- what the application believes about connectivity —
window.navigator.onLineplus theonlineandofflineevents. DefiningonLineas an own property onwindow.navigatorshadows the getter inherited fromNavigator.prototype, and dispatching the matching event lets the application re-render. - the requests themselves failing —
cy.intercept(). Where an entire test is offline, the static{ forceNetworkError: true }response is enough. Where connectivity has to flip mid-test, a request handler that callsreq.destroy()while a flag is set gives you both states from a single route.
Neither touches the browser's real network stack, so nothing here is specific to one browser engine. CI runs these tests in Electron, Chrome, and Firefox.
Because requests fail at the proxy rather than at the network interface, the browser still makes them. These tests assert that the application attempted a request and handled the failure, not that nothing left the machine.
