Mocha: Cannot run `ui: 'exports'` tests using API.

Created on 12 Sep 2017  Â·  8Comments  Â·  Source: mochajs/mocha

Prerequisites

  • [x] Checked that your issue isn't already filed by cross referencing issues with the common mistake label
  • [x] Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • [x] 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • [x] Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with:
    node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend avoiding the use of globally installed Mocha.

Description


Cannot run ui: 'exports' tests using API. The call to mocha.run() throws an exception "TypeError: Cannot read property 'skip' of undefined".

Steps to Reproduce

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.

Versions


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.

Additional Information

confirmed-bug help wanted

All 8 comments

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:

  • minus sign prepended to title will skip test, same as calling it.skip
  • exclamation point prepended to title will make test exclusive, same as calling it.only

The 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:

mocha.js line 256:

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.

Was this page helpful?
0 / 5 - 0 ratings