Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Even though my mocha test cases are using should , eslint still warns about it?

I'm using mocha and chai.should assertion library to unit test my OSS npm module. But eslint [v3.18.0^] warns at the should declaration in the code about it not being used

const should = chai.should();

Lint Warning 'should' is assigned a value but never used.

We have mocha as an environment in the .eslintrc. This warning appears despite there being atleast seven assertion statements in my test file like below that have "should" in them.

return sampleModule.getSomeFunction('dummy_accesss_token', 'dummy_event_id').should.eventually.be.eql([]);

I'm aware of the following SO issue which talks about other assertion statments which are never declared while a reply[which was not even declared as the answer] to the query had someone created a plugin to overcome this.

But my basic premise , isnt this a bug in eslint? Atleast "should" should :-) not be flagged since its being used in code [multiple times in my case].

like image 942
indcoder Avatar asked Dec 19 '22 07:12

indcoder


1 Answers

isnt this a bug in eslint?

No, because you are not using the variable called should.

You're using a getter called should that chai.should() adds to Object.prototype, but that's not the same as using the variable.

You can still use that variable, for instance like this:

should.exist(...);
should.not.exist(...);

But you're probably not doing that.

So to get rid of the warning, don't do this:

const should = chai.should();

But do this:

chai.should();
like image 189
robertklep Avatar answered May 04 '23 10:05

robertklep