简体   繁体   English

在Mocha测试中绕过ESLint的`no-unused-var` for Should

[英]Bypass ESLint's `no-unused-var` for Should in a Mocha test

I'm using ESLint on all my files, including the test files, with a no-unused-var: true rule. 我在我的所有文件上使用ESLint,包括测试文件,使用no-unused-var: true规则。 I'm using Should.js in my Mocha tests, and in one of the files, I'm getting an error on the should variable. 我在我的Mocha测试中使用了Should.js,在其中一个文件中,我在should变量上遇到错误。 A quick comparison to the other tests shows, that in other files I have at least one line that starts with should (ie should.not.exist(err); ), whereas in this particular file, I only use it in property form (ie a.should.equal(b) ). 与其他测试的快速比较表明,在其他文件中我至少有一行以should开头(即should.not.exist(err); ),而在这个特定的文件中,我只在属性形式中使用它(即a.should.equal(b) )。

Short of turning the rule off for the entire file, or coercing perfectly readable tests into the variable use of should , is there any way around this? 较短的翻转规则关闭整个文件,或胁迫完全可读的测试到变量使用的should是有解决什么办法? Can I turn off the rule just for the should variable? 我可以为should变量关闭规则吗? Perhaps add an exception for it? 也许为它添加一个例外? Or (hopefully) a more elegant solution? 或者(希望)更优雅的解决方案?

In this particular file you can just require instead of declaring. 在这个特定的文件中,您可以只需要而不是声明。

var should = require('should');

instead do 相反

require('should');

Well, doesn't seem like there's an elegant solution to this, but I think I came up with a close approximation: I just added a single line to my before() hook, stating simply should; 好吧,似乎没有一个优雅的解决方案,但我认为我得出了一个近似的近似:我只是添加了一行到我的before()钩子,说明should; . Since this evaluates to the should object, or in other words a "truthy" value, it can be treated as a no-op. 由于这会评估为should对象,或者换句话说是“truthy”值,因此可以将其视为no-op。 The side effect is that now the should variable is "used", and ESLint does not throw a warning. 副作用是现在should变量被“使用”,并且ESLint不会发出警告。

I'm going to let this answer hang for a while, hoping one of you has a better solution. 我会让这个答案挂起一段时间,希望你们中的一个有更好的解决方案。

Per request, here's a gist showing the hack in action . 根据请求,这是一个显示黑客行动的要点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM