Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

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

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

require() call is missing

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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.

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

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:

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

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

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.

screenshot of the JavaScript Libraries Usage Scopes preferences window


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...