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

如何将Mocha和Jest与TypeScript一起使用而不发生冲突?,typescript,mocha.js,jestjs,jstestrunner,Typescript,Mocha.js,Jestjs,Jstestrunner,我正试图在一个项目上安装Mocha和Jest。我们使用严格的类型检查,所以我得到了与冲突的全局类型相关的错误 我试图创建模棱两可的模块声明,只在typesattsconfig中定义Mocha。我一直试图删除Jest的声明,但这会有部分帮助。禁用严格类型检查或库检查不是一个选项 我希望它能正常工作,但却出现了下一个错误 node_modules/@types/jest/index.d.ts(29,13): error TS2403: Subsequent variable declarations

我正试图在一个项目上安装Mocha和Jest。我们使用严格的类型检查,所以我得到了与冲突的全局类型相关的错误

我试图创建模棱两可的模块声明,只在
types
at
tsconfig
中定义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月,该版本似乎已经确定,但新版本尚未发布

同时,我通过在定义中添加以下内容来解决这些错误:

声明模块'@jest/types/build/Global'{
接口DescribeBase扩展了mocha.SuiteFunction{}
接口ItBase扩展了mocha.TestFunction{}
}

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

你有没有发现这个问题?不幸的是,没有。这完全是sadHi Oleg,谢谢你的回答。我们面临着同样的问题。我应该在何处/向哪个文件添加此语句?我不明白你所说的“我的定义之一”是什么意思。谢谢。@MattG您应该可以将它放在任何文件中,例如您的测试设置文件或类型定义文件。不幸的是,这对我不起作用。仍然看到同样的错误。