JavaScript的标准样式不承认摩卡

问题描述 投票:53回答:6

我有一个Mocha测试文件看起来像这样:

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)
    })
  })
})

当我从CLI运行mocha,它成功地运行测试。

当我运行standard(可执行的JavaScript Standard Style)我得到像这样的摩卡的框架功能的错误:

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.

什么是做标准不抱怨这些功能的干净的方式?

javascript mocha
6个回答
111
投票

其实,你并不需要列出每一个全局变量在你的package.json

您可以指定的环境,而不是像这样:

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

来源:Official ESLint configuration docs


80
投票

我宁愿编辑我.eslintrc并添加到摩卡ENV部分:

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

这样一来我的package.json文件保持清洁,对eslint也vscode插件明白它更好


58
投票

而eslint的评论配置单个文件的伟大工程,我更喜欢使用标准qazxsw POI qazxsw POI配置为我的项目做到这一点。例如。

package.json

42
投票

对于eslint使用此行test_file.js的开始

globals

38
投票

您可以使用相同的解决方案为{ "name": "my-package", "version": "1.0.0", "standard": { "globals": [ "describe", "context", "before", "beforeEach", "after", "afterEach", "it", "expect" ] } }

/* eslint-env mocha */

3
投票

正如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)
    })
  })
})

在我的项目在其他地方我想使用jQuery的,所以我使用lint代码

Nick Tomlin

分红型

如果您使用的是带有Syntastic VIM,你也许要添加到您的.vimrc

standard --global describe --global it test/
© www.soinside.com 2019 - 2024. All rights reserved.