117

I have a Mocha test file that looks like this:

var expect = require('chai').expect
var muting = require('../muting')

describe('muting', function () {
  describe('init()', function () {
    it('should inject an object into twitter', function () {
      var twitter = 'twitter'
      muting.init(twitter)
      expect(muting.twitter).to.equal(twitter)
    })
  })
})

When I run mocha from the CLI, it runs the test successfully.

When I run standard (the executable for JavaScript Standard Style) I get errors on Mocha's framework functions like so:

standard: Use JavaScript Standard Style (https://github.com/feross/standard)   
c:\..\test\index.js:5:0: 'describe' is not defined.  
c:\..\test\index.js:6:2: 'describe' is not defined.  
c:\..\test\index.js:7:4: 'it' is not defined.

What's the cleanest way to make Standard not complain about these functions?

urig
  • 16,016
  • 26
  • 115
  • 184

6 Answers6

213

I prefer to edit my .eslintrc and add mocha to env section:

...
"env": {
  "commonjs": true,
  "node": true,
  "mocha": true
},
...

this way my package.json file is kept clean, also vscode plugin for eslint understands it better

Developerium
  • 7,155
  • 5
  • 36
  • 56
167

Actually, you don't need to list every single global variable in your package.json

You can specify environments instead like this:

"standard": {
  "env": [ "mocha" ]
}

Source: Official ESLint configuration docs.

jbarros
  • 628
  • 10
  • 28
Krzysztof Kaczor
  • 5,408
  • 7
  • 39
  • 47
  • 2
    Good solution. Doesn't that mean I can call `it` in regular non-test code and it will pass linting? In other words. Can it be restricted only to test classes? – Ashley Dec 04 '16 at 17:44
  • 3
    Yeah, that's the problem... `package.json` settings are 'global' for linter. You can bypass it by providing different CLI arguments for different files: something like `standard --env mocha test/**/js` for lint-tests (not tested), but IRL i never had a need to tweak settings like this. – Krzysztof Kaczor Dec 04 '16 at 19:48
  • 4
    If using jest, you also can: "standard": { "env": [ "jest" ]} – palafox_e Jul 03 '18 at 22:30
  • To add to the comment by @palafox_e you can find out which values are available by navigating to: https://github.com/sindresorhus/globals/blob/master/globals.json – Wil Moore III Jul 09 '18 at 22:26
  • I'm using `jest` but I don't know why only works for mocha and not jest! – Developerium Jan 11 '20 at 06:30
66

while eslint's comment configuration works great for a single file, I prefer to use standard's package.json globals configuration to do this for my projects. E.g.

{
  "name": "my-package",
  "version": "1.0.0",
  "standard": {
    "globals": [
      "describe",
      "context",
      "before",
      "beforeEach",
      "after",
      "afterEach",
      "it",
      "expect"
    ]
  }
}
Nick Tomlin
  • 28,402
  • 11
  • 61
  • 90
59

for eslint use this line on the beginning of test_file.js

/* eslint-env mocha */
Ryu_hayabusa
  • 3,666
  • 2
  • 29
  • 32
41

You can use the same solution as for web workers

/* global describe it */
var expect = require('chai').expect
var muting = require('../muting')

describe('muting', function () {
  describe('init()', function () {
    it('should inject an object into twitter', function () {
     var twitter = 'twitter'
     muting.init(twitter)
     expect(muting.twitter).to.equal(twitter)
    })
  })
})
Niklas Ingholt
  • 477
  • 4
  • 5
2

As pointed out by Nick Tomlin you just need to declare globals.

I use to put it in the command line, since I have different globals for tests as for sources or different parts of the project.

For tests we should use

standard --global describe --global it test/

elsewhere in my project I want to lint code that uses jQuery so I use

standard --global $ src/client/

Bonus tip

If you are using vim with Syntastic you maybe want to add to your .vimrc

let b:syntastic_checkers = ['standard']
let g:syntastic_javascript_standard_args = "--global $ --global it --global describe"
Community
  • 1
  • 1
Gianluca Casati
  • 3,303
  • 1
  • 32
  • 20