Jest 忽略 Cypress 测试

den*_*xic 2 jestjs cypress

我有一个create-react-app应用程序,曾经jest用于测试,但我正在慢慢迁移到cypress.

问题是,现在当我运行jest测试时,它包含我的cypress测试并给出错误

ReferenceError: Cypress is not defined
Run Code Online (Sandbox Code Playgroud)

我怎样才能让我的jest(命名约定*.test.js)测试忽略我的cypress测试(通常称为*.spec.js)?

You*_*gla 8

你应该testPathIgnorePatterns在你的笑话配置中使用。

在执行测试之前与所有测试路径匹配的正则表达式模式字符串数组。如果测试路径匹配任何模式,它将被跳过。

根据Jest 配置文档

开玩笑的配置文件

module.exports = {
   // Your normal jest config settings
   testPathIgnorePatterns: ["<rootDir>/cypress/"],
}
Run Code Online (Sandbox Code Playgroud)


den*_*xic 3

在您的jest/config.js或任何您有笑话配置(可能是包)的地方,添加以下内容来替换默认的正则表达式以从中查找测试

"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$"
Run Code Online (Sandbox Code Playgroud)

到:

"testRegex": "(/__tests__/.*|(\\.|/)(test))\\.[jt]sx?$"
Run Code Online (Sandbox Code Playgroud)