From 2411318f600ef3d0f16c315ff4e4e99f34d6e0eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Fri, 7 Apr 2017 16:15:46 +0200 Subject: [PATCH] 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 Reviewed-By: Rich Trott --- test/parallel/test-assert.js | 14 ++++++++++---- test/parallel/test-buffer.js | 18 ++++++++++-------- test/parallel/test-http-mutable-headers.js | 16 ++++++++++++---- test/parallel/test-repl-context.js | 2 +- .../test-vm-new-script-this-context.js | 2 +- 5 files changed, 34 insertions(+), 18 deletions(-) diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index ca25c9965d..74ef705f13 100644 --- a/test/parallel/test-assert.js +++ b/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); }); diff --git a/test/parallel/test-buffer.js b/test/parallel/test-buffer.js index 805c8b9e7a..a2fdab55b7 100644 --- a/test/parallel/test-buffer.js +++ b/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.'); diff --git a/test/parallel/test-http-mutable-headers.js b/test/parallel/test-http-mutable-headers.js index 8f9d597efb..2a4b0dc6c1 100644 --- a/test/parallel/test-http-mutable-headers.js +++ b/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'); diff --git a/test/parallel/test-repl-context.js b/test/parallel/test-repl-context.js index 2287a247d8..a2f0421431 100644 --- a/test/parallel/test-repl-context.js +++ b/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); } diff --git a/test/parallel/test-vm-new-script-this-context.js b/test/parallel/test-vm-new-script-this-context.js index 3df5dc1b50..62aecfed28 100644 --- a/test/parallel/test-vm-new-script-this-context.js +++ b/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');