Skip to content

Latest commit

 

History

History
24 lines (17 loc) · 1.67 KB

File metadata and controls

24 lines (17 loc) · 1.67 KB

Stubbing window.fetch

Work around inability to control network requests that use window.fetch [#95][issue]

You can spy and stub window.fetch directly, or, to recreate the same Network support experience, remove window.fetch completely and let your application fall back to XMLHttpRequest protocol. If the application does NOT have fallback mechanism, then the Cypress tests can provide it using a polyfill.

See individual spec files in cypress/integration folder.

Spec Description
spy-on-fetch-spec.js Observes calls the application makes using window.fetch via cy.spy()
stub-fetch-spec.js Uses Cypress default network stubbing to intercept fetch calls from the application
control-clock-spec.js Bonus: shows how to "speed-up" application to make Ajax calls by controlling time using cy.clock() and cy.tick()

Stubbing fetch

Cypress wraps the native XMLHttpRequest object to allow observing and stubbing network requests from the application. It also polyfills the native window.fetch method to work via wrapped XMLHttpRequest - this is how we allow network stubbing for applications that use fetch calls. See cypress.json

{
  "experimentalFetchPolyfill": true
}

In the future we plan to move network stubbing into Cypress' proxy layer, allowing much more powerful and complete network control. Watch issue [#95][issue] for progress.