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
16 changes: 16 additions & 0 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,19 @@ it.expectFailure('should do the thing', () => {
it('should do the thing', { expectFailure: true }, () => {
assert.strictEqual(doTheThing(), true);
});

it('should do the thing', { expectFailure: 'feature not implemented' }, () => {
assert.strictEqual(doTheThing(), true);
});

it('should fail with specific error', {
expectFailure: {
with: /error message/,
message: 'reason for failure',
},
Comment on lines +254 to +257
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like these field names the most compared to all the other ideas 👍🏾. Put it in the proposal?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(i only just learned that pressing the comment button is not enough. you have to then submit the comment. this is a really bad GitHub UX)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following are equivalent, yes?

expectFailure: 'reason for failure'
expectFailure: {message: 'reason for failure'}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the proposal based on the feedback:

  • Added support for Direct Matchers (e.g., expectFailure: /error/).
  • Clarified that with is for validation and message is for reasoning.
  • Noted that expectFailure: 'reason' and { message: 'reason' } are equivalent.

Copy link
Contributor

@vassudanagunta vassudanagunta Jan 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added support for Direct Matchers (e.g., expectFailure: /error/).

This means that the following are also equivalent, yes?

expectFailure: /error/
expectFailure: {with: /error/}

and likewise any other value type accepted by assert.throws, yes?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JakobJingleheimer and the test runner team as a whole: the discussion of this feature is now occurring in two places... far from ideal and likely to happen again and again because the natural place to have such discussions is on the Issues and PRs made in the nodejs repo.

I was gonna say corralling all discussion to a separate repo will be like corralling cats... but maybe the unconventional split of test runner code and test runner discussion is the cat?

I think it's worth the test runner team addressing this.

Copy link
Member

@JakobJingleheimer JakobJingleheimer Jan 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should all happen in the proposal (I believe I suggested closing this PR in the interim; it can always be re-opened).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've included it in the proposal. Could you please review it?

nodejs/test-runner#10

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I shall tomorrow when I'm back from holiday.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll close this for now as requested until the proposal is passed. I'll reopen it once we are ready

}, () => {
assert.strictEqual(doTheThing(), true);
});
```

`skip` and/or `todo` are mutually exclusive to `expectFailure`, and `skip` or `todo`
Expand Down Expand Up @@ -1677,6 +1690,9 @@ changes:
thread. If `false`, only one test runs at a time.
If unspecified, subtests inherit this value from their parent.
**Default:** `false`.
* `expectFailure` {boolean|string} If truthy, the test is expected to fail.
If a string is provided, that string is displayed in the test results as the
reason why the test is expected to fail. **Default:** `false`.
* `only` {boolean} If truthy, and the test context is configured to run
`only` tests, then this test will be run. Otherwise, the test is skipped.
**Default:** `false`.
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/test_runner/reporter/tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function reportTest(nesting, testNumber, status, name, skip, todo, expectFailure
} else if (todo !== undefined) {
line += ` # TODO${typeof todo === 'string' && todo.length ? ` ${tapEscape(todo)}` : ''}`;
} else if (expectFailure !== undefined) {
line += ' # EXPECTED FAILURE';
line += ` # EXPECTED FAILURE${typeof expectFailure === 'string' && expectFailure.length ? ` ${tapEscape(expectFailure)}` : ''}`;
}

line += '\n';
Expand Down
46 changes: 43 additions & 3 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const {
once: runOnce,
setOwnProperty,
} = require('internal/util');
const assert = require('assert');
const { isPromise } = require('internal/util/types');
const {
validateAbortSignal,
Expand Down Expand Up @@ -635,7 +636,13 @@ class Test extends AsyncResource {
this.plan = null;
this.expectedAssertions = plan;
this.cancelled = false;
this.expectFailure = expectFailure !== undefined && expectFailure !== false;
if (expectFailure === undefined || expectFailure === false) {
this.expectFailure = false;
} else if (typeof expectFailure === 'string' || typeof expectFailure === 'object') {
this.expectFailure = expectFailure;
} else {
this.expectFailure = true;
}
this.skipped = skip !== undefined && skip !== false;
this.isTodo = (todo !== undefined && todo !== false) || this.parent?.isTodo;
this.startTime = null;
Expand Down Expand Up @@ -947,7 +954,23 @@ class Test extends AsyncResource {
return;
}

if (this.expectFailure === true) {
if (this.expectFailure) {
if (typeof this.expectFailure === 'object' &&
this.expectFailure.with !== undefined) {
const { with: validation } = this.expectFailure;
try {
const { throws } = assert;
throws(() => { throw err; }, validation);
} catch (e) {
this.passed = false;
this.error = new ERR_TEST_FAILURE(
'The test failed, but the error did not match the expected validation',
kTestCodeFailure,
);
this.error.cause = e;
return;
}
}
this.passed = true;
} else {
this.passed = false;
Expand All @@ -961,6 +984,20 @@ class Test extends AsyncResource {
return;
}

if (this.skipped || this.isTodo) {
this.passed = true;
return;
}

if (this.expectFailure) {
this.passed = false;
this.error = new ERR_TEST_FAILURE(
'Test passed but was expected to fail',
kTestCodeFailure,
);
return;
}

this.passed = true;
}

Expand Down Expand Up @@ -1350,7 +1387,10 @@ class Test extends AsyncResource {
} else if (this.isTodo) {
directive = this.reporter.getTodo(this.message);
} else if (this.expectFailure) {
directive = this.reporter.getXFail(this.expectFailure); // TODO(@JakobJingleheimer): support specifying failure
const message = typeof this.expectFailure === 'object' ?
this.expectFailure.message :
this.expectFailure;
directive = this.reporter.getXFail(message);
}

if (this.reportedType) {
Expand Down
56 changes: 56 additions & 0 deletions test/parallel/test-runner-xfail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';
const common = require('../common');
const { test } = require('node:test');
const { spawn } = require('child_process');
const assert = require('node:assert');

if (process.env.CHILD_PROCESS === 'true') {
test('fail with message string', { expectFailure: 'reason string' }, () => {
assert.fail('boom');
});

test('fail with message object', { expectFailure: { message: 'reason object' } }, () => {
assert.fail('boom');
});

test('fail with validation regex', { expectFailure: { with: /boom/ } }, () => {
assert.fail('boom');
});

test('fail with validation object', { expectFailure: { with: { message: 'boom' } } }, () => {
assert.fail('boom');
});

test('fail with validation class', { expectFailure: { with: assert.AssertionError } }, () => {
assert.fail('boom');
});

test('fail with validation error (wrong error)', { expectFailure: { with: /bang/ } }, () => {
assert.fail('boom'); // Should result in real failure because error doesn't match
});

test('unexpected pass', { expectFailure: true }, () => {
// Should result in real failure because it didn't fail
});

} else {
const child = spawn(process.execPath, ['--test-reporter', 'tap', __filename], {
env: { ...process.env, CHILD_PROCESS: 'true' },
stdio: 'pipe',
});

let stdout = '';
child.stdout.setEncoding('utf8');
child.stdout.on('data', (chunk) => { stdout += chunk; });

child.on('close', common.mustCall((code) => {
// We expect exit code 1 because 'unexpected pass' and 'wrong error' should fail the test run
assert.strictEqual(code, 1);

// Check outputs
assert.match(stdout, /# EXPECTED FAILURE reason string/);
assert.match(stdout, /# EXPECTED FAILURE reason object/);
assert.match(stdout, /not ok \d+ - fail with validation error \(wrong error\)/);
assert.match(stdout, /not ok \d+ - unexpected pass/);
}));
}
Loading