typescript - 如何在没有冲突的情况下将 Mocha 和 Jest 与 TypeScript 一起使用?

标签 typescript mocha.js jestjs jstestrunner

我正在尝试在一个项目中安装带有类型的 Mocha 和 Jest。我们使用严格的类型检查,所以我会收到与冲突的全局类型相关的错误。

我试图创建模棱两可的模块声明,在 tsconfigtypes 中只定义 Mocha。我一直在尝试删除 Jest 的声明 - 但这会有所帮助。禁用严格的类型检查或库检查不是一个选项。

我希望它能正常工作,但却遇到了下一个错误。

node_modules/@types/jest/index.d.ts(29,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'beforeEach' must be of type 'HookFunction', but here has type 'Lifecycle'.
node_modules/@types/jest/index.d.ts(31,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'afterEach' must be of type 'HookFunction', but here has type 'Lifecycle'.
node_modules/@types/jest/index.d.ts(32,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'describe' must be of type 'SuiteFunction', but here has type 'Describe'.
node_modules/@types/jest/index.d.ts(34,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'xdescribe' must be of type 'PendingSuiteFunction', but here has type 'Describe'.
node_modules/@types/jest/index.d.ts(35,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'it' must be of type 'TestFunction', but here has type 'It'.
node_modules/@types/jest/index.d.ts(37,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'xit' must be of type 'PendingTestFunction', but here has type 'It'.
node_modules/@types/jest/index.d.ts(38,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'test' must be of type 'TestFunction', but here has type 'It'.
node_modules/@types/node/globals.d.ts(926,15): error TS2430: Interface 'Global' incorrectly extends interface 'MochaGlobals'.
  Types of property 'describe' are incompatible.
    Type 'Describe' is not assignable to type 'SuiteFunction'.
      Types of property 'only' are incompatible.
        Type 'DescribeBase' is not assignable to type 'ExclusiveSuiteFunction'.
          Type 'void' is not assignable to type 'Suite'.

即使从 Jest 中删除所有类型也会出现此错误:

node_modules/@types/node/globals.d.ts(926,15): error TS2430: Interface 'Global' incorrectly extends interface 'MochaGlobals'.
  Types of property 'describe' are incompatible.
    Type 'Describe' is not assignable to type 'SuiteFunction'.
      Types of property 'only' are incompatible.
        Type 'DescribeBase' is not assignable to type 'ExclusiveSuiteFunction'.
          Type 'void' is not assignable to type 'Suite'.

最佳答案

截至 2019 年 8 月,这似乎已修复 master , 但尚未发布新版本。

与此同时,我能够通过将以下内容添加到我的定义之一来解决这些错误:

declare module '@jest/types/build/Global' {
  interface DescribeBase extends mocha.SuiteFunction {}
  interface ItBase extends mocha.TestFunction {}
}

关于typescript - 如何在没有冲突的情况下将 Mocha 和 Jest 与 TypeScript 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56181799/

相关文章:

javascript - 什么时候应该在 Typescript 中显式添加类型注释?

如果 Typescript 文件有任何错误,则不应生成 JavaScript 文件

testing - Yeoman webapp 生成器 - 如何在浏览器中运行 mocha 测试

javascript - Jest spyOn() 调用实际函数而不是模拟函数

mongoose - jest 和 mongoose - jest 检测到打开的句柄

node.js - 开 Jest : child_process. exec.mockImplementation 不是一个函数

reactjs - 如何正确拆分调度和状态,以免重新渲染?

javascript - ng build --prod 的 javascript 加载问题

javascript - 如何在 watch 模式下进行任何 mocha 测试之前运行全局设置脚本

node.js - 用于测试的内存 MongoDB?