typescript - 如何解决 Testcafe 和 Jest 之间的类型冲突? ("Cannot redeclare block-scoped variable ' 测试'")

标签 typescript vue.js jestjs testcafe ts-jest

我有一个使用 Vue CLI 创建的 Vue 2 项目,该项目已经进行了一段时间的 Testcafe 测试。 现在我也尝试添加 Jest 测试。

当我运行我的应用程序 (npm runserve = vue-cli-serviceserve) 时,由于 Jest 和 Testcafe 都声明了全局,我收到以下 Typescript 错误test() 函数:

ERROR in /Users/.../app/node_modules/@types/jest/index.d.ts(44,13):
44:13 Cannot redeclare block-scoped variable 'test'.
    42 | declare var fit: jest.It;
    43 | declare var xit: jest.It;
  > 44 | declare var test: jest.It;
       |             ^
    45 | declare var xtest: jest.It;
    46 |
    47 | declare const expect: jest.Expect;
ERROR in /Users/.../app/node_modules/testcafe/ts-defs/index.d.ts(2397,15):
2397:15 Cannot redeclare block-scoped variable 'test'.
    2395 |
    2396 | declare const fixture: FixtureFn;
  > 2397 | declare const test: TestFn;
         |               ^
    2398 |
Version: typescript 3.7.5

an old Testcafe该问题表示该问题已于 2019 年修复,并且:

Only definitions from the node_modules/@types directory are loaded automatically by default when compiling any TypeScript source file.

但是,正如您在上面看到的,我的配置仍然从 node_modules/testcafe/ts-defs/index.d.ts 加载 Testcafe 类型,即使我没有运行 Testcafe 但只是运行应用程序。

我创建了一个具有相同问题的 Vue 2 应用程序骨架并将其上传到 Github here 。 直到我在 tests/testcafe/Testcafe.spec.ts 添加测试后,才出现类型问题。

我该怎么做才能消除这种类型冲突?我可以做一些简单的事情来停止运行应用程序时加载 Testcafe 类型吗?

最佳答案

在我的项目中,将 "tests/testcafe" 添加到 tsconfig.json 中的 exclude 属性可以阻止运行开发时出现的错误服务器,并且 TestCafe 测试仍然会运行(这是一个惊喜)。所以我想告诉 Typescript(由 Vue CLI/Webpack 运行?)不要编译 Testcafe 测试是一个答案。

我觉得可能还有另一个答案,其中 TestCafe 的类型被忽略,但我不知道如何实现这一点。 (我尝试过几种不同的方法。)

关于typescript - 如何解决 Testcafe 和 Jest 之间的类型冲突? ("Cannot redeclare block-scoped variable ' 测试'"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69341503/

相关文章:

html - Angular 4 Material - mat-button 样式未应用于 mat-dialog 组件

javascript - 如何使用 jest 进入 setTimeout 函数

javascript - 使用 Jest 的问题 - 无法读取未定义的属性 'xxx'

reactjs - 在哪里可以找到 Jest __snapshots__ 目录

typescript - 扩展数组 <number>

javascript - Handsontable:更改下拉菜单宽度

typescript - 每个组件的 Rollup + Typescript 输出 JS 文件

javascript - 如何将 vue 与原始 tcp 节点服务器连接?

vue.js - 将标记注入(inject)现有的 Vue 组件

vue.js - Vue/Bluebird : then callback doesn't run