Skip to content

Commit 4c0d9e6

Browse files
committed
Merge pull request #268 from ljharb/throws_non_function_should_fail
[Fix] Ensure that non-functions passed to `throws` fail the test, just like `assert`
1 parent aa5bd84 commit 4c0d9e6

File tree

2 files changed

+136
-1
lines changed

2 files changed

+136
-1
lines changed

lib/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ Test.prototype['throws'] = function (fn, expected, msg, extra) {
425425
expected = String(expected);
426426
}
427427

428-
this._assert(passed, {
428+
this._assert(typeof fn === 'function' && passed, {
429429
message : defined(msg, 'should throw'),
430430
operator : 'throws',
431431
actual : caught && caught.error,

test/throws.js

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
var tape = require('../');
2+
var tap = require('tap');
3+
var concat = require('concat-stream');
4+
5+
function fn() {
6+
throw new TypeError('RegExp');
7+
}
8+
9+
function getNonFunctionMessage(fn) {
10+
try {
11+
fn();
12+
} catch (e) {
13+
return e.message;
14+
}
15+
}
16+
17+
tape('throws', function (t) {
18+
t.throws(fn);
19+
t.end();
20+
});
21+
22+
tape('throws (RegExp match)', function (t) {
23+
t.throws(fn, /RegExp/);
24+
t.end();
25+
});
26+
27+
tape('throws (Function match)', function (t) {
28+
t.throws(fn, TypeError);
29+
t.end();
30+
});
31+
32+
tap.test('failures', function (tt) {
33+
tt.plan(1);
34+
35+
var test = tape.createHarness();
36+
test.createStream().pipe(concat(function (body) {
37+
tt.equal(
38+
body.toString('utf8'),
39+
'TAP version 13\n'
40+
+ '# non functions\n'
41+
+ 'not ok 1 should throw\n'
42+
+ ' ---\n'
43+
+ ' operator: throws\n'
44+
+ ' expected: |-\n'
45+
+ ' undefined\n'
46+
+ ' actual: |-\n'
47+
+ " { [TypeError: " + getNonFunctionMessage() + "] message: '" + getNonFunctionMessage() + "' }\n"
48+
+ ' ...\n'
49+
+ 'not ok 2 should throw\n'
50+
+ ' ---\n'
51+
+ ' operator: throws\n'
52+
+ ' expected: |-\n'
53+
+ ' undefined\n'
54+
+ ' actual: |-\n'
55+
+ " { [TypeError: " + getNonFunctionMessage(null) + "] message: '" + getNonFunctionMessage(null) + "' }\n"
56+
+ ' ...\n'
57+
+ 'not ok 3 should throw\n'
58+
+ ' ---\n'
59+
+ ' operator: throws\n'
60+
+ ' expected: |-\n'
61+
+ ' undefined\n'
62+
+ ' actual: |-\n'
63+
+ " { [TypeError: " + getNonFunctionMessage(true) + "] message: '" + getNonFunctionMessage(true) + "' }\n"
64+
+ ' ...\n'
65+
+ 'not ok 4 should throw\n'
66+
+ ' ---\n'
67+
+ ' operator: throws\n'
68+
+ ' expected: |-\n'
69+
+ ' undefined\n'
70+
+ ' actual: |-\n'
71+
+ " { [TypeError: " + getNonFunctionMessage(false) + "] message: '" + getNonFunctionMessage(false) + "' }\n"
72+
+ ' ...\n'
73+
+ 'not ok 5 should throw\n'
74+
+ ' ---\n'
75+
+ ' operator: throws\n'
76+
+ ' expected: |-\n'
77+
+ ' undefined\n'
78+
+ ' actual: |-\n'
79+
+ " { [TypeError: " + getNonFunctionMessage('abc') + "] message: '" + getNonFunctionMessage('abc') + "' }\n"
80+
+ ' ...\n'
81+
+ 'not ok 6 should throw\n'
82+
+ ' ---\n'
83+
+ ' operator: throws\n'
84+
+ ' expected: |-\n'
85+
+ ' undefined\n'
86+
+ ' actual: |-\n'
87+
+ " { [TypeError: " + getNonFunctionMessage(/a/g) + "] message: '" + getNonFunctionMessage(/a/g) + "' }\n"
88+
+ ' ...\n'
89+
+ 'not ok 7 should throw\n'
90+
+ ' ---\n'
91+
+ ' operator: throws\n'
92+
+ ' expected: |-\n'
93+
+ ' undefined\n'
94+
+ ' actual: |-\n'
95+
+ " { [TypeError: " + getNonFunctionMessage([]) + "] message: '" + getNonFunctionMessage([]) + "' }\n"
96+
+ ' ...\n'
97+
+ 'not ok 8 should throw\n'
98+
+ ' ---\n'
99+
+ ' operator: throws\n'
100+
+ ' expected: |-\n'
101+
+ ' undefined\n'
102+
+ ' actual: |-\n'
103+
+ " { [TypeError: " + getNonFunctionMessage({}) + "] message: '" + getNonFunctionMessage({}) + "' }\n"
104+
+ ' ...\n'
105+
+ '# function\n'
106+
+ 'not ok 9 should throw\n'
107+
+ ' ---\n'
108+
+ ' operator: throws\n'
109+
+ ' expected: undefined\n'
110+
+ ' actual: undefined\n'
111+
+ ' ...\n\n'
112+
+ '1..9\n'
113+
+ '# tests 9\n'
114+
+ '# pass 0\n'
115+
+ '# fail 9\n'
116+
);
117+
}));
118+
119+
test('non functions', function (t) {
120+
t.plan(8);
121+
t.throws();
122+
t.throws(null);
123+
t.throws(true);
124+
t.throws(false);
125+
t.throws('abc');
126+
t.throws(/a/g);
127+
t.throws([]);
128+
t.throws({});
129+
});
130+
131+
test('function', function (t) {
132+
t.plan(1);
133+
t.throws(function () {});
134+
});
135+
});

0 commit comments

Comments
 (0)