Skip to content

Commit 67a4a61

Browse files
committed
test_runner: enhance expectFailure with validation support
Enhance `expectFailure` option to accep - Add `message` property for custom TAP directives. - Add `with` property for error validation using `assert.throws`. Tests added in `test/parallel/test-runner-xfail.js`.
1 parent cd8b5aa commit 67a4a61

File tree

2 files changed

+10
-20
lines changed

2 files changed

+10
-20
lines changed

lib/internal/test_runner/test.js

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ const {
5656
once: runOnce,
5757
setOwnProperty,
5858
} = require('internal/util');
59-
const { isDeepStrictEqual } = require('internal/util/comparisons');
60-
const { isPromise, isRegExp } = require('internal/util/types');
59+
const assert = require('assert');
60+
const { isPromise } = require('internal/util/types');
6161
const {
6262
validateAbortSignal,
6363
validateFunction,
@@ -958,29 +958,15 @@ class Test extends AsyncResource {
958958
if (typeof this.expectFailure === 'object' &&
959959
this.expectFailure.with !== undefined) {
960960
const { with: validation } = this.expectFailure;
961-
let match = false;
962-
963-
if (isRegExp(validation)) {
964-
match = RegExpPrototypeExec(validation, err.message) !== null;
965-
} else if (typeof validation === 'object' && validation !== null) {
966-
match = true;
967-
for (const prop in validation) {
968-
if (!isDeepStrictEqual(err[prop], validation[prop])) {
969-
match = false;
970-
break;
971-
}
972-
}
973-
} else if (validation === err) {
974-
match = true;
975-
}
976-
977-
if (!match) {
961+
try {
962+
assert.throws(() => { throw err; }, validation);
963+
} catch (e) {
978964
this.passed = false;
979965
this.error = new ERR_TEST_FAILURE(
980966
'The test failed, but the error did not match the expected validation',
981967
kTestCodeFailure,
982968
);
983-
this.error.cause = err;
969+
this.error.cause = e;
984970
return;
985971
}
986972
}

test/parallel/test-runner-xfail.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ if (process.env.CHILD_PROCESS === 'true') {
2121
assert.fail('boom');
2222
});
2323

24+
test('fail with validation class', { expectFailure: { with: assert.AssertionError } }, () => {
25+
assert.fail('boom');
26+
});
27+
2428
test('fail with validation error (wrong error)', { expectFailure: { with: /bang/ } }, () => {
2529
assert.fail('boom'); // Should result in real failure because error doesn't match
2630
});

0 commit comments

Comments
 (0)