tester123
tester123

Reputation: 1113

Sinon and eslint

I am writing unit tests for my angular app with Karma, Jasmine, and Sinon and I run eslint over my code base.

I define global variables that will be used in the beforeEach inject to create a sinon.stub. ESLint keeps complaining that my global variables are defined but never used. For example:

'getListStub' is defined but never used no-unused-vars

but in my code it looks like this

var listService, getListStub;

beforeEach(inject(function(_listService_) {
  listService = _listService_;
  getListStub = sinon.stub(listService, 'getList').returns(q.when(listResponse));
}

What is the best way to stop ESLint from producing errors?

Is setting /*eslint no-unused-vars: 0*/ at the top of those testing files best?

Upvotes: 0

Views: 1047

Answers (1)

ssube
ssube

Reputation: 48317

If you aren't using getListStub anywhere, why are you assigning it to a variable?

The properties of JS closure and memory management (specifically, holding referenced objects) will allow you to use _listService_ directly and you shouldn't need to cache getListStub.

If that does work correctly with sinon, you should be able to rewrite your function as:

beforeEach(inject(function(_listService_) {
  sinon.stub(_listService_, 'getList').returns(q.when(listResponse));
}

Upvotes: 2

Related Questions