"commander.opts" function gets overwritten with string "test/mocha.opts"
Closed this issue ยท 5 comments
In our unit test, when we use the commander
module with grunt-mocha-istanbul
, it looks like the commander.opts
is being overwritten with the test/mocha.opts
string:
Running "mocha_istanbul:coverage" (mocha_istanbul) task
commander
1) exposes extension functions
0 passing (9ms)
1 failing
1) commander exposes extension functions:
AssertionError: expected 'test/mocha.opts' to be a function
expected 'test/mocha.opts' to have type function
expected 'string' to be 'function'
at Assertion.fail (node_modules/should/cjs/should.js:258:17)
at Assertion.Object.defineProperty.value [as Function] (node_modules/should/cjs/should.js:335:19)
at Context.<anonymous> (commander_test.js:9:463)
However, if we run our unit test with mocha
by itself, then our assertion passes.
In the below steps, I've attached a zip file that contains the minimum amount of files to reproduce this issue.
Steps to reproduce:
- Download and unzip sandbox3.zip
- Run
npm install && npm test
it's because commander is looking for the command line options, and since this library spawn new processes, and mocha also with additional arguments, I don't see how this could be circumvented
Yeah, it looks like before this library loads the mocha
module, commander.opts
is still preserved. Once this library loads the mocha
module, the mocha
module overwrites the commander.opts
with its commander
default value, which is test/mocha.opts
. Interestingly, I don't see this issue if I use mocha@2.5.3
.
All right, I found a way to workaround this:
var program = null,
should = require('should');
describe('commander', function () {
before(function () {
delete require.cache[require.resolve('commander')];
program = require('commander');
});
it('exposes extension functions', function () {
program.parse.should.be.a.Function();
program.opts.should.be.a.Function();
});
});
I'll file a another issue with the mocha project to see if they have any insight as well.
Mocha maintainer here; as far as I can tell this is caused entirely by Mocha/Commander and isn't really affected by grunt-mocha-istanbul one way or the other.
I'll close this issue as per ScottFreeCode's comment.