Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 534
BanksySan Avatar asked Nov 08 '15 22:11

BanksySan


2 Answers

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

like image 90
BamaPookie Avatar answered Sep 29 '22 16:09

BamaPookie


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

Related ticket: WEB-18768

like image 23
lena Avatar answered Sep 29 '22 17:09

lena