Skip to content
Snippets Groups Projects
Commit bf9830ee authored by Brian Terlson's avatar Brian Terlson
Browse files

Merge pull request #131 from domenic/assert-throws

Add assert.throws
parents 3883a2e9 66a39c04
No related branches found
No related tags found
No related merge requests found
......@@ -40,3 +40,25 @@ assert.notSameValue = function (actual, unexpected, message) {
}
$ERROR(message);
};
assert.throws = function (expectedErrorConstructor, func) {
if (func === undefined) {
$ERROR('assert.throws requires two arguments: the error constructor and a function to run');
return;
}
try {
func();
} catch (thrown) {
if (typeof thrown !== 'object' || thrown === null) {
$ERROR('Thrown value was not an object!');
return;
}
if (thrown.constructor !== expectedErrorConstructor) {
$ERROR('Expected a ' + expectedErrorConstructor.name + ' but got a ' + thrown.constructor.name);
}
return;
}
$ERROR('Expected a ' + expectedErrorConstructor.name + ' to be thrown but no exception was thrown at all');
};
......@@ -6,9 +6,10 @@ info: >
Promise throws TypeError when 'this' is constructed but unsettled promise
author: Sam Mikes
description: Promise.call(new Promise()) throws TypeError
negative: TypeError
---*/
var p = new Promise(function() {});
Promise.call(p, function () {});
assert.throws(TypeError, function () {
Promise.call(p, function () {});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment