如何在 TypeScript 中从 mocha 导入“describe”和“it”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39816482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:22:35  来源:igfitidea点击:

How to import "describe" and "it" from mocha in TypeScript?

typescriptmocha

提问by Ace

By default, when importing mochain TypeScript, it brings in describeand it(and some others) into the global namespace.

默认情况下,mocha在 TypeScript 中导入时,它会将describeit(以及其他一些)引入全局命名空间。

Is there a way to bring in specific imports like import {describe, it} from 'mocha'?

有没有办法引入特定的进口产品import {describe, it} from 'mocha'

回答by Eryk Warren

Install mocha and its types:

安装 mocha 及其类型:

npm install mocha --save-dev
npm install @types/mocha --save-dev

Then, simply import mocha in your test files:

然后,只需在您的测试文件中导入 mocha:

import 'mocha';

describe('my test', () => {
  it('does something', () => {
    // your test
  });
});

回答by jgillich

Since TypeScript 2.0, you can add mochato the typesconfiguration of your tsconfig.jsonand it will always be loaded:

从 TypeScript 2.0 开始,您可以添加mochatypes您的配置中tsconfig.json,它将始终加载:

{
  "compilerOptions": {
    "types": [
      "mocha"
    ]
  }
}

回答by ubershmekel

I was having issues with errors and warnings, the problem stemmed from me renaming tsconfig.jsonto something else which makes Visual Studio Code enter "File Scope" instead of "Explicit Project". That made it impossible to import itwithout a red squiggly. Now that I've renamed the config back to tsconfig.jsonthen import 'mocha';works as Eryk mentioned.

我遇到了错误和警告问题,问题源于我重命名tsconfig.json为其他内容,这使 Visual Studio Code 输入“文件范围”而不是“显式项目”。这使得it没有红色波浪线就无法导入。现在我已经将配置重命名为tsconfig.json然后import 'mocha';像 Eryk 提到的那样工作。

https://code.visualstudio.com/Docs/languages/typescript

https://code.visualstudio.com/Docs/languages/typescript