common mistake labelnode node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend avoiding the use of globally installed Mocha.
Cannot run ui: 'exports' tests using API. The call to mocha.run() throws an exception "TypeError: Cannot read property 'skip' of undefined".
Create a file MyTestFile.js with contents:
exports.MyTest = {
myTestFunction() {
}
};
Then run node -e "const Mocha = require('mocha'); const mocha = new Mocha({ ui: 'exports' }); mocha.addFile('./MyTestFile.js'); mocha.run();"
Expected behavior:
Test should pass.
Actual behavior:
Throws exception:
/home/rpaterson/programming/signpost/toshokan/node_modules/mocha/lib/mocha.js:207
exports.xit = context.xit || context.test.skip;
^
TypeError: Cannot read property 'skip' of undefined
at Suite.<anonymous> (/home/rpaterson/programming/signpost/toshokan/node_modules/mocha/lib/mocha.js:207:46)
at emitThree (events.js:116:13)
at Suite.emit (events.js:194:7)
at /home/rpaterson/programming/signpost/toshokan/node_modules/mocha/lib/mocha.js:230:11
at Array.forEach (native)
at Mocha.loadFiles (/home/rpaterson/programming/signpost/toshokan/node_modules/mocha/lib/mocha.js:228:14)
at Mocha.run (/home/rpaterson/programming/signpost/toshokan/node_modules/mocha/lib/mocha.js:514:10)
at [eval]:1:117
at ContextifyScript.Script.runInThisContext (vm.js:25:33)
at Object.exports.runInThisContext (vm.js:77:17)
Reproduces how often:
100% of the time.
Problem does not happen in v3.5.0, starts happening in v3.5.1. The line that is throwing the exception was added in that release by 1cc0fc0e6153bbd746b0c2da565363570432cdf7.
Hmm. Given that those assignments seem to depend on the BDD or TDD interface being chosen, I think we could put that whole callback in an if to filter out the Exports interface. ...And the QUnit interface if it isn't close enough to the TDD interface to make these assignments work, which I'd have to do a little more digging to confirm or deny. [EDITTED TO ADD:] The Qunit interface at least doesn't throw if I try the same experiment using it instead of the exports interface, so yeah, for now we just need to skip that callback if the chosen interface is Exports.
@ScottFreeCode: I see you've self-assigned this issue, and I was wondering if you had an update? We're experiencing this issue as well (vega/vega-lite#4166) when using ui: "assert" for what I imagine is the same underlying issue (the assignments depend on BDD/TDD interfaces). Thanks!
@arvind, To my knowledge, _no one is working on this issue_!
Provide an MCVE and I'll take a look when I get a chance.
BTW, since I have never used either of these UIs, what is their equivalent of a skipped test?
Unverified but...
exports ui:
it.skipit.onlyThe ui: 'exports' interface is implemented in a basic manner. Only suites, tests and before-/beforeAll- and after-/afterAll-hooks are supported. Neither it.skip nor it.only are accepted.
To avoid TypeError: Cannot read property 'skip' of undefined one line of code has to be changed:
this.suite.on('pre-require', function(context) {
...
exports.it = context.it || context.test;
exports.xit = context.xit || context.test.skip; //old
exports.xit = context.xit || context.test && context.test.skip; //new
exports.setup = context.setup || context.beforeEach;
exports.MyTest = {
before() { console.log("before hook");},
after() { console.log("after hook");},
myTestFunction() { console.log("test function");
}
};
md5-09b835bb1186281b1f57d5d946ce47e1
MyTest
before hook
test function
√ myTestFunction
after hook
1 passing (13ms)
@boneskull please close, since solved in b5c0fb0
@outsideris could you please close this issue? Thanks.
I confirmed it fixed as @juergba said.