Skip to content

Commit 751896b

Browse files
targosgibfahn
authored andcommitted
test: add second argument to assert.throws
This adds RegExp or error constructor arguments to the remaining places where it is missing in preparation for the commit that will enforce the presence of at least two arguments. PR-URL: #12270 Backport-PR-URL: #13785 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 8032e0a commit 751896b

5 files changed

+34
-18
lines changed

test/parallel/test-assert.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ assert.throws(
240240
{
241241
const re1 = /a/;
242242
re1.lastIndex = 3;
243-
assert.throws(makeBlock(a.deepStrictEqual, re1, /a/));
243+
assert.throws(makeBlock(a.deepStrictEqual, re1, /a/),
244+
/^AssertionError: \/a\/ deepStrictEqual \/a\/$/);
244245
}
245246

246247
// 7.4 - strict
@@ -262,10 +263,12 @@ assert.doesNotThrow(makeBlock(a.deepStrictEqual, {a: 4}, {a: 4}));
262263
assert.doesNotThrow(makeBlock(a.deepStrictEqual,
263264
{a: 4, b: '2'},
264265
{a: 4, b: '2'}));
265-
assert.throws(makeBlock(a.deepStrictEqual, [4], ['4']));
266+
assert.throws(makeBlock(a.deepStrictEqual, [4], ['4']),
267+
/^AssertionError: \[ 4 ] deepStrictEqual \[ '4' ]$/);
266268
assert.throws(makeBlock(a.deepStrictEqual, {a: 4}, {a: 4, b: true}),
267-
a.AssertionError);
268-
assert.throws(makeBlock(a.deepStrictEqual, ['a'], {0: 'a'}));
269+
/^AssertionError: { a: 4 } deepStrictEqual { a: 4, b: true }$/);
270+
assert.throws(makeBlock(a.deepStrictEqual, ['a'], {0: 'a'}),
271+
/^AssertionError: \[ 'a' ] deepStrictEqual { '0': 'a' }$/);
269272
//(although not necessarily the same order),
270273
assert.doesNotThrow(makeBlock(a.deepStrictEqual,
271274
{a: 4, b: '1'},
@@ -342,9 +345,11 @@ function thrower(errorConstructor) {
342345
assert.throws(makeBlock(thrower, a.AssertionError),
343346
a.AssertionError, 'message');
344347
assert.throws(makeBlock(thrower, a.AssertionError), a.AssertionError);
348+
// eslint-disable-next-line assert-throws-arguments
345349
assert.throws(makeBlock(thrower, a.AssertionError));
346350

347351
// if not passing an error, catch all.
352+
// eslint-disable-next-line assert-throws-arguments
348353
assert.throws(makeBlock(thrower, TypeError));
349354

350355
// when passing a type, only catch errors of the appropriate type
@@ -517,6 +522,7 @@ testAssertionMessage({a: NaN, b: Infinity, c: -Infinity},
517522

518523
// #2893
519524
try {
525+
// eslint-disable-next-line assert-throws-arguments
520526
assert.throws(function() {
521527
assert.ifError(null);
522528
});

test/parallel/test-buffer.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ for (const [key, value] of e.entries()) {
4141

4242
assert.throws(function() {
4343
Buffer(8).fill('a', -1);
44-
});
44+
}, /^RangeError: Out of range index$/);
4545

4646
assert.throws(function() {
4747
Buffer(8).fill('a', 0, 9);
48-
});
48+
}, /^RangeError: Out of range index$/);
4949

5050
// Make sure this doesn't hang indefinitely.
5151
Buffer(8).fill('');
@@ -1433,17 +1433,17 @@ if (common.hasCrypto) {
14331433
assert.throws(function() {
14341434
const b = Buffer(1);
14351435
Buffer.compare(b, 'abc');
1436-
});
1436+
}, /^TypeError: Arguments must be Buffers$/);
14371437

14381438
assert.throws(function() {
14391439
const b = Buffer(1);
14401440
Buffer.compare('abc', b);
1441-
});
1441+
}, /^TypeError: Arguments must be Buffers$/);
14421442

14431443
assert.throws(function() {
14441444
const b = Buffer(1);
14451445
b.compare('abc');
1446-
});
1446+
}, /^TypeError: Argument must be a Buffer$/);
14471447

14481448
// Test Equals
14491449
{
@@ -1461,10 +1461,12 @@ assert.throws(function() {
14611461
assert.throws(function() {
14621462
const b = Buffer(1);
14631463
b.equals('abc');
1464-
});
1464+
}, /^TypeError: Argument must be a Buffer$/);
14651465

14661466
// Regression test for https://github.com/nodejs/node/issues/649.
1467-
assert.throws(function() { Buffer(1422561062959).toString('utf8'); });
1467+
assert.throws(function() {
1468+
Buffer(1422561062959).toString('utf8');
1469+
}, /^RangeError: Invalid typed array length$/);
14681470

14691471
const ps = Buffer.poolSize;
14701472
Buffer.poolSize = 0;
@@ -1474,7 +1476,7 @@ Buffer.poolSize = ps;
14741476
// Test Buffer.copy() segfault
14751477
assert.throws(function() {
14761478
Buffer(10).copy();
1477-
});
1479+
}, /^TypeError: argument should be a Buffer$/);
14781480

14791481
const regErrorMsg = new RegExp('First argument must be a string, Buffer, ' +
14801482
'ArrayBuffer, Array, or array-like object.');

test/parallel/test-http-mutable-headers.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,18 @@ const cookies = [
2222
const s = http.createServer(function(req, res) {
2323
switch (test) {
2424
case 'headers':
25-
assert.throws(function() { res.setHeader(); });
26-
assert.throws(function() { res.setHeader('someHeader'); });
27-
assert.throws(function() { res.getHeader(); });
28-
assert.throws(function() { res.removeHeader(); });
25+
assert.throws(() => {
26+
res.setHeader();
27+
}, /^TypeError: Header name must be a valid HTTP Token \["undefined"\]$/);
28+
assert.throws(() => {
29+
res.setHeader('someHeader');
30+
}, /^Error: "value" required in setHeader\("someHeader", value\)$/);
31+
assert.throws(() => {
32+
res.getHeader();
33+
}, /^Error: "name" argument is required for getHeader\(name\)$/);
34+
assert.throws(() => {
35+
res.removeHeader();
36+
}, /^Error: "name" argument is required for removeHeader\(name\)$/);
2937

3038
res.setHeader('x-test-header', 'testing');
3139
res.setHeader('X-TEST-HEADER2', 'testing');

test/parallel/test-repl-context.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ function testContext(repl) {
2222
assert.strictEqual(context.global, context);
2323

2424
// ensure that the repl console instance does not have a setter
25-
assert.throws(() => context.console = 'foo');
25+
assert.throws(() => context.console = 'foo', TypeError);
2626
}

test/parallel/test-vm-new-script-this-context.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ console.error('thrown error');
1414
script = new Script('throw new Error(\'test\');');
1515
assert.throws(function() {
1616
script.runInThisContext(script);
17-
});
17+
}, /^Error: test$/);
1818

1919
global.hello = 5;
2020
script = new Script('hello = 2');

0 commit comments

Comments
 (0)