Skip to content

Commit 1c834e7

Browse files
larissayvetteTrott
authored andcommitted
errors,test: migrating error to internal/errors
PR-URL: #11505 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent d74a545 commit 1c834e7

File tree

2 files changed

+40
-32
lines changed

2 files changed

+40
-32
lines changed

lib/assert.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,9 @@ function _throws(shouldThrow, block, expected, message) {
513513
var actual;
514514

515515
if (typeof block !== 'function') {
516-
throw new TypeError('"block" argument must be a function');
516+
const errors = lazyErrors();
517+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'block', 'function',
518+
typeof block);
517519
}
518520

519521
if (typeof expected === 'string') {

test/parallel/test-assert.js

+37-31
Original file line numberDiff line numberDiff line change
@@ -655,40 +655,46 @@ try {
655655
'Message incorrectly marked as generated');
656656
}
657657

658-
// Verify that throws() and doesNotThrow() throw on non-function block
659-
function testBlockTypeError(method, block) {
660-
let threw = true;
661-
662-
try {
663-
method(block);
664-
threw = false;
665-
} catch (e) {
666-
assert.strictEqual(e.toString(),
667-
'TypeError: "block" argument must be a function');
668-
}
658+
{
659+
// Verify that throws() and doesNotThrow() throw on non-function block
660+
const validationFunction = common.expectsError({
661+
code: 'ERR_INVALID_ARG_TYPE',
662+
type: TypeError
663+
});
664+
665+
const testBlockTypeError = (method, block) => {
666+
let threw = true;
667+
668+
try {
669+
method(block);
670+
threw = false;
671+
} catch (e) {
672+
validationFunction(e);
673+
}
674+
675+
assert.ok(threw);
676+
};
669677

670-
assert.ok(threw);
678+
testBlockTypeError(assert.throws, 'string');
679+
testBlockTypeError(assert.doesNotThrow, 'string');
680+
testBlockTypeError(assert.throws, 1);
681+
testBlockTypeError(assert.doesNotThrow, 1);
682+
testBlockTypeError(assert.throws, true);
683+
testBlockTypeError(assert.doesNotThrow, true);
684+
testBlockTypeError(assert.throws, false);
685+
testBlockTypeError(assert.doesNotThrow, false);
686+
testBlockTypeError(assert.throws, []);
687+
testBlockTypeError(assert.doesNotThrow, []);
688+
testBlockTypeError(assert.throws, {});
689+
testBlockTypeError(assert.doesNotThrow, {});
690+
testBlockTypeError(assert.throws, /foo/);
691+
testBlockTypeError(assert.doesNotThrow, /foo/);
692+
testBlockTypeError(assert.throws, null);
693+
testBlockTypeError(assert.doesNotThrow, null);
694+
testBlockTypeError(assert.throws, undefined);
695+
testBlockTypeError(assert.doesNotThrow, undefined);
671696
}
672697

673-
testBlockTypeError(assert.throws, 'string');
674-
testBlockTypeError(assert.doesNotThrow, 'string');
675-
testBlockTypeError(assert.throws, 1);
676-
testBlockTypeError(assert.doesNotThrow, 1);
677-
testBlockTypeError(assert.throws, true);
678-
testBlockTypeError(assert.doesNotThrow, true);
679-
testBlockTypeError(assert.throws, false);
680-
testBlockTypeError(assert.doesNotThrow, false);
681-
testBlockTypeError(assert.throws, []);
682-
testBlockTypeError(assert.doesNotThrow, []);
683-
testBlockTypeError(assert.throws, {});
684-
testBlockTypeError(assert.doesNotThrow, {});
685-
testBlockTypeError(assert.throws, /foo/);
686-
testBlockTypeError(assert.doesNotThrow, /foo/);
687-
testBlockTypeError(assert.throws, null);
688-
testBlockTypeError(assert.doesNotThrow, null);
689-
testBlockTypeError(assert.throws, undefined);
690-
testBlockTypeError(assert.doesNotThrow, undefined);
691-
692698
// https://github.com/nodejs/node/issues/3275
693699
// eslint-disable-next-line no-throw-literal
694700
assert.throws(() => { throw 'error'; }, (err) => err === 'error');

0 commit comments

Comments
 (0)