我刚刚开始在我的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?
});
});
在Cypress中运行正常。但是在Webstorm中我收到TypeScript错误,说cy
未定义(TS和ESLint错误),并且在--isolatedModules
标志提供的情况下,对describe
的错误说所有文件必须是模块
。
我可以将其变为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://dev59.com/VlQJ5IYBdhLWcg3wiWab#56693706
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
在规则列表的末尾(文件加载器之前)进行此操作。
当我按照文章中指示设置plugins/index
文件时,Cypress“主屏幕”会运行,但是当我点击打开我的测试时,需要很长时间,然后显示大量错误,从以下内容开始:
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]
随后,实际上,有很多TypeScript输出,类似于这样:
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的文件,而且像您所看到的那样,许多错误似乎与它推断出来的期望
上的Assertion
类型与Jest不同,导致它认为toEqual
匹配器是错误的。同时,在Webstorm中,ESLint仍在抱怨我所有的
cy
,而TypeScript则在下划线下面显示了所有在输出中提到的Jest断言。这全部都是在一个ts测试文件中的。如果我将文件重命名为js,则它会说文件中没有测试。
有帮助吗?我喜欢Cypress,但我在让它完全工作方面遇到了巨大的麻烦!