Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Mocha and Jest with TypeScript without conflicts?

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'.
like image 907
hypeofpipe Avatar asked May 17 '19 07:05

hypeofpipe


2 Answers

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 {}
}
like image 88
Oleg Vaskevich Avatar answered Nov 09 '22 10:11

Oleg Vaskevich


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

like image 28
dkocich Avatar answered Nov 09 '22 11:11

dkocich