我刚刚开始使用 Cypress 和我的 React 类型脚本项目,我已经得到了一些简单的测试运行:
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 遇到了打字错误,说 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文件时,正如文章中指出的那样,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]
实际上,接下来是大量的类型脚本输出,如下所示:
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类型不是 Jest 有关,因此它认为 toEqual匹配器是错误的。
与此同时,在 Webstorm 中,ESLint 仍然在抱怨我的 cy和 TypeScript 在输出中提到的所有 Jest 断言下划线。
这些都是测试文件。如果我将文件重命名为 js,它表示该文件没有测试。
有什么帮助吗? 我爱柏树,但我有一个地狱的时间让它完全工作!