mirror of
https://github.com/nodejs/node.git
synced 2025-08-16 06:08:50 +02:00
assert: add rejects() and doesNotReject()
Implement asynchronous equivalent for assert.throws() and assert.doesNotThrow(). PR-URL: https://github.com/nodejs/node/pull/18023 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Shingo Inoue <leko.noor@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
parent
580ad0157a
commit
599337f43e
4 changed files with 206 additions and 15 deletions
|
@ -425,14 +425,23 @@ function getActual(block) {
|
|||
return NO_EXCEPTION_SENTINEL;
|
||||
}
|
||||
|
||||
// Expected to throw an error.
|
||||
assert.throws = function throws(block, error, message) {
|
||||
const actual = getActual(block);
|
||||
async function waitForActual(block) {
|
||||
if (typeof block !== 'function') {
|
||||
throw new ERR_INVALID_ARG_TYPE('block', 'Function', block);
|
||||
}
|
||||
try {
|
||||
await block();
|
||||
} catch (e) {
|
||||
return e;
|
||||
}
|
||||
return NO_EXCEPTION_SENTINEL;
|
||||
}
|
||||
|
||||
function expectsError(stackStartFn, actual, error, message) {
|
||||
if (typeof error === 'string') {
|
||||
if (arguments.length === 3)
|
||||
if (arguments.length === 4) {
|
||||
throw new ERR_INVALID_ARG_TYPE('error', ['Function', 'RegExp'], error);
|
||||
|
||||
}
|
||||
message = error;
|
||||
error = null;
|
||||
}
|
||||
|
@ -443,21 +452,21 @@ assert.throws = function throws(block, error, message) {
|
|||
details += ` (${error.name})`;
|
||||
}
|
||||
details += message ? `: ${message}` : '.';
|
||||
const fnType = stackStartFn === rejects ? 'rejection' : 'exception';
|
||||
innerFail({
|
||||
actual,
|
||||
expected: error,
|
||||
operator: 'throws',
|
||||
message: `Missing expected exception${details}`,
|
||||
stackStartFn: throws
|
||||
operator: stackStartFn.name,
|
||||
message: `Missing expected ${fnType}${details}`,
|
||||
stackStartFn
|
||||
});
|
||||
}
|
||||
if (error && expectedException(actual, error, message) === false) {
|
||||
throw actual;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
assert.doesNotThrow = function doesNotThrow(block, error, message) {
|
||||
const actual = getActual(block);
|
||||
function expectsNoError(stackStartFn, actual, error, message) {
|
||||
if (actual === NO_EXCEPTION_SENTINEL)
|
||||
return;
|
||||
|
||||
|
@ -468,16 +477,41 @@ assert.doesNotThrow = function doesNotThrow(block, error, message) {
|
|||
|
||||
if (!error || expectedException(actual, error)) {
|
||||
const details = message ? `: ${message}` : '.';
|
||||
const fnType = stackStartFn === doesNotReject ? 'rejection' : 'exception';
|
||||
innerFail({
|
||||
actual,
|
||||
expected: error,
|
||||
operator: 'doesNotThrow',
|
||||
message: `Got unwanted exception${details}\n${actual && actual.message}`,
|
||||
stackStartFn: doesNotThrow
|
||||
operator: stackStartFn.name,
|
||||
message: `Got unwanted ${fnType}${details}\n${actual && actual.message}`,
|
||||
stackStartFn
|
||||
});
|
||||
}
|
||||
throw actual;
|
||||
};
|
||||
}
|
||||
|
||||
function throws(block, ...args) {
|
||||
expectsError(throws, getActual(block), ...args);
|
||||
}
|
||||
|
||||
assert.throws = throws;
|
||||
|
||||
async function rejects(block, ...args) {
|
||||
expectsError(rejects, await waitForActual(block), ...args);
|
||||
}
|
||||
|
||||
assert.rejects = rejects;
|
||||
|
||||
function doesNotThrow(block, ...args) {
|
||||
expectsNoError(doesNotThrow, getActual(block), ...args);
|
||||
}
|
||||
|
||||
assert.doesNotThrow = doesNotThrow;
|
||||
|
||||
async function doesNotReject(block, ...args) {
|
||||
expectsNoError(doesNotReject, await waitForActual(block), ...args);
|
||||
}
|
||||
|
||||
assert.doesNotReject = doesNotReject;
|
||||
|
||||
assert.ifError = function ifError(err) {
|
||||
if (err !== null && err !== undefined) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue