简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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