From fa4c3375705d6bf0bd4e72e0f88d3454e95cc664 Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Wed, 3 Dec 2014 18:29:52 -0500 Subject: [PATCH] Add assert.throws Closes #57. --- test/harness/assert.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/harness/assert.js b/test/harness/assert.js index 1d9d4a48f2..2c1c884a38 100644 --- a/test/harness/assert.js +++ b/test/harness/assert.js @@ -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'); +};