如何在没有冲突的情况下将Mocha和Jest与TypeScript一起使用? [英] How to use Mocha and Jest with TypeScript without conflicts?

查看:221
本文介绍了如何在没有冲突的情况下将Mocha和Jest与TypeScript一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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

推荐答案

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

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 {}
}

这篇关于如何在没有冲突的情况下将Mocha和Jest与TypeScript一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆