Cypress.minimatch
Cypress 自动包含 minimatch 并将其暴露为 Cypress.minimatch
。
使用 Cypress.minimatch
可以测试字符串是否符合 glob 模式。
语法
Cypress.minimatch(target: string, pattern: string, options?: MinimatchOptions);
用法
正确用法
Cypress.minimatch('/users/1/comments/2', '/users/*/comments', {
matchBase: true,
})
错误用法
cy.minimatch() // 错误,不能链式调用 'cy'
示例
默认情况下,Cypress 使用 minimatch
来测试请求 URL 是否符合 glob 模式。
如果你在编写正确的模式时遇到困难,可以直接在开发者工具控制台中进行测试以加快迭代速度。
// 测试你编写的 glob 是否匹配请求的 url
// 返回 true
Cypress.minimatch('/users/1/comments', '/users/*/comments', {
matchBase: true,
})
// 返回 false
Cypress.minimatch('/users/1/comments/2', '/users/*/comments', {
matchBase: true,
})
我们添加了 { matchBase: true }
选项,因为 Cypress 在底层默认使用该选项。
现在让我们测试 **
的支持情况。
// ** 匹配所有后续路径段
// 返回 true
Cypress.minimatch('/foo/bar/baz/123/quux?a=b&c=2', '/foo/**', {
matchBase: true,
})
// 而 * 只匹配下一个路径段
// 返回 false
Cypress.minimatch('/foo/bar/baz/123/quux?a=b&c=2', '/foo/*', {
matchBase: false,
})