question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

implement `window.fetch` capturing/mocking

See original GitHub issue

Many developers are migrating to window.fetch from XHRs due to the simplified async interface it exposes. I happen to be one of those developers.

Cypress should support window.fetch with similar abilities to those of XHRs: I should be able to query the request and response, wait on them, and mock with data and fixtures.

Cypress team edit

This feature will work seamlessly once Full network stubbing is implemented.

Workarounds

  • use experimental fetch polyfill available in v4.9.0

or do it yourself:

  • delete window.fetch before visiting the page to force your application to polyfill it on top of XHR. If the application does not include a polyfill, you can load one from your tests.
  • stub window.fetch directly in JavaScript

See these solutions in Stubbing window.fetch example recipe

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:502
  • Comments:119 (15 by maintainers)

github_iconTop GitHub Comments

141reactions
nmchavescommented, Mar 2, 2020

@brian-mann worked like a charm! I just put this in support/index.js:

// use `Cypress` instead of `cy` so this persists across all tests
Cypress.on("window:before:load", win => {
  win.fetch = null;
});
103reactions
lughinocommented, May 12, 2020

Guys, please prioritize this task… it’s open since 2016! In 2020 the entire world use fetch!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using the Fetch API - MDN Web Docs
Here we are fetching a JSON file across the network and printing it to the console. The simplest use of fetch() takes one...
Read more >
Introduction to fetch() - web.dev
The fetch() API is landing in the window object and is looking to replace XHRs.
Read more >
Intercepting JavaScript Fetch API requests and responses
We demonstrate intercepting JavaScript Fetch API calls using monkey patching and the fetch-intercept library.
Read more >
window-fetch - npm
Instead of implementing XMLHttpRequest in Node.js to run browser-specific Fetch polyfill, why not go from native http to fetch API directly?
Read more >
How to Use the Fetch API (Correctly) - CODE Magazine
html file and at the bottom of the page, modify the window.onload function to look like the following code snippet. You're building a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found