Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bypass ESLint no-unused-vars error for Should in a Mocha test
// Without line 9 hack, linting the file throws: 5:4 error should is defined but never used no-unused-vars
'use strict';
var debug = require('debug')('test:myModel');
var should = require('should');
var security = require('../models/myModel');
before((done) => {
should; //bypass ESLint no-unused-var error
done();
});
describe('Security tests', () => {
let baz = 'blahblah'
it('should do foo', () => {
let result = model.foo(baz, 64);
debug(result);
result.should.not.be.empty; //not considered use of `should`
result.length.should.equal(64); //not considered use of `should`
});
it('should do bar', () => {
result = model.bar(baz);
debug(result);
result.should.not.be.empty; //not considered use of `should`
result.should.have.property('error'); //not considered use of `should`
result.error.should.be.false; //not considered use of `should`
result.should.have.property('data'); //not considered use of `should`
result.data.should.equal(baz); //not considered use of `should
});
...//more tests
});
@mrchief

This comment has been minimized.

Copy link

@mrchief mrchief commented Sep 13, 2016

You don't need this hack. Simply say require('should'); instead of var should = require('should');

@tamj0rd2

This comment has been minimized.

Copy link

@tamj0rd2 tamj0rd2 commented May 22, 2017

@mrchief you hero!

@fluxsauce

This comment has been minimized.

Copy link

@fluxsauce fluxsauce commented Jul 8, 2017

I don't fully agree with @mrchief as a test like:

should.not.exist(something());

won't work.

Instead, just configure ESLint correctly.

  "rules": {
    "no-unused-vars": [
      "error",
      { "varsIgnorePattern": "should\|expect" }
    ]
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.