Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3320 Native types exceptions can crash Mocha runner #3321

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 23 additions & 9 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,7 @@ Runner.prototype.fail = function(test, err) {
++this.failures;
test.state = 'failed';

if (!(err instanceof Error || (err && typeof err.message === 'string'))) {
err = new Error(
'the ' +
type(err) +
' ' +
stringify(err) +
' was thrown, throw an Error :)'
);
}
err = string2Error(err);
Copy link
Contributor

@plroebuck plroebuck Aug 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somewhere else in the code, let's have:

/**
 * Check if argument is an instance of Error object or a duck-typed equivalent.
 *
 * @private
 * @param {Object} err - object to check
 * @param {string} err.message - error message
 * @returns {boolean}
 */
function isError(err) {
  return err instanceof Error || (err && typeof err.message === 'string');
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And replace the check so it's clear why the other routine would be called.

  if (!isError(err)) {
    err = thrown2Error(err);
  }


try {
err.stack =
Expand Down Expand Up @@ -731,6 +723,7 @@ Runner.prototype.uncaught = function(err) {
debug('uncaught undefined exception');
err = undefinedError();
}
err = string2Error(err);
Copy link
Contributor

@plroebuck plroebuck Aug 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And again...

  if (!isError(err)) {
    err = thrown2Error(err);
  }

err.uncaught = true;

var runnable = this.currentRunnable;
Expand Down Expand Up @@ -993,6 +986,27 @@ function filterLeaks(ok, globals) {
});
}

/**
*
* Check that the given object is an Error, convert it at such on the contrary
*
* @api private
* @param {Object} err
* @return {Error}
*/
function string2Error(err) {
Copy link
Contributor

@plroebuck plroebuck Aug 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed for added clarity.

/**
 * Converts thrown non-extensible type into proper Error.
 *
 * @private
 * @param {*} thrown - Non-extensible type thrown by code
 * @return {Error}
 */
function thrown2Error(thrown) {
  return new Error(
    'the ' +
    type(thrown) +
    ' ' +
    stringify(thrown) +
    ' was thrown, throw an Error :)'
  );
}

if (!(err instanceof Error || (err && typeof err.message === 'string'))) {
err = new Error(
'the ' +
type(err) +
' ' +
stringify(err) +
' was thrown, throw an Error :)'
);
}
return err;
}

/**
* Array of globals dependent on the environment.
*
Expand Down
18 changes: 18 additions & 0 deletions test/unit/throw.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ describe('a test that throws', function() {
});
});

describe('non-extensible', function() {
it('should not crash if throwing non-extensible type', function(done) {
var test = new Test('im async and throw string async', function(done2) {
process.nextTick(function() {
throw 'error';
});
});
suite.addTest(test);
runner = new Runner(suite);
runner.on('end', function() {
expect(runner.failures, 'to be', 1);
expect(test.state, 'to be', 'failed');
done();
});
runner.run();
});
});

describe('undefined', function() {
it('should not pass if throwing sync and test is sync', function(done) {
var test = new Test('im sync and throw undefined sync', function() {
Expand Down