Skip to content

Commit 1b2681d

Browse files
committed
[Fix] throws: fix crash when throwing primitives with a non-empty expected object
1 parent 9133c93 commit 1b2681d

File tree

2 files changed

+117
-15
lines changed

2 files changed

+117
-15
lines changed

Diff for: lib/test.js

+19-15
Original file line numberDiff line numberDiff line change
@@ -838,22 +838,26 @@ Test.prototype['throws'] = function (fn, expected, msg, extra) {
838838
passed = $exec(expected, caught.error) !== null;
839839
expected = inspect(expected);
840840
} else if (expected && typeof expected === 'object') { // Handle validation objects.
841-
var keys = objectKeys(expected);
842-
// Special handle errors to make sure the name and the message are compared as well.
843-
if (expected instanceof Error) {
844-
$push(keys, 'name', 'message');
845-
} else if (keys.length === 0) {
846-
throw new TypeError('`throws` validation object must not be empty');
847-
}
848-
passed = every(keys, function (key) {
849-
if (typeof caught.error[key] === 'string' && isRegExp(expected[key]) && $exec(expected[key], caught.error[key]) !== null) {
850-
return true;
851-
}
852-
if (key in caught.error && deepEqual(caught.error[key], expected[key], { strict: true })) {
853-
return true;
841+
if (caught.error && typeof caught.error === 'object') {
842+
var keys = objectKeys(expected);
843+
// Special handle errors to make sure the name and the message are compared as well.
844+
if (expected instanceof Error) {
845+
$push(keys, 'name', 'message');
846+
} else if (keys.length === 0) {
847+
throw new TypeError('`throws` validation object must not be empty');
854848
}
855-
return false;
856-
});
849+
passed = every(keys, function (key) {
850+
if (typeof caught.error[key] === 'string' && isRegExp(expected[key]) && $exec(expected[key], caught.error[key]) !== null) {
851+
return true;
852+
}
853+
if (key in caught.error && deepEqual(caught.error[key], expected[key], { strict: true })) {
854+
return true;
855+
}
856+
return false;
857+
});
858+
} else {
859+
passed = false;
860+
}
857861
}
858862
}
859863

Diff for: test/throws.js

+98
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,112 @@ tap.test('failures', function (tt) {
8484
'ok ' + ++count + ' unexpected error',
8585
'# throwing primitives',
8686
'ok ' + ++count + ' primitive: null, no expected',
87+
'not ok ' + ++count + ' primitive: null, with non-empty expected object',
88+
' ---',
89+
' operator: throws',
90+
' expected: |-',
91+
' { a: \'b\' }',
92+
' actual: |-',
93+
' null',
94+
' at: <anonymous> ($TEST/throws.js:$LINE:$COL)',
95+
' ...',
8796
'ok ' + ++count + ' primitive: undefined, no expected',
97+
'not ok ' + ++count + ' primitive: undefined, with non-empty expected object',
98+
' ---',
99+
' operator: throws',
100+
' expected: |-',
101+
' { a: \'b\' }',
102+
' actual: |-',
103+
' undefined',
104+
' at: <anonymous> ($TEST/throws.js:$LINE:$COL)',
105+
' stack: |-',
106+
' Error: primitive: undefined, with non-empty expected object',
107+
' [... stack stripped ...]',
108+
' at $TEST/throws.js:$LINE:$COL',
109+
' [... stack stripped ...]',
110+
' at Test.<anonymous> ($TEST/throws.js:$LINE:$COL)',
111+
' [... stack stripped ...]',
112+
' ...',
88113
'ok ' + ++count + ' primitive: 0, no expected',
114+
'not ok ' + ++count + ' primitive: 0, with non-empty expected object',
115+
' ---',
116+
' operator: throws',
117+
' expected: |-',
118+
' { a: \'b\' }',
119+
' actual: |-',
120+
' 0',
121+
' at: <anonymous> ($TEST/throws.js:$LINE:$COL)',
122+
' ...',
89123
'ok ' + ++count + ' primitive: NaN, no expected',
124+
'not ok ' + ++count + ' primitive: NaN, with non-empty expected object',
125+
' ---',
126+
' operator: throws',
127+
' expected: |-',
128+
' { a: \'b\' }',
129+
' actual: |-',
130+
' NaN',
131+
' at: <anonymous> ($TEST/throws.js:$LINE:$COL)',
132+
' ...',
90133
'ok ' + ++count + ' primitive: 42, no expected',
134+
'not ok ' + ++count + ' primitive: 42, with non-empty expected object',
135+
' ---',
136+
' operator: throws',
137+
' expected: |-',
138+
' { a: \'b\' }',
139+
' actual: |-',
140+
' 42',
141+
' at: <anonymous> ($TEST/throws.js:$LINE:$COL)',
142+
' ...',
91143
'ok ' + ++count + ' primitive: Infinity, no expected',
144+
'not ok ' + ++count + ' primitive: Infinity, with non-empty expected object',
145+
' ---',
146+
' operator: throws',
147+
' expected: |-',
148+
' { a: \'b\' }',
149+
' actual: |-',
150+
' Infinity',
151+
' at: <anonymous> ($TEST/throws.js:$LINE:$COL)',
152+
' ...',
92153
'ok ' + ++count + ' primitive: \'\', no expected',
154+
'not ok ' + ++count + ' primitive: \'\', with non-empty expected object',
155+
' ---',
156+
' operator: throws',
157+
' expected: |-',
158+
' { a: \'b\' }',
159+
' actual: |-',
160+
' \'\'',
161+
' at: <anonymous> ($TEST/throws.js:$LINE:$COL)',
162+
' ...',
93163
'ok ' + ++count + ' primitive: \'foo\', no expected',
164+
'not ok ' + ++count + ' primitive: \'foo\', with non-empty expected object',
165+
' ---',
166+
' operator: throws',
167+
' expected: |-',
168+
' { a: \'b\' }',
169+
' actual: |-',
170+
' \'foo\'',
171+
' at: <anonymous> ($TEST/throws.js:$LINE:$COL)',
172+
' ...',
94173
'ok ' + ++count + ' primitive: true, no expected',
174+
'not ok ' + ++count + ' primitive: true, with non-empty expected object',
175+
' ---',
176+
' operator: throws',
177+
' expected: |-',
178+
' { a: \'b\' }',
179+
' actual: |-',
180+
' true',
181+
' at: <anonymous> ($TEST/throws.js:$LINE:$COL)',
182+
' ...',
95183
'ok ' + ++count + ' primitive: false, no expected',
184+
'not ok ' + ++count + ' primitive: false, with non-empty expected object',
185+
' ---',
186+
' operator: throws',
187+
' expected: |-',
188+
' { a: \'b\' }',
189+
' actual: |-',
190+
' false',
191+
' at: <anonymous> ($TEST/throws.js:$LINE:$COL)',
192+
' ...',
96193
'# ambiguous arguments',
97194
'ok ' + ++count + ' Second',
98195
'ok ' + ++count + ' Second',
@@ -283,6 +380,7 @@ tap.test('failures', function (tt) {
283380
test('throwing primitives', function (t) {
284381
[null, undefined, 0, NaN, 42, Infinity, '', 'foo', true, false].forEach(function (primitive) {
285382
t.throws(function () { throw primitive; }, 'primitive: ' + inspect(primitive) + ', no expected');
383+
t.throws(function () { throw primitive; }, { a: 'b' }, 'primitive: ' + inspect(primitive) + ', with non-empty expected object');
286384
});
287385

288386
t.end();

0 commit comments

Comments
 (0)