JavaScript Standard Style does not recognize Mocha

后端 未结 6 563
囚心锁ツ
囚心锁ツ 2020-12-23 15:35

I have a Mocha test file that looks like this:

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

describe(\'muting\', function () {
         


        
相关标签:
6条回答
  • 2020-12-23 16:08

    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

    0 讨论(0)
  • 2020-12-23 16:08

    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)
        })
      })
    })
    
    0 讨论(0)
  • 2020-12-23 16:17

    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"
    
    0 讨论(0)
  • 2020-12-23 16:20

    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"
        ]
      }
    }
    
    0 讨论(0)
  • 2020-12-23 16:21

    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.

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

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

    /* eslint-env mocha */
    
    0 讨论(0)
提交回复
热议问题