mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
assert: fix AssertionError, assign error code
Using `assert.AssertionError()` without the `new` keyword results
in a non-intuitive error:
```js
> assert.AssertionError({})
TypeError: Cannot assign to read only property 'name' of function 'function ok(value, message) {
if (!value) fail(value, true, message, '==', assert.ok);
}'
at Function.AssertionError (assert.js:45:13)
at repl:1:8
at realRunInThisContextScript (vm.js:22:35)
at sigintHandlersWrap (vm.js:98:12)
at ContextifyScript.Script.runInThisContext (vm.js:24:12)
at REPLServer.defaultEval (repl.js:346:29)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.onLine (repl.js:545:10)
at emitOne (events.js:101:20)
>
```
The `assert.AssertionError()` can only be used correctly with `new`,
so this converts it into a proper ES6 class that will give an
appropriate error message.
This also associates the appropriate internal/errors code with all
`assert.AssertionError` instances and updates the appropriate test
cases.
PR-URL: https://github.com/nodejs/node/pull/12651
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
This commit is contained in:
1 parent
c1b3b95939
commit
e48d58b8b2
12 files changed
+278
-108
No files matched your search
@@ -41,47 +41,89 @@ assert.strictEqual(err5.code, 'TEST_ERROR_1');
|
||||
|
||||
assert.throws(
|
||||
() => new errors.Error('TEST_FOO_KEY'),
|
||||
/^AssertionError: An invalid error message key was used: TEST_FOO_KEY\.$/);
|
||||
common.expectsError({
|
||||
code: 'ERR_ASSERTION',
|
||||
message: /^An invalid error message key was used: TEST_FOO_KEY\.$/
|
||||
}));
|
||||
// Calling it twice yields same result (using the key does not create it)
|
||||
assert.throws(
|
||||
() => new errors.Error('TEST_FOO_KEY'),
|
||||
/^AssertionError: An invalid error message key was used: TEST_FOO_KEY\.$/);
|
||||
common.expectsError({
|
||||
code: 'ERR_ASSERTION',
|
||||
message: /^An invalid error message key was used: TEST_FOO_KEY\.$/
|
||||
}));
|
||||
assert.throws(
|
||||
() => new errors.Error(1),
|
||||
/^AssertionError: 'number' === 'string'$/);
|
||||
common.expectsError({
|
||||
code: 'ERR_ASSERTION',
|
||||
message: /^'number' === 'string'$/
|
||||
}));
|
||||
assert.throws(
|
||||
() => new errors.Error({}),
|
||||
/^AssertionError: 'object' === 'string'$/);
|
||||
common.expectsError({
|
||||
code: 'ERR_ASSERTION',
|
||||
message: /^'object' === 'string'$/
|
||||
}));
|
||||
assert.throws(
|
||||
() => new errors.Error([]),
|
||||
/^AssertionError: 'object' === 'string'$/);
|
||||
common.expectsError({
|
||||
code: 'ERR_ASSERTION',
|
||||
message: /^'object' === 'string'$/
|
||||
}));
|
||||
assert.throws(
|
||||
() => new errors.Error(true),
|
||||
/^AssertionError: 'boolean' === 'string'$/);
|
||||
common.expectsError({
|
||||
code: 'ERR_ASSERTION',
|
||||
message: /^'boolean' === 'string'$/
|
||||
}));
|
||||
assert.throws(
|
||||
() => new errors.TypeError(1),
|
||||
/^AssertionError: 'number' === 'string'$/);
|
||||
common.expectsError({
|
||||
code: 'ERR_ASSERTION',
|
||||
message: /^'number' === 'string'$/
|
||||
}));
|
||||
assert.throws(
|
||||
() => new errors.TypeError({}),
|
||||
/^AssertionError: 'object' === 'string'$/);
|
||||
common.expectsError({
|
||||
code: 'ERR_ASSERTION',
|
||||
message: /^'object' === 'string'$/
|
||||
}));
|
||||
assert.throws(
|
||||
() => new errors.TypeError([]),
|
||||
/^AssertionError: 'object' === 'string'$/);
|
||||
common.expectsError({
|
||||
code: 'ERR_ASSERTION',
|
||||
message: /^'object' === 'string'$/
|
||||
}));
|
||||
assert.throws(
|
||||
() => new errors.TypeError(true),
|
||||
/^AssertionError: 'boolean' === 'string'$/);
|
||||
common.expectsError({
|
||||
code: 'ERR_ASSERTION',
|
||||
message: /^'boolean' === 'string'$/
|
||||
}));
|
||||
assert.throws(
|
||||
() => new errors.RangeError(1),
|
||||
/^AssertionError: 'number' === 'string'$/);
|
||||
common.expectsError({
|
||||
code: 'ERR_ASSERTION',
|
||||
message: /^'number' === 'string'$/
|
||||
}));
|
||||
assert.throws(
|
||||
() => new errors.RangeError({}),
|
||||
/^AssertionError: 'object' === 'string'$/);
|
||||
common.expectsError({
|
||||
code: 'ERR_ASSERTION',
|
||||
message: /^'object' === 'string'$/
|
||||
}));
|
||||
assert.throws(
|
||||
() => new errors.RangeError([]),
|
||||
/^AssertionError: 'object' === 'string'$/);
|
||||
common.expectsError({
|
||||
code: 'ERR_ASSERTION',
|
||||
message: /^'object' === 'string'$/
|
||||
}));
|
||||
assert.throws(
|
||||
() => new errors.RangeError(true),
|
||||
/^AssertionError: 'boolean' === 'string'$/);
|
||||
common.expectsError({
|
||||
code: 'ERR_ASSERTION',
|
||||
message: /^'boolean' === 'string'$/
|
||||
}));
|
||||
|
||||
|
||||
// Tests for common.expectsError
|
||||
@@ -115,7 +157,10 @@ assert.throws(() => {
|
||||
assert.throws(() => {
|
||||
throw new errors.TypeError('TEST_ERROR_1', 'a');
|
||||
}, common.expectsError({ code: 'TEST_ERROR_1', type: RangeError }));
|
||||
}, /^AssertionError: .+ is not the expected type \S/);
|
||||
}, common.expectsError({
|
||||
code: 'ERR_ASSERTION',
|
||||
message: /^.+ is not the expected type \S/
|
||||
}));
|
||||
|
||||
assert.throws(() => {
|
||||
assert.throws(() => {
|
||||
@@ -123,7 +168,10 @@ assert.throws(() => {
|
||||
}, common.expectsError({ code: 'TEST_ERROR_1',
|
||||
type: TypeError,
|
||||
message: /^Error for testing 2/ }));
|
||||
}, /AssertionError: .+ does not match \S/);
|
||||
}, common.expectsError({
|
||||
code: 'ERR_ASSERTION',
|
||||
message: /.+ does not match \S/
|
||||
}));
|
||||
|
||||
// // Test ERR_INVALID_ARG_TYPE
|
||||
assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', ['a', 'b']),
|
||||
@@ -156,8 +204,10 @@ assert.strictEqual(errors.message('ERR_INVALID_URL_SCHEME', [['a', 'b', 'c']]),
|
||||
'The URL must be one of scheme a, b, or c');
|
||||
assert.throws(
|
||||
() => errors.message('ERR_INVALID_URL_SCHEME', [[]]),
|
||||
/^AssertionError: At least one expected value needs to be specified$/
|
||||
);
|
||||
common.expectsError({
|
||||
code: 'ERR_ASSERTION',
|
||||
message: /^At least one expected value needs to be specified$/
|
||||
}));
|
||||
|
||||
// Test ERR_MISSING_ARGS
|
||||
assert.strictEqual(errors.message('ERR_MISSING_ARGS', ['name']),
|
||||
@@ -168,5 +218,7 @@ assert.strictEqual(errors.message('ERR_MISSING_ARGS', ['a', 'b', 'c']),
|
||||
'The "a", "b", and "c" arguments must be specified');
|
||||
assert.throws(
|
||||
() => errors.message('ERR_MISSING_ARGS'),
|
||||
/^AssertionError: At least one arg needs to be specified$/
|
||||
);
|
||||
common.expectsError({
|
||||
code: 'ERR_ASSERTION',
|
||||
message: /^At least one arg needs to be specified$/
|
||||
}));
|
||||
Reference in new issue
Block a user