DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk4 min

How to Use a Fetch Polyfill in Cypress Tests

Cypress removed experimentalFetchPolyfill. Use cy.intercept() to control fetch requests, and add an application polyfill only when a supported browser lacks window.fetch.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For current Cypress, remove experimentalFetchPolyfill from your configuration. Cypress removed that option; use cy.intercept() to spy on or control fetch-backed requests in tests. Add a real fetch polyfill only if your application must support a browser that does not provide window.fetch, and load it into the application before code that calls fetch.

First decide whether you need a polyfill or request interception

These solve different problems. A fetch polyfill gives application code a fetch API in a browser that lacks one. Cypress’s cy.intercept() observes, stubs, or modifies HTTP traffic for a test. A polyfill does not itself make a test response deterministic, and interception does not add a missing browser API.

As an Amazon Associate I earn from qualifying purchases.

Need Use Where it runs
The application must run in a supported browser without native fetch. A project-appropriate fetch polyfill. In the application runtime, before code that uses fetch.
A test needs to observe or control an API request. cy.intercept(). In Cypress test setup.

Cypress’s migration guidance says to remove the old setting and use cy.intercept() for fetch requests. Its recipes index calls the relevant example “Stubbing window.fetch.”

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Stub a fetch-backed request with cy.intercept()

Register the route before visiting the app or taking the action that triggers the request. For example:

// In a spec, before the action that triggers the request
cy.intercept('GET', '/api/users', {
  statusCode: 200,
  body: [{ id: 1, name: 'Ada' }],
}).as('getUsers')

cy.visit('/')
cy.get('[data-cy=load-users]').click()
cy.wait('@getUsers')
  .its('response.statusCode')
  .should('eq', 200)

The test waits on the named route after triggering the request, then checks the response status. To use checked-in data instead of an inline response, point the intercept at a fixture:

cy.intercept('GET', '/api/users', { fixture: 'users.json' }).as('getUsers')

Cypress supports serving fixture data through cy.intercept(), so this pattern does not require a live API response. Keep the route in the test that needs it or in a beforeEach hook: Cypress automatically clears intercepts before each test.

Add a polyfill only for an application browser-support requirement

  1. Check the supported browser matrix. Confirm that the application must run in an environment without native fetch. If all supported browsers provide it, a test-only polyfill is unnecessary and can hide a mismatch between the test and real runtime.
  2. Choose a package for your app. The right package and import syntax depend on the project’s browser targets and bundler; Cypress does not prescribe a particular third-party polyfill in the guidance covered here.
  3. Load it through the application’s normal dependency and build process. Ensure it runs before any module or startup code that calls fetch. This keeps the application’s test behavior aligned with its production runtime.
  4. Continue to intercept requests separately. Use cy.intercept() to control the resulting HTTP request or response whether the app uses native fetch or a polyfilled implementation.

Do not assume that importing a package into Cypress’s support file installs it on the application-under-test (AUT) window. The support bundle is test setup, distinct from the app’s runtime. If you use a test-only pre-load hook, verify that the polyfill is present on the AUT’s window before application startup and before the first request. Cypress’s current official guidance does not give a universal third-party polyfill injection snippet, so the precise integration must match your Cypress version and bundler.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Keep shared Cypress setup focused

Cypress support files run before each spec and can hold shared hooks, but imports there are bundled and loaded for every spec. Keep them lean. Put an application polyfill in the application’s own startup path when the application actually needs it; put shared Cypress test setup in the support file only when it belongs to test setup.

Troubleshoot fetch and interception problems

  • experimentalFetchPolyfill errors or has no effect: remove it. It is no longer a supported Cypress configuration option.
  • The request is not intercepted: define the route before the request-triggering action. Also check browser caching: a response served from the browser cache does not reach the network layer, so cy.intercept() will not fire for that request.
  • Fetch fails from Electron’s initial about:blank page: Cypress documents that Electron no longer permits fetch or XMLHttpRequest from that page before navigation. Use cy.request() for a direct Cypress-side request, or navigate with cy.visit() first.
  • The polyfill import appears ineffective: verify that it modifies the AUT’s window, not just Cypress test setup, and that it runs before app code uses fetch. Confirm the exact hook and bundler behavior for your project rather than relying on a universal injection recipe.

Or skip the browser setup

For capturing a rendered website as an image or PDF, ScreenshotNeo offers a separate screenshot API. This does not replace cy.intercept() or install a fetch polyfill in Cypress; it is an option when the task is to capture a page rather than test its network behavior. One GET request returns a screenshot or PDF. See the ScreenshotNeo API documentation.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
  • Cookie and consent banners, newsletter popups, and chat widgets are removed before the shot; each cleanup step can be turned off.
  • Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed; response headers indicate the page verdict and billing status.
  • An MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents.
  • The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots.

Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month with no card.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Frequently Asked Questions

How do I intercept a fetch request in Cypress?

Register cy.intercept() before the app action that sends the request, then wait on the route alias. The example above shows an inline response and a fixture alternative.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Does Cypress still support experimentalFetchPolyfill?

No. Remove the obsolete option; current Cypress guidance directs tests to cy.intercept() for handling fetch requests.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.