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

1.8k views Asked by At

I'm using ESLint on all my files, including the test files, with a no-unused-var: true rule. I'm using Should.js in my Mocha tests, and in one of the files, I'm getting an error on the should variable. A quick comparison to the other tests shows, that in other files I have at least one line that starts with should (i.e. should.not.exist(err);), whereas in this particular file, I only use it in property form (i.e. 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? Can I turn off the rule just for the should variable? Perhaps add an exception for it? Or (hopefully) a more elegant solution?

2

There are 2 answers

5
Traveling Tech Guy On BEST ANSWER

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;. Since this evaluates to the should object, or in other words a "truthy" value, it can be treated as a no-op. The side effect is that now the should variable is "used", and ESLint does not throw a warning.

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.

0
Yan On

In this particular file you can just require instead of declaring.

var should = require('should');

instead do

require('should');