Created
May 22, 2025 17:00
-
-
Save danguilherme/483298c8cdbe212301d638d13d1b61f6 to your computer and use it in GitHub Desktop.
Support baseUrl in `cy.intercept`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const urlHasProtocol = (url: string) => { | |
| return url.match(/^([a-z]*:\/\/)/i) !== null; | |
| }; | |
| const isRelativePath = (arg: unknown) => | |
| typeof arg === "string" && !urlHasProtocol(arg); | |
| const isHttpMethod = (arg: unknown) => | |
| typeof arg === "string" && | |
| ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"].includes(arg); | |
| /** | |
| * Overwrite the `cy.intercept` command to support `baseUrl` with a subpath. | |
| * | |
| * TODO: remove this if/ | |
| when Cypress properly supports baseUrl with subpaths | |
| * https://discord.com/channels/755913899261296641/1064946933187235880/threads/1367880984753737839 | |
| */ | |
| Cypress.Commands.overwrite("intercept", (originalIntercept, ...args) => { | |
| // #region All possible overloads: https://docs.cypress.io/api/commands/intercept#Syntax | |
| // -> spying only | |
| // cy.intercept(url); | |
| // cy.intercept(method, url); | |
| // cy.intercept(routeMatcher); | |
| // -> spying and response stubbing | |
| // cy.intercept(url, staticResponse); | |
| // cy.intercept(method, url, staticResponse); | |
| // cy.intercept(routeMatcher, staticResponse); | |
| // cy.intercept(url, routeMatcher, staticResponse); | |
| // -> spying, dynamic stubbing, request modification, etc. | |
| // cy.intercept(url, routeHandler); | |
| // cy.intercept(method, url, routeHandler); | |
| // cy.intercept(routeMatcher, routeHandler); | |
| // cy.intercept(url, routeMatcher, routeHandler); | |
| // #endregion | |
| const baseUrl = Cypress.config("baseUrl"); | |
| if (!baseUrl) { | |
| return originalIntercept(...args); | |
| } | |
| const addBaseUrl = (baseUrl: string, path: string) => { | |
| const pathWithoutLeadingSlash = path.replace(/^\//, ""); | |
| const baseUrlWithoutTrailingSlash = baseUrl.replace(/\/$/, ""); | |
| return `${baseUrlWithoutTrailingSlash}/${pathWithoutLeadingSlash}`; | |
| }; | |
| // If the first argument is a route matcher object... | |
| if (typeof args[0] === "object" && "url" in args[0]) { | |
| const [routeMatcher, ...otherArgs] = args; | |
| if (typeof routeMatcher.url !== "string") { | |
| console.warn( | |
| "Cypress intercept overwrite: routeMatcher.url is not a string, cannot add baseUrl: ", | |
| routeMatcher.url, | |
| args, | |
| ); | |
| // if the url is not a string (ie. a RegExp), we can't modify it | |
| return originalIntercept(...args); | |
| } | |
| const newRouteMatcher = { | |
| ...routeMatcher, | |
| url: addBaseUrl(baseUrl, routeMatcher.url as string), | |
| }; | |
| return originalIntercept(newRouteMatcher, ...otherArgs); | |
| } | |
| const newArgs = args.map((arg, index) => { | |
| if (index >= 2) { | |
| // only the first two arguments can be the URL | |
| return arg; | |
| } | |
| if (isHttpMethod(arg)) { | |
| return arg; | |
| } | |
| return isRelativePath(arg) ? addBaseUrl(baseUrl, arg as string) : arg; | |
| }) as typeof args; | |
| return originalIntercept(...newArgs); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment