assert.rejects(asyncFn[, error][, message])
asyncFn
<Function> | <Promise>error
<RegExp> | <Function> | <Object> | <Error>message
<string>
等待 asyncFn
promise,或者,如果 asyncFn
是函数,则立即调用该函数并等待返回的 promise 完成。
然后将检查 promise 是否被拒绝。
如果 asyncFn
是函数并且它同步抛出错误,则 assert.rejects()
将返回使用使用该错误拒绝的 Promise
。
如果函数没有返回 promise,则 assert.rejects()
将返回使用 ERR_INVALID_RETURN_VALUE
错误拒绝的 Promise
。
在这两种情况下,都会跳过错误句柄。
除了等待完成的异步性质外,其行为与 assert.throws()
相同。
如果指定,则 error
可以是 Class
、RegExp
、验证函数、每个属性将被测试的对象,或者每个属性(包括不可枚举的 message
和 name
属性)将被测试的错误实例。
如果指定,则 message
将是 AssertionError
提供的消息(如果 asyncFn
没有被拒绝)。
import assert from 'node:assert/strict';
await assert.rejects(
async () => {
throw new TypeError('Wrong value');
},
{
name: 'TypeError',
message: 'Wrong value'
}
);
const assert = require('node:assert/strict');
(async () => {
await assert.rejects(
async () => {
throw new TypeError('Wrong value');
},
{
name: 'TypeError',
message: 'Wrong value'
}
);
})();
import assert from 'node:assert/strict';
await assert.rejects(
async () => {
throw new TypeError('Wrong value');
},
(err) => {
assert.strictEqual(err.name, 'TypeError');
assert.strictEqual(err.message, 'Wrong value');
return true;
}
);
const assert = require('node:assert/strict');
(async () => {
await assert.rejects(
async () => {
throw new TypeError('Wrong value');
},
(err) => {
assert.strictEqual(err.name, 'TypeError');
assert.strictEqual(err.message, 'Wrong value');
return true;
}
);
})();
import assert from 'node:assert/strict';
assert.rejects(
Promise.reject(new Error('Wrong value')),
Error
).then(() => {
// ...
});
const assert = require('node:assert/strict');
assert.rejects(
Promise.reject(new Error('Wrong value')),
Error
).then(() => {
// ...
});
error
不能是字符串。
如果提供字符串作为第二个参数,则假定 error
被省略,而该字符串将用于 message
。
这可能会导致容易遗漏的错误。
如果考虑使用字符串作为第二个参数,则请仔细阅读 assert.throws()
中的示例。
asyncFn
<Function> | <Promise>error
<RegExp> | <Function> | <Object> | <Error>message
<string>
Awaits the asyncFn
promise or, if asyncFn
is a function, immediately
calls the function and awaits the returned promise to complete. It will then
check that the promise is rejected.
If asyncFn
is a function and it throws an error synchronously,
assert.rejects()
will return a rejected Promise
with that error. If the
function does not return a promise, assert.rejects()
will return a rejected
Promise
with an ERR_INVALID_RETURN_VALUE
error. In both cases the error
handler is skipped.
Besides the async nature to await the completion behaves identically to
assert.throws()
.
If specified, error
can be a Class
, RegExp
, a validation function,
an object where each property will be tested for, or an instance of error where
each property will be tested for including the non-enumerable message
and
name
properties.
If specified, message
will be the message provided by the AssertionError
if the asyncFn
fails to reject.
import assert from 'node:assert/strict';
await assert.rejects(
async () => {
throw new TypeError('Wrong value');
},
{
name: 'TypeError',
message: 'Wrong value'
}
);
const assert = require('node:assert/strict');
(async () => {
await assert.rejects(
async () => {
throw new TypeError('Wrong value');
},
{
name: 'TypeError',
message: 'Wrong value'
}
);
})();
import assert from 'node:assert/strict';
await assert.rejects(
async () => {
throw new TypeError('Wrong value');
},
(err) => {
assert.strictEqual(err.name, 'TypeError');
assert.strictEqual(err.message, 'Wrong value');
return true;
}
);
const assert = require('node:assert/strict');
(async () => {
await assert.rejects(
async () => {
throw new TypeError('Wrong value');
},
(err) => {
assert.strictEqual(err.name, 'TypeError');
assert.strictEqual(err.message, 'Wrong value');
return true;
}
);
})();
import assert from 'node:assert/strict';
assert.rejects(
Promise.reject(new Error('Wrong value')),
Error
).then(() => {
// ...
});
const assert = require('node:assert/strict');
assert.rejects(
Promise.reject(new Error('Wrong value')),
Error
).then(() => {
// ...
});
error
cannot be a string. If a string is provided as the second
argument, then error
is assumed to be omitted and the string will be used for
message
instead. This can lead to easy-to-miss mistakes. Please read the
example in assert.throws()
carefully if using a string as the second
argument gets considered.