Skip to content

Commit 3cd4462

Browse files
not-an-aardvarkMylesBorins
authored andcommitted
test: ensure failed assertions cause build to fail
This updates the test in `test/parallel/test-assert-async.js` to add an assertion that the Promises used in the test end up fulfilled. Previously, if an assertion failure occurred, the Promises would have rejected and a warning would have been logged, but the test would still have exit code 0. Backport-PR-URL: #24019 PR-URL: #19650 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 3babc5b commit 3cd4462

File tree

1 file changed

+45
-51
lines changed

1 file changed

+45
-51
lines changed

test/parallel/test-assert-async.js

+45-51
Original file line numberDiff line numberDiff line change
@@ -9,58 +9,52 @@ const wait = promisify(setTimeout);
99
// Test assert.rejects() and assert.doesNotReject() by checking their
1010
// expected output and by verifying that they do not work sync
1111

12-
assert.rejects(
13-
() => assert.fail(),
14-
common.expectsError({
15-
code: 'ERR_ASSERTION',
16-
type: assert.AssertionError,
17-
message: 'Failed'
18-
})
19-
);
12+
common.crashOnUnhandledRejection();
2013

21-
assert.doesNotReject(() => {});
14+
(async () => {
15+
await assert.rejects(
16+
() => assert.fail(),
17+
common.expectsError({
18+
code: 'ERR_ASSERTION',
19+
type: assert.AssertionError,
20+
message: 'Failed'
21+
})
22+
);
2223

23-
{
24-
const promise = assert.rejects(async () => {
25-
await wait(1);
26-
assert.fail();
27-
}, common.expectsError({
28-
code: 'ERR_ASSERTION',
29-
type: assert.AssertionError,
30-
message: 'Failed'
31-
}));
32-
assert.doesNotReject(() => promise);
33-
}
24+
await assert.doesNotReject(() => {});
3425

35-
{
36-
const promise = assert.doesNotReject(async () => {
37-
await wait(1);
38-
throw new Error();
39-
});
40-
assert.rejects(() => promise,
41-
(err) => {
42-
assert(err instanceof assert.AssertionError,
43-
`${err.name} is not instance of AssertionError`);
44-
assert.strictEqual(err.code, 'ERR_ASSERTION');
45-
assert(/^Got unwanted rejection\.\n$/.test(err.message));
46-
assert.strictEqual(err.operator, 'doesNotReject');
47-
assert.ok(!err.stack.includes('at Function.doesNotReject'));
48-
return true;
49-
}
50-
);
51-
}
26+
{
27+
const promise = assert.doesNotReject(async () => {
28+
await wait(1);
29+
throw new Error();
30+
});
31+
await assert.rejects(
32+
() => promise,
33+
(err) => {
34+
assert(err instanceof assert.AssertionError,
35+
`${err.name} is not instance of AssertionError`);
36+
assert.strictEqual(err.code, 'ERR_ASSERTION');
37+
assert(/^Got unwanted rejection\.\n$/.test(err.message));
38+
assert.strictEqual(err.operator, 'doesNotReject');
39+
assert.ok(!err.stack.includes('at Function.doesNotReject'));
40+
return true;
41+
}
42+
);
43+
}
5244

53-
{
54-
const promise = assert.rejects(() => {});
55-
assert.rejects(() => promise,
56-
(err) => {
57-
assert(err instanceof assert.AssertionError,
58-
`${err.name} is not instance of AssertionError`);
59-
assert.strictEqual(err.code, 'ERR_ASSERTION');
60-
assert(/^Missing expected rejection\.$/.test(err.message));
61-
assert.strictEqual(err.operator, 'rejects');
62-
assert.ok(!err.stack.includes('at Function.rejects'));
63-
return true;
64-
}
65-
);
66-
}
45+
{
46+
const promise = assert.rejects(() => {});
47+
await assert.rejects(
48+
() => promise,
49+
(err) => {
50+
assert(err instanceof assert.AssertionError,
51+
`${err.name} is not instance of AssertionError`);
52+
assert.strictEqual(err.code, 'ERR_ASSERTION');
53+
assert(/^Missing expected rejection\.$/.test(err.message));
54+
assert.strictEqual(err.operator, 'rejects');
55+
assert.ok(!err.stack.includes('at Function.rejects'));
56+
return true;
57+
}
58+
);
59+
}
60+
})().then(common.mustCall());

0 commit comments

Comments
 (0)