Skip to content

🚀 Feature: Allow hooks to easily set "allowUncaught" flag #1985

@indexzero

Description

@indexzero

Both suite.allowUncaught and test.allowUncaught are completely ignored by the mocha.Runner instance being used at any time. (See code: https://github.com/mochajs/mocha/blob/master/lib/runner.js#L413-L416).

  if (this.allowUncaught) {
    test.allowUncaught = true;
    return test.run(fn);
  }

Which really should be something like:

  if (this.allowUncaught || test.parent.allowUncaught || test.allowUncaught) {
    test.allowUncaught = true;
    return test.run(fn);
  }

Now I'm not really sure the best way to traverse up the tree of suites to find all of the parents so the test.parent.allowUncaught is pretty naive, but the second test.allowUncaught check allows for a (somewhat) elegant work-around:

describe('Anything', function () {
  this.on('test', function (test) {
    test.allowUncaught = true;
  });
});

The super-hack work-around until I have the time to make a pull-request for this is:

//
// This is an awful and fragile hack that
// needs to be changed ASAP.
//
var _runTest = mocha.Runner.prototype.runTest;
mocha.Runner.prototype.runTest = function () {
  this.allowUncaught = true;
  _runTest.apply(this, arguments);
};

describe('Anything', function () {

  after(function () {
    mocha.Runner.prototype.runTest = _runTest;
  });
});

Worth noting that the --allowUncaught CLI option also appears to be completely broken right now. My use-case is slightly different, however, since I only want to set allowUncaught to true for some test suites (not all).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions