javascript Mocha 在 WebStorm 11 中描述“缺少 require()”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33599983/
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-10-28 16:40:24  来源:igfitidea点击:

Mocha's describe "require() is missing" in WebStorm 11

javascriptnode.jsmochawebstorm

提问by BanksySan

I've just installed WebStorm 11 and created a new project, however WebStorm is saying that a require()is needed for the describemethod:

我刚刚安装了 WebStorm 11 并创建了一个新项目,但是 WebStorm 说require()describe方法需要a :

require() call is missing

缺少 require() 调用

I've added the definitely-types mocha library and the scope is for the test directory (which this file is in).

我已经添加了绝对类型的 mocha 库,范围是测试目录(这个文件所在的目录)。

回答by BamaPookie

Edit: As noted in the comments, the actual issue addressed in this question is actually a bug in IntelliJ 15 / WebStorm 11 (see https://youtrack.jetbrains.com/issue/WEB-18768). I'm leaving the answer below because many people come here when searching for the solution provided.

编辑:如评论中所述,此问题中解决的实际问题实际上是 IntelliJ 15 / WebStorm 11 中的错误(请参阅https://youtrack.jetbrains.com/issue/WEB-18768)。我在下面留下答案是因为很多人在寻找提供的解决方案时来到这里。

The accepted answer is a sledgehammer approach to solving the problem. A better solution is to enable the mocha-DefinitelyTyped library for the test directory. Not only will this remove the erroneous inspection, it will provide better syntax highlighting.

公认的答案是解决问题的大锤方法。更好的解决方案是为测试目录启用 mocha-DefinitelyTyped 库。这不仅会消除错误检查,还会提供更好的语法突出显示。

From the description of the inspection:

从检查的描述来看:

When using libraries that define their own global symbols outside their visible JavaScript code (e.g. describe()in Mocha), it is recommended that you add the corresponding TypeScript type definition file as a JavaScript library in Preferences | Languages & Frameworks | JavaScript | Libraries.

当使用在可见 JavaScript 代码之外(例如describe()在 Mocha 中)定义自己的全局符号的库时,建议您在 Preferences | 添加相应的 TypeScript 类型定义文件作为 JavaScript 库。语言和框架 | JavaScript | 图书馆。

Navigate to the preference section referenced above, ‘Preferences | Languages & Frameworks | JavaScript | Libraries', and check if the mocha-DefinitelyTyped library is in the list.

导航到上面引用的首选项部分,“首选项 | 语言和框架 | JavaScript | 库”,并检查 mocha-DefinitelyTyped 库是否在列表中。

If @types/mocha(used to be named mocha-DefinitelyTyped) is not in the list, click the Download…button, find ‘mocha' in the ‘TypeScript community stubs' section, select it, and click Download and Install:

如果@types/mocha(曾经被命名mocha-DefinitelyTyped)不在列表中,点击Download…按钮,在'TypeScript community stubs'部分找到'mocha',选择它,然后点击下载并安装:

‘mocha' library to import, shown in the Download Library window

要导入的“mocha”库,显示在“下载库”窗口中

The library is named just mochain the list, but will convert into @types/mocha(prior to early 2019, it would convert to mocha-DefinitelyTyped) once imported.

该库仅mocha在列表中命名,但在导入后将转换为@types/mocha(在 2019 年初之前,它将转换为mocha-DefinitelyTyped)。

Once you have mocha-DefinitelyTyped, uncheck its Enabled checkbox. Leaving it fully checked would enable it globally, even outside of tests, which would be inaccurate. Instead, we will manage its scope to be enabled only in your test directory. Click the Manage Scopes…button, find your test directory, click on the Library column, and select the ‘mocha-DefinitelyTyped' entry. See the screenshot below.

一旦你有了 mocha-DefinitelyTyped,取消选中它的 Enabled 复选框。对其进行全面检查将使其在全局范围内启用,即使在测试之外也是如此,这将是不准确的。相反,我们将管理其范围,使其仅在您的测试目录中启用。单击Manage Scopes…按钮,找到您的测试目录,单击库列,然后选择“mocha-DefinitelyTyped”条目。请参阅下面的屏幕截图。

screenshot of the JavaScript Libraries Usage Scopes preferences window

JavaScript 库使用范围首选项窗口的屏幕截图

回答by lena

This is a new inspection introduced in v. 11 - it checks if the module used in code is referenced through require() statement. This inspection, indeed, is redundant for mocha tests ('describe' and other stuff don't need being required explicitly as they are added to global scope by Mocha). You can either disable this inspection or suppress it for 'describe', 'it', etc (see https://www.jetbrains.com/webstorm/help/suppressing-inspections.html). Another option: create a custom scope with your spec files excluded (Settings/Appearance & Behavior/Scopes), then choose this scope for 'Missing require() statement' inspection (Settings/Editor/Inspections/Javascript/Node.js).

这是 v. 11 中引入的新检查 - 它检查代码中使用的模块是否通过 require() 语句引用。实际上,这种检查对于 mocha 测试来说是多余的(“描述”和其他内容不需要明确要求,因为它们已被 Mocha 添加到全局范围内)。您可以禁用此检查或针对“描述”、“它”等禁止它(请参阅https://www.jetbrains.com/webstorm/help/suppressing-inspections.html)。另一种选择:创建一个排除规范文件的自定义范围(设置/外观和行为/范围),然后选择此范围进行“缺少 require() 语句”检查(设置/编辑器/检查/Javascript/Node.js)。

Related ticket: WEB-18768

相关票证:WEB-18768

回答by Dominic Cerquetti

There is currently a bug in Webstorm 2017.3 that prevents installation of Javascript Libraries via the ‘Preferences | Languages & Frameworks | JavaScript | Libraries | Download and Install' method.

当前 Webstorm 2017.3 中存在一个错误,该错误阻止通过“首选项 | 安装 Javascript 库”。语言和框架 | JavaScript | 图书馆 | 下载并安装'方法。

It's being tracked here, and the latest EAP (Early Access Preview), (Webstorm 2017.3.2 at the time of writing) fixes the issue. https://youtrack.jetbrains.com/oauth?state=%2Fissue%2FWEB-29725

正在此处对其进行跟踪,最新的 EAP(早期访问预览版)(撰写本文时为 Webstorm 2017.3.2)修复了该问题。 https://youtrack.jetbrains.com/oauth?state=%2Fissue%2FWEB-29725

I banged my head against the wall on this for hours, hopefully this helps someone else.

我把头撞在墙上几个小时,希望这能帮助别人。

回答by Oleg

If you already have installed mocha globally and if it's still not work then try to remove all Run/Debug Configurations.

如果您已经全局安装了 mocha 并且仍然无法正常工作,请尝试删除所有运行/调试配置。

It will update npm runner and I hope it solves the problem.

它将更新 npm runner,我希望它能解决问题。

enter image description here

enter image description here