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

前端 未结 5 1318
故里飘歌
故里飘歌 2020-12-23 20:27

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

相关标签:
5条回答
  • 2020-12-23 21:09

    There is very simple workaround that will solve the issue for you and for all other developers in the project:

    Add the following line at the top of the file:

    const { describe, it, before, beforeEach, afterEach, after } = require('mocha');
    
    0 讨论(0)
  • 2020-12-23 21:16

    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.

    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

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

    0 讨论(0)
  • 2020-12-23 21:20

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

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

    0 讨论(0)
  • 2020-12-23 21:21

    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:

    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.

    0 讨论(0)
  • 2020-12-23 21:23

    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

    0 讨论(0)
提交回复
热议问题