Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,21 +243,21 @@ function expectedException(actual, expected, msg) {
return expected.call({}, actual) === true;
}

function getActual(block) {
async function getActual(block) {
if (typeof block !== 'function') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'block', 'Function',
block);
}
try {
block();
await block();
} catch (e) {
return e;
}
}

// Expected to throw an error.
assert.throws = function throws(block, error, message) {
const actual = getActual(block);
assert.throws = async function throws(block, error, message) {
const actual = await getActual(block);

if (typeof error === 'string') {
if (arguments.length === 3)
Expand Down Expand Up @@ -289,8 +289,8 @@ assert.throws = function throws(block, error, message) {
}
};

assert.doesNotThrow = function doesNotThrow(block, error, message) {
const actual = getActual(block);
assert.doesNotThrow = async function doesNotThrow(block, error, message) {
const actual = await getActual(block);
if (actual === undefined)
return;

Expand Down
42 changes: 42 additions & 0 deletions test/parallel/test-assert-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const promisify = require('util').promisify;
const wait = promisify(setTimeout);

// Ensure async support for assert.throws() and assert.doesNotThrow()
/* eslint-disable prefer-common-expectserror */

assert.throws(
async () => { assert.fail(); },
common.expectsError({
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: 'Failed',
operator: undefined,
actual: undefined,
expected: undefined
})
);

assert.throws(
async () => {
await wait(common.platformTimeout(10));
assert.fail();
},
common.expectsError({
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: 'Failed',
operator: undefined,
actual: undefined,
expected: undefined
})
);

assert.doesNotThrow(async () => {});

assert.doesNotThrow(async () => {
await wait(common.platformTimeout(10));
assert.fail();
});