test: validate more properties in expectsError
PR-URL: https://github.com/nodejs/node/pull/14058 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
This commit is contained in:
parent
b55ab01b45
commit
2a621d4051
@ -51,28 +51,41 @@ Platform normalizes the `dd` command
|
|||||||
Check if there is more than 1gb of total memory.
|
Check if there is more than 1gb of total memory.
|
||||||
|
|
||||||
### expectsError([fn, ]settings[, exact])
|
### expectsError([fn, ]settings[, exact])
|
||||||
* `fn` [<Function>]
|
* `fn` [<Function>] a function that should throw.
|
||||||
* `settings` [<Object>]
|
* `settings` [<Object>]
|
||||||
with the following optional properties:
|
that must contain the `code` property plus any of the other following
|
||||||
|
properties (some properties only apply for `AssertionError`):
|
||||||
* `code` [<String>]
|
* `code` [<String>]
|
||||||
expected error must have this value for its `code` property
|
expected error must have this value for its `code` property.
|
||||||
* `type` [<Function>]
|
* `type` [<Function>]
|
||||||
expected error must be an instance of `type`
|
expected error must be an instance of `type` and must be an Error subclass.
|
||||||
* `message` [<String>]
|
* `message` [<String>] or [<RegExp>]
|
||||||
or [<RegExp>]
|
|
||||||
if a string is provided for `message`, expected error must have it for its
|
if a string is provided for `message`, expected error must have it for its
|
||||||
`message` property; if a regular expression is provided for `message`, the
|
`message` property; if a regular expression is provided for `message`, the
|
||||||
regular expression must match the `message` property of the expected error
|
regular expression must match the `message` property of the expected error.
|
||||||
|
* `name` [<String>]
|
||||||
|
expected error must have this value for its `name` property.
|
||||||
|
* `generatedMessage` [<String>]
|
||||||
|
(`AssertionError` only) expected error must have this value for its
|
||||||
|
`generatedMessage` property.
|
||||||
|
* `actual` <any>
|
||||||
|
(`AssertionError` only) expected error must have this value for its
|
||||||
|
`actual` property.
|
||||||
|
* `expected` <any>
|
||||||
|
(`AssertionError` only) expected error must have this value for its
|
||||||
|
`expected` property.
|
||||||
|
* `operator` <any>
|
||||||
|
(`AssertionError` only) expected error must have this value for its
|
||||||
|
`operator` property.
|
||||||
* `exact` [<Number>] default = 1
|
* `exact` [<Number>] default = 1
|
||||||
|
* return [<Function>]
|
||||||
|
|
||||||
* return function suitable for use as a validation function passed as the second
|
If `fn` is provided, it will be passed to `assert.throws` as first argument
|
||||||
argument to e.g. `assert.throws()`. If the returned function has not been
|
and `undefined` will be returned.
|
||||||
called exactly `exact` number of times when the test is complete, then the
|
Otherwise a function suitable as callback or for use as a validation function
|
||||||
test will fail.
|
passed as the second argument to `assert.throws()` will be returned. If the
|
||||||
|
returned function has not been called exactly `exact` number of times when the
|
||||||
If `fn` is provided, it will be passed to `assert.throws` as first argument.
|
test is complete, then the test will fail.
|
||||||
|
|
||||||
The expected error should be [subclassed by the `internal/errors` module](https://github.com/nodejs/node/blob/master/doc/guides/using-internal-errors.md#api).
|
|
||||||
|
|
||||||
### expectWarning(name, expected)
|
### expectWarning(name, expected)
|
||||||
* `name` [<String>]
|
* `name` [<String>]
|
||||||
|
@ -696,24 +696,43 @@ Object.defineProperty(exports, 'hasSmallICU', {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Useful for testing expected internal/error objects
|
// Useful for testing expected internal/error objects
|
||||||
exports.expectsError = function expectsError(fn, options, exact) {
|
exports.expectsError = function expectsError(fn, settings, exact) {
|
||||||
if (typeof fn !== 'function') {
|
if (typeof fn !== 'function') {
|
||||||
exact = options;
|
exact = settings;
|
||||||
options = fn;
|
settings = fn;
|
||||||
fn = undefined;
|
fn = undefined;
|
||||||
}
|
}
|
||||||
const { code, type, message } = options;
|
|
||||||
const innerFn = exports.mustCall(function(error) {
|
const innerFn = exports.mustCall(function(error) {
|
||||||
assert.strictEqual(error.code, code);
|
assert.strictEqual(error.code, settings.code);
|
||||||
if (type !== undefined) {
|
if ('type' in settings) {
|
||||||
|
const type = settings.type;
|
||||||
|
if (type !== Error && !Error.isPrototypeOf(type)) {
|
||||||
|
throw new TypeError('`settings.type` must inherit from `Error`');
|
||||||
|
}
|
||||||
assert(error instanceof type,
|
assert(error instanceof type,
|
||||||
`${error} is not the expected type ${type}`);
|
`${error.name} is not instance of ${type.name}`);
|
||||||
}
|
}
|
||||||
if (message instanceof RegExp) {
|
if ('message' in settings) {
|
||||||
assert(message.test(error.message),
|
const message = settings.message;
|
||||||
`${error.message} does not match ${message}`);
|
if (typeof message === 'string') {
|
||||||
} else if (typeof message === 'string') {
|
assert.strictEqual(error.message, message);
|
||||||
assert.strictEqual(error.message, message);
|
} else {
|
||||||
|
assert(message.test(error.message),
|
||||||
|
`${error.message} does not match ${message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ('name' in settings) {
|
||||||
|
assert.strictEqual(error.name, settings.name);
|
||||||
|
}
|
||||||
|
if (error.constructor.name === 'AssertionError') {
|
||||||
|
['generatedMessage', 'actual', 'expected', 'operator'].forEach((key) => {
|
||||||
|
if (key in settings) {
|
||||||
|
const actual = error[key];
|
||||||
|
const expected = settings[key];
|
||||||
|
assert.strictEqual(actual, expected,
|
||||||
|
`${key}: expected ${expected}, not ${actual}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}, exact);
|
}, exact);
|
||||||
|
@ -166,7 +166,7 @@ assert.throws(() => {
|
|||||||
}, common.expectsError({ code: 'TEST_ERROR_1', type: RangeError }));
|
}, common.expectsError({ code: 'TEST_ERROR_1', type: RangeError }));
|
||||||
}, common.expectsError({
|
}, common.expectsError({
|
||||||
code: 'ERR_ASSERTION',
|
code: 'ERR_ASSERTION',
|
||||||
message: /^.+ is not the expected type \S/
|
message: /^.+ is not instance of \S/
|
||||||
}));
|
}));
|
||||||
|
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user