JavaScript 标准样式无法识别 Mocha

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30018271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:30:20  来源:igfitidea点击:

JavaScript Standard Style does not recognize Mocha

javascriptmocha

提问by urig

I have a Mochatest file that looks like this:

我有一个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)
    })
  })
})

When I run mochafrom the CLI, it runs the test successfully.

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

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

当我运行standardJavaScript Standard Style的可执行文件)时,我在 Mocha 的框架函数上遇到错误,如下所示:

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?

让 Standard 不抱怨这些功能的最简洁方法是什么?

回答by Krzysztof Kaczor

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

实际上,您不需要在 package.json 中列出每个全局变量

You can specify environments instead like this:

您可以像这样指定环境:

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

Source: Official ESLint configuration docs.

来源:官方 ESLint 配置文档

回答by Developerium

I prefer to edit my .eslintrcand add mocha to env section:

我更喜欢编辑我的.eslintrc并将 mocha 添加到 env 部分:

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

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

这样我的package.json文件就保持干净了,eslint 的 vscode 插件也能更好地理解它

回答by Nick Tomlin

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

虽然 eslint 的注释配置适用于单个文件,但我更喜欢使用标准的配置来为我的项目执行此操作。例如package.jsonglobals

{
  "name": "my-package",
  "version": "1.0.0",
  "standard": {
    "globals": [
      "describe",
      "context",
      "before",
      "beforeEach",
      "after",
      "afterEach",
      "it",
      "expect"
    ]
  }
}

回答by Ryu_hayabusa

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

对于 eslint 在test_file.js的开头使用这一行

/* eslint-env mocha */

回答by Niklas Ingholt

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

回答by Gianluca Casati

As pointed out by Nick Tomlinyou just need to declare globals.

正如Nick Tomlin所指出的,你只需要声明全局变量。

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

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

standard --global $ src/client/

Bonus tip

小费

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

如果你在 Syntastic 中使用 vim,你可能想添加到你的.vimrc

let b:syntastic_checkers = ['standard']
let g:syntastic_javascript_standard_args = "--global $ --global it --global describe"