Skip to content

Commit 1e5c7e3

Browse files
BridgeARMylesBorins
authored andcommitted
test: refactor common.expectsError
A combination of try catch and common.expectsError is not necessary as the latter already does everything on its own. Backport-PR-URL: #19230 PR-URL: #17703 Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 24aeca7 commit 1e5c7e3

File tree

1 file changed

+18
-36
lines changed

1 file changed

+18
-36
lines changed

test/parallel/test-assert.js

+18-36
Original file line numberDiff line numberDiff line change
@@ -476,33 +476,21 @@ common.expectsError(
476476
}
477477
);
478478

479-
{
480-
let threw = false;
481-
try {
482-
assert.doesNotThrow(makeBlock(thrower, Error), 'user message');
483-
} catch (e) {
484-
threw = true;
485-
common.expectsError({
486-
code: 'ERR_ASSERTION',
487-
message: /Got unwanted exception: user message\n\[object Object\]/
488-
})(e);
479+
common.expectsError(
480+
() => assert.doesNotThrow(makeBlock(thrower, Error), 'user message'),
481+
{
482+
code: 'ERR_ASSERTION',
483+
message: /Got unwanted exception: user message\n\[object Object\]/
489484
}
490-
assert.ok(threw);
491-
}
485+
);
492486

493-
{
494-
let threw = false;
495-
try {
496-
assert.doesNotThrow(makeBlock(thrower, Error));
497-
} catch (e) {
498-
threw = true;
499-
common.expectsError({
500-
code: 'ERR_ASSERTION',
501-
message: /Got unwanted exception\.\n\[object Object\]/
502-
})(e);
487+
common.expectsError(
488+
() => assert.doesNotThrow(makeBlock(thrower, Error)),
489+
{
490+
code: 'ERR_ASSERTION',
491+
message: /Got unwanted exception\.\n\[object Object\]/
503492
}
504-
assert.ok(threw);
505-
}
493+
);
506494

507495
// make sure that validating using constructor really works
508496
{
@@ -691,21 +679,15 @@ try {
691679
}
692680

693681
const testBlockTypeError = (method, block) => {
694-
let threw = true;
695-
696-
try {
697-
method(block);
698-
threw = false;
699-
} catch (e) {
700-
common.expectsError({
682+
common.expectsError(
683+
() => method(block),
684+
{
701685
code: 'ERR_INVALID_ARG_TYPE',
702686
type: TypeError,
703687
message: 'The "block" argument must be of type Function. Received ' +
704-
`type ${typeName(block)}`
705-
})(e);
706-
}
707-
708-
assert.ok(threw);
688+
`type ${typeName(block)}`
689+
}
690+
);
709691
};
710692

711693
testBlockTypeError(assert.throws, 'string');

0 commit comments

Comments
 (0)