Skip to content

Commit 0208341

Browse files
kofry
authored andcommitted
assert.throws can now accept as RegExp
makes validation of errors more flexible
1 parent 86727b1 commit 0208341

2 files changed

Lines changed: 43 additions & 30 deletions

File tree

lib/assert.js

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -239,47 +239,50 @@ assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
239239
}
240240
};
241241

242-
function _throws (shouldThrow, block, err, message) {
243-
var exception = null,
244-
threw = false,
245-
typematters = true;
246-
247-
message = message || "";
248-
249-
//handle optional arguments
250-
if (arguments.length == 3) {
251-
if (typeof(err) == "string") {
252-
message = err;
253-
typematters = false;
242+
function expectedException(actual, expected) {
243+
if (!actual || !expected) {
244+
return false;
245+
}
246+
247+
if (expected instanceof RegExp) {
248+
if (expected.test(actual)) {
249+
return true;
254250
}
255-
} else if (arguments.length == 2) {
256-
typematters = false;
251+
} else if (actual instanceof expected || expected.call({}, actual) !== false) {
252+
return true;
253+
}
254+
}
255+
256+
function _throws (shouldThrow, block, expected, message) {
257+
var actual;
258+
259+
if (typeof expected === "string") {
260+
message = expected;
261+
expected = null;
257262
}
258263

259264
try {
260265
block();
261266
} catch (e) {
262-
threw = true;
263-
exception = e;
267+
actual = e;
264268
}
265269

266-
if (shouldThrow && !threw) {
267-
fail( "Missing expected exception"
268-
+ (err && err.name ? " ("+err.name+")." : '.')
269-
+ (message ? " " + message : "")
270-
);
270+
message = (expected && expected.name ? " (" + expected.name + ")." : ".")
271+
+ (message ? " " + message : ".");
272+
273+
if (shouldThrow && !actual) {
274+
fail("Missing expected exception" + message);
271275
}
272-
if (!shouldThrow && threw && typematters && exception instanceof err) {
273-
fail( "Got unwanted exception"
274-
+ (err && err.name ? " ("+err.name+")." : '.')
275-
+ (message ? " " + message : "")
276-
);
276+
277+
if (!shouldThrow && expectedException(actual, expected)) {
278+
fail("Got unwanted exception" + message);
277279
}
278-
if ((shouldThrow && threw && typematters && !(exception instanceof err)) ||
279-
(!shouldThrow && threw)) {
280-
throw exception;
280+
281+
if ((shouldThrow && actual && expected && !expectedException(actual, expected)) ||
282+
(!shouldThrow && actual)) {
283+
throw actual;
281284
}
282-
};
285+
}
283286

284287
// 11. Expected to throw an error:
285288
// assert.throws(block, Error_opt, message_opt);

test/simple/test-assert.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,13 @@ assert.equal(true,threw,'a.doesNotThrow is not catching type matching errors');
158158
assert.throws(function () {assert.ifError(new Error('test error'))});
159159
assert.doesNotThrow(function(){assert.ifError(null)});
160160
assert.doesNotThrow(function(){assert.ifError()});
161+
162+
// use a RegExp to validate error message
163+
a.throws(makeBlock(thrower, TypeError), /test/ );
164+
165+
// use a fn to validate error object
166+
a.throws(makeBlock(thrower, TypeError), function(err) {
167+
if (!(err instanceof TypeError) || !/test/.test(err)) {
168+
return false;
169+
}
170+
});

0 commit comments

Comments
 (0)