Skip to content

Commit dc861a8

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 a1bbb0f commit dc861a8

2 files changed

Lines changed: 10 additions & 20 deletions

File tree

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,
@@ -959,29 +959,15 @@ class Test extends AsyncResource {
959959
if (typeof this.expectFailure === 'object' &&
960960
this.expectFailure.with !== undefined) {
961961
const { with: validation } = this.expectFailure;
962-
let match = false;
963-
964-
if (isRegExp(validation)) {
965-
match = RegExpPrototypeExec(validation, err.message) !== null;
966-
} else if (typeof validation === 'object' && validation !== null) {
967-
match = true;
968-
for (const prop in validation) {
969-
if (!isDeepStrictEqual(err[prop], validation[prop])) {
970-
match = false;
971-
break;
972-
}
973-
}
974-
} else if (validation === err) {
975-
match = true;
976-
}
977-
978-
if (!match) {
962+
try {
963+
assert.throws(() => { throw err; }, validation);
964+
} catch (e) {
979965
this.passed = false;
980966
this.error = new ERR_TEST_FAILURE(
981967
'The test failed, but the error did not match the expected validation',
982968
kTestCodeFailure,
983969
);
984-
this.error.cause = err;
970+
this.error.cause = e;
985971
return;
986972
}
987973
}

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)