问题描述
我一直在使用 react-testing-library
以及 @testing-library/jest-dom/extend-expect
.我昨天安装了 Cypress,现在我所有的玩笑匹配器都出现了 Typescript 错误:
I had been using react-testing-library
as well as @testing-library/jest-dom/extend-expect
. I installed Cypress yesterday, and now I'm getting Typescript errors on all my jest matchers:
断言"类型上不存在属性toEqual".你的意思是平等"吗?
它看起来像是从错误的断言库中获取了 expect
的类型?此外,expect(...).to.equal(...)
甚至也不起作用.
It looks like it's getting the type of expect
from the wrong assertion library or something? Also, expect(...).to.equal(...)
doesn't even work either.
我实际上尝试安装 @types/jest
并且 yarn 似乎已经成功但它没有列在我的 package.json
的 devDependencies
.
I actually tried installing @types/jest
and yarn appears to have succeeded but it's not listed in my package.json
's devDependencies
.
这是我的 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"
]
}
我还要提一下,我在 cypress 测试中的所有 cy
调用都收到来自 ESLint 的 cy is not defined
错误.
I'll also mention that all my cy
calls in my cypress tests are getting a cy is not defined
error from ESLint.
推荐答案
我昨天遇到了这个问题.看来你说的是对的,cypress 和 jest 都声明了 expect
的类型.在这种情况下,cypress 声明似乎是被选中的声明.这是 cypress 存储库中关于此问题的一个问题:
I ran into this problem yesterday. It seems that what you are saying is correct, cypress and jest both declares types for expect
. In this case the cypress declaration seem to be the one that is picked up. Here's an issue from the cypress repo regarding this:
https://github.com/cypress-io/cypress/issues/1603
那里提到的解决方案对我有用.您需要从 tsconfig.json
中排除 jest 规范文件,然后声明一个 tsconfig.spec.json
,其中再次明确包含它们.
The solution mentioned in there worked for me. You need to exclude the jest spec files from in tsconfig.json
and then declare a tsconfig.spec.json
where they are explicitly included again.
tsconfig.json:
tsconfig.json:
{
...,
"exclude": [
"**/*.spec.ts"
]
}
tsconfig.spec.json:
tsconfig.spec.json:
{
"extends": "./tsconfig.json",
"include": [
"**/*.spec.ts"
],
...
}
有了这个,我的(angular 8)应用程序编译得很好,我可以毫无问题地运行 jest 测试.这是问题中提到的另一个示例,正在实施类似的修复:
With this in place, both my (angular 8) app compiles fine and I can run the jest tests without issue. Here's another example mentioned in the issue with a similar fix being implemented:
这篇关于赛普拉斯在玩笑断言中导致类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!