How to use Mocha and Jest with TypeScript without conflicts?

时光总嘲笑我的痴心妄想 提交于 2021-01-21 07:04:50

问题


I'm trying to install Mocha and Jest with types on one project. We use strict typecheck, so I get errors related to conflicting globals type.

I've tried to create ambiguous module declaration, defining only Mocha in types at tsconfig. I've been trying to remove Jest's declaration - but that would partially help. Disabling strict typecheck or lib check is not an option.

I expected to work it properly, but instead got the next errors.

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'.

Even removing all types from Jest gets me this error:

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'.

回答1:


As of August 2019, this appears to have been fixed on master, but a new version has not been released yet.

In the meantime, I was able to address these errors by adding the following to one of my definitions:

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



回答2:


I searched through package lock file to see which dependencies use @types/jest and found out that in my case I still have ts-jest in my project which I removed and conflicts disappeared



来源:https://stackoverflow.com/questions/56181799/how-to-use-mocha-and-jest-with-typescript-without-conflicts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!