Browse Source

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: https://github.com/nodejs/node/pull/12270
Backport-PR-URL: https://github.com/nodejs/node/pull/13785
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
v6.x
Michaël Zasso 8 years ago
committed by Myles Borins
parent
commit
2411318f60
No known key found for this signature in database GPG Key ID: 933B01F40B5CA946
  1. 14
      test/parallel/test-assert.js
  2. 18
      test/parallel/test-buffer.js
  3. 16
      test/parallel/test-http-mutable-headers.js
  4. 2
      test/parallel/test-repl-context.js
  5. 2
      test/parallel/test-vm-new-script-this-context.js

14
test/parallel/test-assert.js

@ -240,7 +240,8 @@ assert.throws(
{
const re1 = /a/;
re1.lastIndex = 3;
assert.throws(makeBlock(a.deepStrictEqual, re1, /a/));
assert.throws(makeBlock(a.deepStrictEqual, re1, /a/),
/^AssertionError: \/a\/ deepStrictEqual \/a\/$/);
}
// 7.4 - strict
@ -262,10 +263,12 @@ assert.doesNotThrow(makeBlock(a.deepStrictEqual, {a: 4}, {a: 4}));
assert.doesNotThrow(makeBlock(a.deepStrictEqual,
{a: 4, b: '2'},
{a: 4, b: '2'}));
assert.throws(makeBlock(a.deepStrictEqual, [4], ['4']));
assert.throws(makeBlock(a.deepStrictEqual, [4], ['4']),
/^AssertionError: \[ 4 ] deepStrictEqual \[ '4' ]$/);
assert.throws(makeBlock(a.deepStrictEqual, {a: 4}, {a: 4, b: true}),
a.AssertionError);
assert.throws(makeBlock(a.deepStrictEqual, ['a'], {0: 'a'}));
/^AssertionError: { a: 4 } deepStrictEqual { a: 4, b: true }$/);
assert.throws(makeBlock(a.deepStrictEqual, ['a'], {0: 'a'}),
/^AssertionError: \[ 'a' ] deepStrictEqual { '0': 'a' }$/);
//(although not necessarily the same order),
assert.doesNotThrow(makeBlock(a.deepStrictEqual,
{a: 4, b: '1'},
@ -342,9 +345,11 @@ function thrower(errorConstructor) {
assert.throws(makeBlock(thrower, a.AssertionError),
a.AssertionError, 'message');
assert.throws(makeBlock(thrower, a.AssertionError), a.AssertionError);
// eslint-disable-next-line assert-throws-arguments
assert.throws(makeBlock(thrower, a.AssertionError));
// if not passing an error, catch all.
// eslint-disable-next-line assert-throws-arguments
assert.throws(makeBlock(thrower, TypeError));
// when passing a type, only catch errors of the appropriate type
@ -517,6 +522,7 @@ testAssertionMessage({a: NaN, b: Infinity, c: -Infinity},
// #2893
try {
// eslint-disable-next-line assert-throws-arguments
assert.throws(function() {
assert.ifError(null);
});

18
test/parallel/test-buffer.js

@ -41,11 +41,11 @@ for (const [key, value] of e.entries()) {
assert.throws(function() {
Buffer(8).fill('a', -1);
});
}, /^RangeError: Out of range index$/);
assert.throws(function() {
Buffer(8).fill('a', 0, 9);
});
}, /^RangeError: Out of range index$/);
// Make sure this doesn't hang indefinitely.
Buffer(8).fill('');
@ -1433,17 +1433,17 @@ if (common.hasCrypto) {
assert.throws(function() {
const b = Buffer(1);
Buffer.compare(b, 'abc');
});
}, /^TypeError: Arguments must be Buffers$/);
assert.throws(function() {
const b = Buffer(1);
Buffer.compare('abc', b);
});
}, /^TypeError: Arguments must be Buffers$/);
assert.throws(function() {
const b = Buffer(1);
b.compare('abc');
});
}, /^TypeError: Argument must be a Buffer$/);
// Test Equals
{
@ -1461,10 +1461,12 @@ assert.throws(function() {
assert.throws(function() {
const b = Buffer(1);
b.equals('abc');
});
}, /^TypeError: Argument must be a Buffer$/);
// Regression test for https://github.com/nodejs/node/issues/649.
assert.throws(function() { Buffer(1422561062959).toString('utf8'); });
assert.throws(function() {
Buffer(1422561062959).toString('utf8');
}, /^RangeError: Invalid typed array length$/);
const ps = Buffer.poolSize;
Buffer.poolSize = 0;
@ -1474,7 +1476,7 @@ Buffer.poolSize = ps;
// Test Buffer.copy() segfault
assert.throws(function() {
Buffer(10).copy();
});
}, /^TypeError: argument should be a Buffer$/);
const regErrorMsg = new RegExp('First argument must be a string, Buffer, ' +
'ArrayBuffer, Array, or array-like object.');

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

@ -22,10 +22,18 @@ const cookies = [
const s = http.createServer(function(req, res) {
switch (test) {
case 'headers':
assert.throws(function() { res.setHeader(); });
assert.throws(function() { res.setHeader('someHeader'); });
assert.throws(function() { res.getHeader(); });
assert.throws(function() { res.removeHeader(); });
assert.throws(() => {
res.setHeader();
}, /^TypeError: Header name must be a valid HTTP Token \["undefined"\]$/);
assert.throws(() => {
res.setHeader('someHeader');
}, /^Error: "value" required in setHeader\("someHeader", value\)$/);
assert.throws(() => {
res.getHeader();
}, /^Error: "name" argument is required for getHeader\(name\)$/);
assert.throws(() => {
res.removeHeader();
}, /^Error: "name" argument is required for removeHeader\(name\)$/);
res.setHeader('x-test-header', 'testing');
res.setHeader('X-TEST-HEADER2', 'testing');

2
test/parallel/test-repl-context.js

@ -22,5 +22,5 @@ function testContext(repl) {
assert.strictEqual(context.global, context);
// ensure that the repl console instance does not have a setter
assert.throws(() => context.console = 'foo');
assert.throws(() => context.console = 'foo', TypeError);
}

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

@ -14,7 +14,7 @@ console.error('thrown error');
script = new Script('throw new Error(\'test\');');
assert.throws(function() {
script.runInThisContext(script);
});
}, /^Error: test$/);
global.hello = 5;
script = new Script('hello = 2');

Loading…
Cancel
Save