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

it('should do the thing', { expectFailure: 'doTheThing is not doing the thing because ...' }, () => {
assert.strictEqual(doTheThing(), true);
});
```

`skip` and/or `todo` are mutually exclusive to `expectFailure`, and `skip` or `todo`
Expand Down Expand Up @@ -1677,6 +1681,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
16 changes: 9 additions & 7 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,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') {
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,11 +953,7 @@ class Test extends AsyncResource {
return;
}

if (this.expectFailure === true) {
this.passed = true;
} else {
this.passed = false;
}
this.passed = this.expectFailure;

this.error = err;
}
Expand Down Expand Up @@ -1350,7 +1352,7 @@ 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
directive = this.reporter.getXFail(this.expectFailure);
}

if (this.reportedType) {
Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-runner-xfail-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'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', { expectFailure: 'doTheThing is not doing the thing because ...' }, () => {
assert.fail('boom');
});
} 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) => {
assert.strictEqual(code, 0);
assert.match(stdout, /# EXPECTED FAILURE doTheThing is not doing the thing because .../);

Check failure on line 23 in test/parallel/test-runner-xfail-message.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Unescaped dot character in regular expression

Check failure on line 23 in test/parallel/test-runner-xfail-message.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Unescaped dot character in regular expression

Check failure on line 23 in test/parallel/test-runner-xfail-message.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Unescaped dot character in regular expression
}));
}
Loading