Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs ESLint:';cy&x27;未定义(Cypress)_Reactjs_Eslint_Cypress - Fatal编程技术网

Reactjs ESLint:';cy&x27;未定义(Cypress)

Reactjs ESLint:';cy&x27;未定义(Cypress),reactjs,eslint,cypress,Reactjs,Eslint,Cypress,我刚刚开始在React Typescript项目中使用Cypress。我有一些简单的测试要运行: description('settings page',()=>{ 在每个之前(()=>{ 参观http://localhost:3000') }); 它('在等待状态下启动,没有设置。',()=>{ cy.contains('正在等待设置…') }); 它('收到设置后显示设置',()=>{ const state=cy.window().its('store').invoke('getState

我刚刚开始在React Typescript项目中使用Cypress。我有一些简单的测试要运行:

description('settings page',()=>{
在每个之前(()=>{
参观http://localhost:3000')
});
它('在等待状态下启动,没有设置。',()=>{
cy.contains('正在等待设置…')
});
它('收到设置后显示设置',()=>{
const state=cy.window().its('store').invoke('getState'))
log(state)//不同的问题:如何使它成为state而不是$Chainer?
});
});
它在柏树上跑得很好。但是我在Webstorm中得到了Typescript错误,说没有定义
cy
(TS和ESlint错误),并且在
description
上有一个错误,说
当提供--isolatedModules标志时,所有文件都必须是模块

我可以让它成为一个JS文件而不是TS文件,然后我仍然得到未定义的
cy

我曾尝试从“cypress”导入cy,但后来我得到了“ParseError:“import”和“export”可能只与“sourceType:module”一起出现,后者是一整罐蠕虫(我正在编写测试,还没有导入任何东西…)

//
不起作用。

更新(排序) 我按照指示做了,取得了一些进展。在我已经非常完整的React webpack.config.dev.js中,我添加了建议的代码:

{//为cypress插入了TODOhttps://stackoverflow.com/a/56693706/6826164
规则:[
{
测试:/\.tsx?$/,,
使用“ts加载器”,
排除:/node\u模块/
}
]
},
到规则列表的末尾(就在文件加载程序之前)

当我这样做,并按照文章中的说明设置
插件/索引
文件时,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
上键入一个不是Jest的类型有关,因此它认为
toEqual
匹配器是错误的

一直以来,在Webstorm中,ESLint仍在抱怨我所有的
cy
,TypeScript在输出中提到的所有笑话断言下划线

这都是一个ts测试文件。如果我将文件重命名为js,它会说该文件没有测试


有什么帮助吗?我喜欢柏树,但我有一个地狱的时间让它充分发挥作用

在文件的顶部放置

/// <reference types="cypress" />
//
或下载


来源:

升级到cypress 4+版后,我遇到了这个错误。我安装了
eslint插件cypress
并在package.json或单独的配置文件中的扩展配置中激活它:

"eslintConfig": {
  "extends": [
    "plugin:cypress/recommended"
  ]
},

只需将以下行添加到e2e测试的
tsconfig.json
文件中:

"compilerOptions": {
    "types": ["cypress"]
}

这增加了对cypress类型的支持。

Try<代码>从“cypress”导入cy这为我解决了问题

将.eslintrc.json添加到cypress目录 在
.eslintrc.json中

{
  "extends": [
    "plugin:cypress/recommended"
  ]
}

  • 我没有安装
    eslint插件cypress
    ,它解决了这个问题
在eslintrc globals中指定
cy

cy是一个全局变量。很像地理位置。所以说真的是window.cy。您可以将其添加到Eslint中的globals。不要从柏树上进口cy


将其添加到我的
.eslintrc
中并修复了该问题

Cypress ESLint插件将消除以下警告:

  • 纱线添加-D eslint插件cypress
    ()
  • 使用以下内容将
    .eslintrc
    添加到项目的根目录中:
{
“插件”:[“cypress”],
“扩展”:[“插件:cypress/推荐”],
“规则”:{
“开玩笑/期待”:“关”
}
}

谢谢,但这根本不起作用。我还尝试下载@types/cypress并将其添加到我的tsconfig中,但这对任何问题都没有帮助。我仍然发现这个错误模块构建失败(来自./node\u modules/cypress plugin tab/src/index.js):ReferenceError:cypress未定义您解决了吗?我面临着类似的问题。嗨,欢迎来到堆栈溢出。你可以使用这个链接来改进你的答案,你需要安装这个插件。看起来问题已经解决的原因是ESLint将出错并停止对目录进行lint,因此所有警告都将消失。看起来您正在使用VSCode,因此您可以单击右下角的ESLint选项卡来验证这一点。但我仍然发现此错误模块构建失败(来自./node\u modules/cypress plugin tab/src/index.js):ReferenceError:cypress未定义您可以查看此错误吗
{
  "globals": {
    "cy": true
  }
}