109

I uses RequireJS AMD in my project. When i run jshint on my project, it throws error like

In AMD Scripts

 'define' is not defined.

In Mocha test cases

 'describe' is not defined.
 'it' is not defined.

How to remove this warning in jshint?

Bas Slagter
  • 9,831
  • 7
  • 47
  • 78
Fizer Khan
  • 88,237
  • 28
  • 143
  • 153

8 Answers8

187

Just to expand a bit, here's a .jshintrc setup for Mocha:

{
  ....
  "globals"   : {
    /* MOCHA */
    "describe"   : false,
    "it"         : false,
    "before"     : false,
    "beforeEach" : false,
    "after"      : false,
    "afterEach"  : false
  }
}

From the JSHint Docs - the false (the default) means the variable is read-only.

If you are defining globals only for a specific file, you can do this:

/*global describe, it, before, beforeEach, after, afterEach */
bendytree
  • 13,095
  • 11
  • 75
  • 91
  • 42
    we can use `{"mocha": true}` 2.5.1+ https://github.com/jshint/jshint/issues/1330 – gwokae Nov 26 '14 at 09:22
  • @bendytree do you know if i can add these rules for some files / folders not global? – Gobliins Sep 26 '17 at 09:14
  • @Gobliins Bohdan's comment above. Add that line as the first line of your mocha test file and it will enforce the rule for that file only – Isaac Apr 18 '19 at 00:46
18
jshint: {
  options: {
    mocha: true,
  }
}

is what you want

Grant Fong
  • 311
  • 3
  • 5
12

To avoid the not defined warning in jshint for the javascript add comments like:

/*global describe:true*/

Options

Roland Puntaier
  • 3,250
  • 30
  • 35
8

Add this in your .jshintrc

"predef" : ["define"]   // Custom globals for requirejs
Shital Shah
  • 63,284
  • 17
  • 238
  • 185
  • 19
    predef is depreceted, use globals – Jonathan Apr 03 '14 at 20:16
  • @JonathanAzulay I didn't find any deprecation note at http://jshint.com/docs/options/#predef . I think Global can be used for custom Global JavaScript e.g angular, $, jQuery etc and predef can be used for implicit global objects like window, document, console etc – Amitesh Aug 10 '17 at 05:22
6

late to the party, but use this option in your jshintrc:

"dojo": true

and thou shall rest peacefully without red warnings...

Gilad Peleg
  • 2,010
  • 16
  • 29
6

If you are working on node js. Add these two lines in the beginning of your file

/*jslint node: true */
"use strict";
salihcenap
  • 1,927
  • 22
  • 25
3

Read the docs and search for /*global

Paul Grime
  • 14,970
  • 4
  • 36
  • 58
1

If you're trying to run JSHint in WebStorm with Mocha, as I am, go into:

WebStorm > Preferences > Languages & Frameworks > JavaScript > Code Quality Tools > JSHint

Scroll down to "Environments" and make sure you have selected the checkbox to enable "Mocha" which will set up the definitions for JSHint for Mocha for you.

nyarasha
  • 1,119
  • 1
  • 14
  • 24