如何将Mocha和Jest与TypeScript一起使用而不会发生冲突?

时间:2019-05-17 07:39:42

标签: typescript mocha jestjs jstestrunner

我正在尝试在一个项目中安装具有类型的Mocha和Jest。我们使用严格的类型检查,因此会出现与冲突的全局类型有关的错误。

我尝试创建模糊的模块声明,仅在types的{​​{1}}中定义Mocha。我一直在尝试删除Jest的声明-但这会有所帮助。不能禁用严格的类型检查或库检查。

我希望它能正常工作,但出现下一个错误。

tsconfig

即使从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'.

2 个答案:

答案 0 :(得分:1)

截至2019年8月,此问题似乎已在master上修复,但是尚未发布新版本。

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

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

答案 1 :(得分:1)

我搜索了包锁定文件以查看哪些依赖项使用 @types/jest,并发现在我的情况下,我的项目中仍然有 ts-jest,我已将其删除并且冲突消失了