test: add optional throw fn to expectsError
PR-URL: https://github.com/nodejs/node/pull/14089 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Refael Ackermann <refack@gmail.com>
This commit is contained in:
parent
44483b6898
commit
d6fece1436
@ -50,7 +50,8 @@ 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(settings)
|
### expectsError([fn, ]settings)
|
||||||
|
* `fn` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function)
|
||||||
* `settings` [<Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
|
* `settings` [<Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
|
||||||
with the following optional properties:
|
with the following optional properties:
|
||||||
* `code` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)
|
* `code` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)
|
||||||
@ -66,6 +67,8 @@ Check if there is more than 1gb of total memory.
|
|||||||
* return function suitable for use as a validation function passed as the second
|
* return function suitable for use as a validation function passed as the second
|
||||||
argument to `assert.throws()`
|
argument to `assert.throws()`
|
||||||
|
|
||||||
|
If `fn` is provided, it will be passed to `assert.throws` as first argument.
|
||||||
|
|
||||||
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).
|
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)
|
||||||
|
@ -702,8 +702,13 @@ Object.defineProperty(exports, 'hasSmallICU', {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Useful for testing expected internal/error objects
|
// Useful for testing expected internal/error objects
|
||||||
exports.expectsError = function expectsError({code, type, message}) {
|
exports.expectsError = function expectsError(fn, options) {
|
||||||
return function(error) {
|
if (typeof fn !== 'function') {
|
||||||
|
options = fn;
|
||||||
|
fn = undefined;
|
||||||
|
}
|
||||||
|
const { code, type, message } = options;
|
||||||
|
function innerFn(error) {
|
||||||
assert.strictEqual(error.code, code);
|
assert.strictEqual(error.code, code);
|
||||||
if (type !== undefined) {
|
if (type !== undefined) {
|
||||||
assert(error instanceof type,
|
assert(error instanceof type,
|
||||||
@ -716,7 +721,12 @@ exports.expectsError = function expectsError({code, type, message}) {
|
|||||||
assert.strictEqual(error.message, message);
|
assert.strictEqual(error.message, message);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
};
|
}
|
||||||
|
if (fn) {
|
||||||
|
assert.throws(fn, innerFn);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return innerFn;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.skipIfInspectorDisabled = function skipIfInspectorDisabled() {
|
exports.skipIfInspectorDisabled = function skipIfInspectorDisabled() {
|
||||||
|
@ -114,12 +114,12 @@ for (const a of similar) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.throws(
|
common.expectsError(() => {
|
||||||
() => { assert.deepEqual(new Set([{a: 0}]), new Set([{a: 1}])); },
|
assert.deepEqual(new Set([{a: 0}]), new Set([{a: 1}]));
|
||||||
common.expectsError({
|
}, {
|
||||||
code: 'ERR_ASSERTION',
|
code: 'ERR_ASSERTION',
|
||||||
message: /^Set { { a: 0 } } deepEqual Set { { a: 1 } }$/
|
message: /^Set { { a: 0 } } deepEqual Set { { a: 1 } }$/
|
||||||
}));
|
});
|
||||||
|
|
||||||
function assertDeepAndStrictEqual(a, b) {
|
function assertDeepAndStrictEqual(a, b) {
|
||||||
assert.deepEqual(a, b);
|
assert.deepEqual(a, b);
|
||||||
|
@ -17,57 +17,52 @@ assert.throws(
|
|||||||
);
|
);
|
||||||
|
|
||||||
// One arg = message
|
// One arg = message
|
||||||
assert.throws(
|
common.expectsError(() => {
|
||||||
() => { assert.fail('custom message'); },
|
assert.fail('custom message');
|
||||||
common.expectsError({
|
}, {
|
||||||
code: 'ERR_ASSERTION',
|
code: 'ERR_ASSERTION',
|
||||||
type: assert.AssertionError,
|
type: assert.AssertionError,
|
||||||
message: 'custom message',
|
message: 'custom message',
|
||||||
operator: undefined,
|
operator: undefined,
|
||||||
actual: undefined,
|
actual: undefined,
|
||||||
expected: undefined
|
expected: undefined
|
||||||
})
|
});
|
||||||
);
|
|
||||||
|
|
||||||
// Two args only, operator defaults to '!='
|
// Two args only, operator defaults to '!='
|
||||||
assert.throws(
|
common.expectsError(() => {
|
||||||
() => { assert.fail('first', 'second'); },
|
assert.fail('first', 'second');
|
||||||
common.expectsError({
|
}, {
|
||||||
code: 'ERR_ASSERTION',
|
code: 'ERR_ASSERTION',
|
||||||
type: assert.AssertionError,
|
type: assert.AssertionError,
|
||||||
message: '\'first\' != \'second\'',
|
message: '\'first\' != \'second\'',
|
||||||
operator: '!=',
|
operator: '!=',
|
||||||
actual: 'first',
|
actual: 'first',
|
||||||
expected: 'second'
|
expected: 'second'
|
||||||
|
});
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
// Three args
|
// Three args
|
||||||
assert.throws(
|
common.expectsError(() => {
|
||||||
() => { assert.fail('ignored', 'ignored', 'another custom message'); },
|
assert.fail('ignored', 'ignored', 'another custom message');
|
||||||
common.expectsError({
|
}, {
|
||||||
code: 'ERR_ASSERTION',
|
code: 'ERR_ASSERTION',
|
||||||
type: assert.AssertionError,
|
type: assert.AssertionError,
|
||||||
message: 'another custom message',
|
message: 'another custom message',
|
||||||
operator: undefined,
|
operator: undefined,
|
||||||
actual: 'ignored',
|
actual: 'ignored',
|
||||||
expected: 'ignored'
|
expected: 'ignored'
|
||||||
})
|
});
|
||||||
);
|
|
||||||
|
|
||||||
// No third arg (but a fourth arg)
|
// No third arg (but a fourth arg)
|
||||||
assert.throws(
|
common.expectsError(() => {
|
||||||
() => { assert.fail('first', 'second', undefined, 'operator'); },
|
assert.fail('first', 'second', undefined, 'operator');
|
||||||
common.expectsError({
|
}, {
|
||||||
code: 'ERR_ASSERTION',
|
code: 'ERR_ASSERTION',
|
||||||
type: assert.AssertionError,
|
type: assert.AssertionError,
|
||||||
message: '\'first\' operator \'second\'',
|
message: '\'first\' operator \'second\'',
|
||||||
operator: 'operator',
|
operator: 'operator',
|
||||||
actual: 'first',
|
actual: 'first',
|
||||||
expected: 'second'
|
expected: 'second'
|
||||||
})
|
});
|
||||||
);
|
|
||||||
|
|
||||||
// The stackFrameFunction should exclude the foo frame
|
// The stackFrameFunction should exclude the foo frame
|
||||||
assert.throws(
|
assert.throws(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user