Mocha’s describe “require() is missing” in WebStorm 11

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.

2023 edit: Looking back at this, I think that if you include the @types/mocha or @types/jest or @types/node entries in your package.json dev dependencies, then IntelliJ will pick them up automatically now.

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

Leave a Comment

tech