无法在Cypress 6.2.1中使用带截获的minimatch
我对cy.intercept有一个问题,我似乎不知道如何解决,在我们的项目中,我们曾经使用route,但现在尝试开始使用intercept。所以,我们的死记硬背过去是这样定义的:无法在Cypress 6.2.1中使用带截获的minimatch,cypress,Cypress,我对cy.intercept有一个问题,我似乎不知道如何解决,在我们的项目中,我们曾经使用route,但现在尝试开始使用intercept。所以,我们的死记硬背过去是这样定义的: 'upload: {route: `api/cases/import-data/**`, alias:'upload'} 因此,我们使用minimatch的**在导入数据之后处理任何内容/,而这目前不适用于intercept 所以我现在尝试使用RegExp,所以我的问题很简单,我应该在RegExp中用什么替换**?似
'upload: {route: `api/cases/import-data/**`, alias:'upload'}
因此,我们使用minimatch的**
在导入数据之后处理任何内容/
,而这目前不适用于intercept
所以我现在尝试使用RegExp,所以我的问题很简单,我应该在RegExp中用什么替换**
?似乎什么都没用,我收到一个超时错误
我的职能:
function listener({ route, method = 'POST', onRes = () => { }, alias = 'listener' }) {
const url = new RegExp(`${Cypress.config().beUrl}${route}`);
cy.intercept(
method,
url,
(req) => {
req.reply(res => {
onRes(res);
});
}
).as(alias);
}
使用regex,您不需要任何东西来替换minimatch“**”<代码>“api/cases/import data/**”可以转换为
/api\/cases\/import data/
但请注意逃过的前斜杠,我认为这是这个过程中缺少的成分
const url = new RegExp(`${Cypress.config().beUrl}${route}`)
用于构建正则表达式的字符串不包含路径分隔符的反斜杠转义
这可能有用
const escapedBaseUrl = Cypress.config().beUrl.replace('/', '\/');
const escapedRoute = route.replace('/', '\/');
const url = new RegExp(`${escapedBaseUrl}${escapedRoute}`);
但是你迟早会遇到其他需要转义的字符,所以你可以使用一个包,比如
因此,我最终找到了问题所在,基本上,我不需要像这样使用
\w+
:api\/cases\/import data\/\\w+$
并将url和方法作为对象属性传递,比如:thiscy.intercept({url,method,matchUrlaginstPath:false}
,也就是replace
不会替换双斜杠,例如http://
,所以使用了replaceAll
。谢谢你的回答,帮了我一点忙!
const RegexEscape = require("regex-escape");
...
const url = new RegExp(RegexEscape(`${Cypress.config().beUrl}${route}`));