mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 21:58:48 +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
66
test/parallel/test-assert-async.js
Normal file
66
test/parallel/test-assert-async.js
Normal file
|
@ -0,0 +1,66 @@
|
|||
'use strict';
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const { promisify } = require('util');
|
||||
const wait = promisify(setTimeout);
|
||||
|
||||
/* eslint-disable prefer-common-expectserror, no-restricted-properties */
|
||||
|
||||
// Test assert.rejects() and assert.doesNotReject() by checking their
|
||||
// expected output and by verifying that they do not work sync
|
||||
|
||||
assert.rejects(
|
||||
() => assert.fail(),
|
||||
common.expectsError({
|
||||
code: 'ERR_ASSERTION',
|
||||
type: assert.AssertionError,
|
||||
message: 'Failed'
|
||||
})
|
||||
);
|
||||
|
||||
assert.doesNotReject(() => {});
|
||||
|
||||
{
|
||||
const promise = assert.rejects(async () => {
|
||||
await wait(1);
|
||||
assert.fail();
|
||||
}, common.expectsError({
|
||||
code: 'ERR_ASSERTION',
|
||||
type: assert.AssertionError,
|
||||
message: 'Failed'
|
||||
}));
|
||||
assert.doesNotReject(() => promise);
|
||||
}
|
||||
|
||||
{
|
||||
const promise = assert.doesNotReject(async () => {
|
||||
await wait(1);
|
||||
throw new Error();
|
||||
});
|
||||
assert.rejects(() => promise,
|
||||
(err) => {
|
||||
assert(err instanceof assert.AssertionError,
|
||||
`${err.name} is not instance of AssertionError`);
|
||||
assert.strictEqual(err.code, 'ERR_ASSERTION');
|
||||
assert(/^Got unwanted rejection\.\n$/.test(err.message));
|
||||
assert.strictEqual(err.operator, 'doesNotReject');
|
||||
assert.ok(!err.stack.includes('at Function.doesNotReject'));
|
||||
return true;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
const promise = assert.rejects(() => {});
|
||||
assert.rejects(() => promise,
|
||||
(err) => {
|
||||
assert(err instanceof assert.AssertionError,
|
||||
`${err.name} is not instance of AssertionError`);
|
||||
assert.strictEqual(err.code, 'ERR_ASSERTION');
|
||||
assert(/^Missing expected rejection\.$/.test(err.message));
|
||||
assert.strictEqual(err.operator, 'rejects');
|
||||
assert.ok(!err.stack.includes('at Function.rejects'));
|
||||
return true;
|
||||
}
|
||||
);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue