我刚刚开始在我的 React Typescript 项目中使用 Cypress。我已经进行了一些简单的测试:
describe('settings page', () => {
beforeEach(() => {
cy.visit('http://localhost:3000')
});
it('starts in a waiting state, with no settings.', () => {
cy.contains('Waiting for settings...')
});
it('shows settings once settings are received', () => {
const state = cy.window().its('store').invoke('getState')
console.log(state) // different question: how do I get this to be the state and not a $Chainer?
});
});
它在赛普拉斯运行得很好。但是我在 Webstorm 中遇到 Typescript 错误,说 cy
没有定义(TS 和 ESlint 错误),并且在 describe
上出现错误,说 all files must be modules when the --isolatedModules flag is provided
。
我可以将其设为 JS 文件而不是 TS 文件,然后我仍然得到 cy
未定义。
我试过 import cy from 'cypress'
但后来我得到了 ParseError: 'import' and 'export' may appear only with 'sourceType: module'
这是另一种蠕虫罐头(我在编写测试时正在采取婴儿步骤,还没有导入任何东西…)
/// <reference types="cypress" />
不起作用。
更新(有点)
我已按照 此处 的说明进行操作,并取得了一些进展。在我已经非常完整的 React webpack.config.dev.js 中,我添加了推荐的代码:
{ // TODO inserted for cypress https://stackoverflow.com/a/56693706/6826164
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
到规则列表的末尾(就在文件加载器之前)。
当我这样做以及设置 plugins/index
文件时,如文章中所示,赛普拉斯“主屏幕”运行,但是当我单击打开我的测试时,它需要很长时间,然后显示很多错误,从
integration\settings.spec.ts
This occurred while Cypress was compiling and bundling your test code. This is usually caused by:
A missing file or dependency
A syntax error in the file or one of its dependencies
Fix the error in your code and re-run your tests.
./cypress/integration/settings.spec.ts
Module build failed (from ./node_modules/ts-loader/index.js):
Error: TypeScript emitted no output for C:\Users\...\...\front_end\cypress\integration\settings.spec.ts.
@ multi ./cypress/integration/settings.spec.ts main[0]
其次,实际上,很多打字稿输出是这样的:
C:\Users\jtuzman\dev\...\...\src__tests__\Errors.test.tsx
[tsl] ERROR in C:\Users\jtuzman\dev\...\...\src__tests__\Errors.test.tsx(37,41)
TS2339: Property 'toBeTruthy' does not exist on type 'Assertion'.
C:\Users\jtuzman\dev\...\...\src__tests__\Errors.test.tsx
[tsl] ERROR in C:\Users\jtuzman\dev\...\...\src__tests__\Errors.test.tsx(41,45)
TS2339: Property 'toBeDefined' does not exist on type 'Assertion'.
请注意,这些现在是测试文件之外的代码的错误(尽管这可能是有道理的)。其中许多是针对我使用 Jest 而不是 Cypress 的文件,正如您所见,许多错误似乎与推断 — 上的 expect
Assertion
类型有关那不是开玩笑,因此它认为 toEqual
匹配器是错误的。
一直以来,在 Webstorm 中,ESLint 仍在抱怨我所有的 cy
并且 TypeScript 强调了输出中提到的所有 Jest 断言。
这都是一个 ts 测试文件。如果我将文件重命名为 js,则表示该文件没有测试。
有什么帮助吗?我喜欢赛普拉斯,但我很难让它充分发挥作用!
原文由 Jonathan Tuzman 发布,翻译遵循 CC BY-SA 4.0 许可协议
升级到 cypress 版本 4+ 后出现该错误。我安装了
eslint-plugin-cypress
https://github.com/cypress-io/eslint-plugin-cypress 并在 package.json 或单独的配置文件中的扩展配置中激活它: