Skip to content

Commit

Permalink
behave more like the underlying assert.throws; 1.6.7
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasq committed Nov 17, 2020
1 parent 509fe87 commit bc98cb9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
22 changes: 11 additions & 11 deletions qassert.js
Expand Up @@ -106,17 +106,17 @@ function _notStrictEqual(a,b,m) {
function _throws(a,e,m) {
if (this && this.assertionCount !== undefined) this.assertionCount += 1;
try {
if (e instanceof Error || e instanceof RegExp || typeof e === 'function') {
// the expected e can be an Error, a RegExp, or a validator function (or, in newer node, an object)
assert.throws(a, e);
} else {
if (m === undefined) m = e; // two-argument form
assert.throws(a);
}
} catch (err) {
// NOTE: nodejs rethrows the user error if it does not match the expected, even when a non-object
// NOTE: It might be better to always fail from here with an AssertionError... TBD.
// NOTE: without an AssertionError cannot distinguish test failure from code crash
// recognize the two-argument form of the call
if (typeof e === 'string' && m === undefined) { m = e; e = undefined }

// Nodejs assert.throws() prior to node-v8 did not consider a falsy error as having been thrown.
// We pass through the native nodejs behavior.
assert.throws(a, e, m);
}
catch (err) {
// Nodejs rethrows the user error if it does not match the expected, even when a non-object
// It might be better to always fail from here with an AssertionError... TBD
// since without an AssertionError cannot distinguish test failure from code crash.

// node-v0.6 assigns the 'Missing expected exception' message to err.actual, not err.message
var altMessage = findFirst([false, err.message, err.actual]); // fix node-v0.6 err.message, `false` added for code cov
Expand Down
11 changes: 8 additions & 3 deletions test-qassert.js
Expand Up @@ -192,13 +192,18 @@ assert.throws(function(){ assert(qassert.notStrictContains(["1",2], "1")) });
// test does throw the expected error
try { qassert.throws(function(){ /* no throw */ }, 'appended diagnostic') }
catch (e) { assert.ok(e.message.indexOf('appended diagnostic') > 0) }
try { qassert.throws(function(){ /* no throw */ }, 'invalid test-to', 'appended diagnostic') }
try { qassert.throws(function(){ /* no throw */ }, /invalid test-to/, 'appended diagnostic') }
catch (e) { assert.ok(e.message.indexOf('appended diagnostic') > 0) }
// Newer node throw a TypeError on an invalid (string) expected exception in the three-argument form
// try { qassert.throws(function(){ /* no throw */ }, 'invalid test-to', 'appended diagnostic') }
// catch (e) { assert.ok(e.message.indexOf('appended diagnostic') > 0) }

qassert.throws(function() { throw new Error() });
qassert.throws(function() { throw 'string' });
qassert.throws(function() { throw false });
qassert.throws(function() { throw 0 });
// NOTE: in older node assert.throws() did not treat a falsy error as a throw having occurred.
// qassert.throws(function() { throw false });
// qassert.throws(function() { throw 0 });
// qassert.throws(function() { throw null });
qassert.throws(function() { throw 'non-object' });

try { qassert.throws(function(){ throw 'string A' }, 'expect string B') }
Expand Down

0 comments on commit bc98cb9

Please sign in to comment.