-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, thanks for the PR.
Mocha does not (and will not) support throwing of anything except an Error
or Error
-like object.
If you wish to modify this PR, you could perform a check as seen here which re-throws the value as an Error
(and sets the uncaught
property).
You're right, with the modification Patch modified accordingly. |
test/unit/throw.spec.js
Outdated
@@ -26,6 +26,24 @@ describe('a test that throws', function () { | |||
}); | |||
}); | |||
|
|||
describe('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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done2
is unused.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's normal to have done2
argument there to make the test an async test.
I added eslint-ignore comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooops! I got it.
It needed to make the test as async as you said.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment makes it clearer, so it's better anyway :)
unused arguments are not checked by the linter, I'd recommend you to add the following lines at the end of your
I have the following result:
Not sure that those aren't false-positive, and definitely not related to this modification, so won't touch this here. |
Would prefer |
4547268
to
7613521
Compare
lib/runner.js
Outdated
@@ -704,6 +704,9 @@ Runner.prototype.uncaught = function (err) { | |||
debug('uncaught undefined exception'); | |||
err = undefinedError(); | |||
} | |||
if (!(err instanceof Error || (err && typeof err.message === 'string'))) { | |||
err = new Error('the ' + type(err) + ' ' + stringify(err) + ' was thrown, throw an Error :)'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test/unit/throw.spec.js
Outdated
@@ -26,6 +26,25 @@ describe('a test that throws', function () { | |||
}); | |||
}); | |||
|
|||
describe('extensible', function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ticky-tack, but aren't you rather testing "non-extensible"?
test/unit/throw.spec.js
Outdated
throw 'error'; | ||
}); | ||
}); | ||
suite.addTest(test); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possibly outside the scope, but rather than repeating these lines over and over, why don't we just use a function runTest(test, done)
instead?
@fargies, I like this PR (making async work like sync). But it may be problematic to integrate. Typical workflow would be to create a local issue-related branch (e.g., "issue3320"), make your changes there, then push that branch to GitHub for the PR. Then the GitHub (remote) branch is eventually merged into a release. But not directly to master. |
- Any non-extensible type thrown in an async callback raises an exception in Mocha runner, stopping tests.
Modification updated. I can rename my remote branch, but where should I make a merge request ? |
bf19d09
to
b7acffa
Compare
' was thrown, throw an Error :)' | ||
); | ||
} | ||
err = string2Error(err); |
There was a problem hiding this comment.
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');
}
There was a problem hiding this comment.
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);
}
' was thrown, throw an Error :)' | ||
); | ||
} | ||
err = string2Error(err); |
There was a problem hiding this comment.
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);
}
* @param {Object} err | ||
* @return {Error} | ||
*/ | ||
function string2Error(err) { |
There was a problem hiding this comment.
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 :)'
);
}
Rename your remote branch "issue3320" after updates. |
New pull request from issue3320 branch -> #3471 |
Any non-extensible type thrown in an async callback raises an exception in Mocha runner, stopping tests.
See issue #3320
Requirements
Description of the Change
Alternate Designs
Why should this be in core?
Benefits
Tests continue and reports errors properly
Possible Drawbacks
Applicable issues