Skip to content

Commit 60bd8a5

Browse files
jamestalmagenovemberborn
authored andcommitted
Protect against bad argument passed to t.throws. (#834)
If a non-function, non-promise, non-observable argument (i.e. a string) is passed to `t.throws`, the assertion passes. This obviously isn't what we want.
1 parent 3651241 commit 60bd8a5

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

lib/assert.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ x.throws = function (fn, err, msg) {
8484
});
8585
}
8686

87+
if (typeof fn !== 'function') {
88+
throw new TypeError('t.throws must be called with a function, Promise, or Observable');
89+
}
90+
8791
try {
8892
if (typeof err === 'string') {
8993
var errMsg = err;
@@ -123,6 +127,10 @@ x.notThrows = function (fn, msg) {
123127
});
124128
}
125129

130+
if (typeof fn !== 'function') {
131+
throw new TypeError('t.notThrows must be called with a function, Promise, or Observable');
132+
}
133+
126134
try {
127135
assert.doesNotThrow(fn, msg);
128136
} catch (err) {

test/assert.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,22 @@ test('.throws() returns the rejection reason of promise', function (t) {
285285
});
286286
});
287287

288+
test('.throws should throw if passed a bad value', function (t) {
289+
t.plan(1);
290+
291+
t.throws(function () {
292+
assert.throws('not a function');
293+
}, {name: 'TypeError', message: /t\.throws must be called with a function, Promise, or Observable/});
294+
});
295+
296+
test('.notThrows should throw if passed a bad value', function (t) {
297+
t.plan(1);
298+
299+
t.throws(function () {
300+
assert.notThrows('not a function');
301+
}, {name: 'TypeError', message: /t\.notThrows must be called with a function, Promise, or Observable/});
302+
});
303+
288304
test('.notThrows()', function (t) {
289305
t.doesNotThrow(function () {
290306
assert.notThrows(function () {});

test/test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,17 @@ test('handle notThrows with error', function (t) {
260260
t.end();
261261
});
262262

263+
test('fails if a bad value is passed to t.throws', function (t) {
264+
var result = ava(function (a) {
265+
a.throws('not a function');
266+
}).run();
267+
268+
t.is(result.passed, false);
269+
t.ok(result.reason);
270+
t.is(result.reason.name, 'TypeError');
271+
t.end();
272+
});
273+
263274
test('handle notThrows without error', function (t) {
264275
var result = ava(function (a) {
265276
a.notThrows(function () {

0 commit comments

Comments
 (0)