赛普拉斯在玩笑断言中导致类型错误

Jon*_*man 24 typescript jestjs cypress

我一直在使用react-testing-library,以及@testing-library/jest-dom/extend-expect。我昨天安装了 Cypress,现在我所有的玩笑匹配器都收到 Typescript 错误:

Property 'toEqual' doesn't exist on type 'Assertion'. Did you mean 'equal'?

看起来它是expect从错误的断言库中获取类型的?此外,expect(...).to.equal(...)甚至都不起作用。

我实际上尝试安装@types/jest,纱线似乎已经成功,但它没有列在我package.jsondevDependencies.

这是我的 tsconfig

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "noImplicitAny": false,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": false,
    "noEmit": true,
    "jsx": "react",
    "skipDefaultLibCheck": true,
    "types": [
      "node",
      "cypress",
      "jest"
    ]
  },
  "include": [
    "src"
  ]
}
Run Code Online (Sandbox Code Playgroud)

我还要提到我cy在 cypress 测试中的所有调用都cy is not defined从 ESLint收到错误。

Mar*_*eca 73

在github 问题“New Cypress 10 global TypeScript type因 Jest Expect 冲突”中遇到此问题的任何人都有一个解决方法

通过排除./cypress.config.ts您的tsconfig.json

"exclude": [
  "./cypress.config.ts",
  //other exclusions that may help https://github.com/cypress-io/cypress/issues/22059#issuecomment-1428298264
  "node_modules",
  "cypress",
  "**/*.cy.tsx"
]
Run Code Online (Sandbox Code Playgroud)

来自赛普拉斯开发者:

对于 v10,我们没有对全局变量进行太大改变。对于我重现的所有案例,它是通过将 ./cypress.config.ts 添加到 tsconfig.exclude 属性来修复的。由于 cypress.config.ts 包含在类型检查中,因此它正在加载 cypress 类型,这会污染您的玩笑测试。

  • 这!而且我还必须在 eslint 配置中“排除”所有 cypress 测试文件,因为这些文件还包含“cypress”,它会污染笑话类型。 (3认同)
  • 我收到“找不到名字‘cy’。” (2认同)

小智 27

终于我找到了解决方案!只需添加"cypress"exclude您的 main 的属性中tsconfig.json

{
  ...YOUR_CONFIGS,
  "compilerOptions": {
    ...YOUR_CONFIGS,
    "typeRoots": [ // THIS TO GET ALL THE TYPES
      "node_modules/@types"
    ],
  },
  "exclude": ["cypress"], 
}
Run Code Online (Sandbox Code Playgroud)

您还应该tsconfig.json为您的 cypress 测试添加另一个。您可以创建一个 cypress 文件夹并将其添加到tsconfig.json其中。以下是我的赛普拉斯tsconfig.json

{
  "compilerOptions": {
    "strict": true,
    "baseUrl": "../node_modules",
    "target": "es5",
    "lib": ["es5", "dom"],
    "types": ["cypress", "@testing-library/cypress"]
  },
  "include": ["./**/*.ts"]
}
Run Code Online (Sandbox Code Playgroud)


Joh*_*son 23

我昨天遇到了这个问题。看来你说的是对的,cypress 和 jest 都声明了expect. 在这种情况下,cypress 声明似乎是被选中的声明。这是 cypress repo 中关于此问题的一个问题:

https://github.com/cypress-io/cypress/issues/1603

那里提到的解决方案对我有用。您需要从 in 中排除 jest 规范文件tsconfig.json,然后再次声明tsconfig.spec.json它们被明确包含的位置。

tsconfig.json:

{
  ...,
  "exclude": [
    "**/*.spec.ts"
  ]
}
Run Code Online (Sandbox Code Playgroud)

tsconfig.spec.json:

{
  "extends": "./tsconfig.json",
  "include": [
    "**/*.spec.ts"
  ],
  ...
}
Run Code Online (Sandbox Code Playgroud)

有了这个,我的(angular 8)应用程序编译得很好,我可以毫无问题地运行 jest 测试。这是问题中提到的另一个示例,其中正在实施类似的修复:

https://github.com/neiltownsley/react-typescript-cypress/pull/1/files#diff-e5e546dd2eb0351f813d63d1b39dbc48R29

  • @foakesm 抱歉,如果上面的文字不清楚。基础 `tsconfig.json` 排除 _jest_ `.spec` 文件,然后 `tsconfig.spec.json` (jest 使用的)包含它们。为此,我们假设您的 Cypress 测试不在名为“*.spec.ts”的文件中。这是否回答你的问题? (2认同)

Hel*_*rld 17

这在我的 tsconfig.json 中对我有用

我必须添加

  "include": ["src/**/*"],
Run Code Online (Sandbox Code Playgroud)

完整代码在这里

{
  "compilerOptions": {
    "outDir": "./dist/",
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es5",
    "jsx": "react",
    "allowJs": true,
    "allowSyntheticDefaultImports" : true,
    "esModuleInterop" : true,
    "sourceMap": true,
    "experimentalDecorators": true
  },
  "include": ["src/**/*"],
  "exclude": [
    "node_modules",
    "**/*.spec.ts"
  ]
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*son 17

有一个官方的 Cypress repo 展示了如何解决这个确切的问题https://github.com/cypress-io/cypress-and-jest-typescript-example

我无法在我的一个项目中使用该方法,因此我将其用作解决方法:

import { expect } from '@jest/globals';
Run Code Online (Sandbox Code Playgroud)


小智 13

有多种情况会导致此问题。就我而言,它是通过将 ./cypress.config.ts 添加到 tsconfig.exclude 属性来修复的。由于 cypress.config.ts 包含在类型检查中,因此它正在加载 cypress 类型,这会污染您的玩笑测试。我相信如果您使用 Typscript 进行 cypress 配置,它会对您有所帮助。

  • 好吧,似乎这是自 Cypress v10 以来唯一可能的解决方案。他们建议排除 `cypress.config.ts` 并将其包含在 `<root>/cypress/tsconfig.ts` 中(如果您为 Cypress ofc 使用单独的 tsconfig)。https://github.com/cypress-io/cypress/issues/22059 (2认同)

Gre*_*Woz 12

可以通过三个步骤让它消失。

  1. 在您的/cypress/目录中添加tsconfig.json.

  2. 粘贴以下内容,如本示例所示

    {
        "extends": "../tsconfig.json",
        "compilerOptions": {
           "noEmit": true,
           "types": ["cypress"]
        },
        "include": [
            "../node_modules/cypress",
            "./**/*.ts"
        ]
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在你原来的 tsconfig.json添加

    {
      ...
      "exclude": ["cypress"]
      ...
     }
    
    Run Code Online (Sandbox Code Playgroud)

到顶层配置。就是这样。

一些额外的注意事项:

  1. 如果您cypress/tsconfig.json抱怨,include, exclude您可能需要覆盖它们的值,因为它们是从原始文件继承的。就我而言添加

    "exclude": [],
    
    Run Code Online (Sandbox Code Playgroud)

    ...已经解决了问题。

  2. .ts可能很简单,但请记住从现在开始使用适合您规范的文件扩展名。


Udi*_*zor 5

解决此问题的最简单方法是将以下行添加到 tsconfig.json:

  "include": [
    "src/**/*.ts"
  ],
Run Code Online (Sandbox Code Playgroud)

我附上我的 tsconfig 文件以便更好地理解。您可以在第 3-5 行看到添加内容。

{
  "compileOnSave": false,
  "include": [
    "src/**/*.ts"
  ],
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}
Run Code Online (Sandbox Code Playgroud)