如何在 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
How to import "describe" and "it" from mocha in TypeScript?
提问by Ace
By default, when importing mocha
in TypeScript, it brings in describe
and it
(and some others) into the global namespace.
默认情况下,mocha
在 TypeScript 中导入时,它会将describe
和it
(以及其他一些)引入全局命名空间。
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 mocha
to the types
configuration of your tsconfig.json
and it will always be loaded:
从 TypeScript 2.0 开始,您可以添加mocha
到types
您的配置中tsconfig.json
,它将始终加载:
{
"compilerOptions": {
"types": [
"mocha"
]
}
}
回答by ubershmekel
I was having issues with errors and warnings, the problem stemmed from me renaming tsconfig.json
to something else which makes Visual Studio Code enter "File Scope" instead of "Explicit Project". That made it impossible to import it
without a red squiggly. Now that I've renamed the config back to tsconfig.json
then import 'mocha';
works as Eryk mentioned.
我遇到了错误和警告问题,问题源于我重命名tsconfig.json
为其他内容,这使 Visual Studio Code 输入“文件范围”而不是“显式项目”。这使得it
没有红色波浪线就无法导入。现在我已经将配置重命名为tsconfig.json
然后import 'mocha';
像 Eryk 提到的那样工作。