Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
FREEDOM251

Freedom251 › Blog › cy.intercept

How to Use cy.intercept() in Cypress: Match, Wait, Spy, and Stub Requests

By Freedom251 Team · · 5 min read

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

Use cy.intercept() to observe or control HTTP requests made by your application in the browser. Register the route before the page action that triggers it, give it an alias, then use cy.wait('@alias') to synchronize and assert on the request or response. An intercept without a response handler spies on real traffic; a static or dynamic response stubs what the app receives.

Write a basic cy.intercept() test

This pattern registers a GET route before visiting the page, waits for its request/response cycle, and checks the response status:

As an Amazon Associate I earn from qualifying purchases.

cy.intercept('GET', '/api/users').as('getUsers')
cy.visit('/users')
cy.wait('@getUsers').its('response.statusCode').should('eq', 200)

Replace the URL, page action, and expected status with the values for your application. The intercept observes matching front-end application traffic and, because no response is supplied, allows the real server response through.

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

Choose what the intercept should do

Spy on the real request

Register a route without a response handler and alias it. Cypress records matching traffic for waits and assertions while the real server continues to handle the request.

Return a static stub

Provide a string, body, fixture, or StaticResponse to control what the application receives. A static response can specify status, headers, body, delay, throttling, or a forced network error. Stubbing makes test data predictable, but it does not verify that the server returns the stubbed data or exercise that endpoint. Keep real end-to-end coverage where you need to verify the server integration.

Build a response dynamically

Use a route handler to inspect the request and call req.reply() with a response you construct. This is useful when the stub depends on request data.

Inspect or modify a real exchange

Change request fields in a route handler if needed, then call req.continue() to send the request to the server. Its callback can inspect the real response. Calling req.reply() or req.continue() ends propagation to later matching handlers.

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

Match the request you mean

cy.intercept() accepts a URL, a method and URL, or a RouteMatcher object. URL patterns may be exact strings, glob patterns, or regular expressions. String matcher values use minimatch with matchBase: true.

With a matcher object, every property you set must match. Available properties include method, hostname, path, pathname, query, headers, port, https, times, and middleware. If you omit the method, the route can match any HTTP method; specify one when GET, POST, or another method matters to the test.

Repeated query parameters

The query matcher cannot compare all repeated array-style values through one string. For those URLs, match with a regular expression or inspect the values in a route handler using URLSearchParams.getAll().

Wait for the network event and assert on it

Attach an alias with .as('name'), then call cy.wait('@name') after the application action that triggers the request. The wait yields an interception object for that request/response cycle. Assert only on details relevant to the behavior under test, such as the request URL, body, or headers and the response status or body.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
cy.intercept('POST', '/api/orders').as('createOrder')
cy.get('[data-cy=submit-order]').click()
cy.wait('@createOrder').then(({ request, response }) => {
  expect(request.body).to.have.property('productId')
  expect(response.statusCode).to.eq(201)
})

Use an aliased wait instead of a fixed delay when the test needs to synchronize with a request: the wait is tied to the network event rather than an assumed amount of time. Cypress also documents waiting on an array of aliases when a test must wait for multiple requests.

Know which requests cy.intercept() can see

cy.intercept() applies to requests made by the front-end application. A cy.request() call originates in Cypress’s Node process, not as browser application traffic, so an intercept will not observe it. If the test uses cy.request(), assert on that command’s result rather than expecting a browser intercept to catch it. See Cypress’s FAQ for this distinction.

Handle route ordering and test lifecycle

Cypress clears intercept routes before each test. Define the routes again in every test that needs them, typically before the visit or UI action that triggers the request.

When routes overlap, ordinary handlers are generally processed in reverse definition order. Routes with middleware: true run first. If the wrong handler seems to win, inspect the Routes display in the Cypress Command Log and check both the registration order and matcher specificity.

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

Troubleshoot an intercept that does not fire

  • The request happened before registration: move cy.intercept() above the visit or action that triggers the request.
  • The method or URL does not match: verify the browser’s actual method, hostname, path, query string, and any matcher properties. Remember that omitting the method allows any method, while every property you do set must match.
  • The request comes from cy.request(): it is Node-originated traffic, not a front-end browser request. Use the cy.request() result for assertions.
  • A route was defined in another test: intercepts are cleared before each test, so register it again.
  • An overlapping route handles it first: inspect the Command Log’s Routes display and review reverse registration order and any middleware: true matchers.
  • Behavior differs across Cypress versions: consult the native network interception guide for version-sensitive details. It notes that before Cypress 16, application requests used the legacy network path; it does not establish a complete current browser compatibility matrix. See Cypress’s native interception guide.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use ScreenshotNeo when the task is capturing a page, not testing its network calls

cy.intercept() is for Cypress network tests; it is not a screenshot API. If you instead need a website screenshot, ScreenshotNeo can capture a URL with one GET request:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for request options. It removes cookie/consent banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, failed loads, timeouts, and cache hits are not billed. It also offers an MCP server for AI agents, including Claude, Cursor, and other MCP clients. The Free plan includes 1,000 shots per month without a card; paid plans start at $5 for 3,000 shots. Sign up for ScreenshotNeo and get 1,000 free screenshots a month, with no card required.

Frequently Asked Questions

Does cy.intercept() automatically stub a request?

No. Without a response handler it spies on matching traffic and lets the real server response through; provide a response or call req.reply() to stub it.

Can I wait for more than one intercept alias?

Yes. Cypress documents passing an array of aliases to cy.wait() when you need to wait for multiple requests.

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

Where should I check current cy.intercept() behavior for my Cypress version?

Use the official API reference and network-request guide, and consult the native interception guide for version-sensitive behavior.

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.

cy.interceptCypressEnd-to-End TestingJavaScript testingNetwork requests

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.

Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

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.