test_runner: support expecting a test-case to fail

Co-Authored-By: Alejandro Espa <98526766+vespa7@users.noreply.github.com>
PR-URL: https://github.com/nodejs/node/pull/60669
Reviewed-By: Aviv Keller <me@aviv.sh>
Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Jordan Harband <ljharb@gmail.com>
This commit is contained in:
Jacob SmithandAlejandro Espa authored and Antoine du Hamel committed 2026-01-20 14:35:58 +01:00
1 parent 3f17acfb1c
commit ff1fcabfc9
22 files changed
+806 -493

No files matched your search

+51
View File
@@ -224,6 +224,57 @@ test('todo() method with message', (t) => {
});
```
## Expecting tests to fail
<!-- YAML
added:
- REPLACEME
-->
This flips the pass/fail reporting for a specific test or suite: A flagged test/test-case must throw
in order to "pass"; a test/test-case that does not throw, fails.
In the following, `doTheThing()` returns _currently_ `false` (`false` does not equal `true`, causing
`strictEqual` to throw, so the test-case passes).
```js
it.expectFailure('should do the thing', () => {
assert.strictEqual(doTheThing(), true);
});
it('should do the thing', { expectFailure: true }, () => {
assert.strictEqual(doTheThing(), true);
});
```
`skip` and/or `todo` are mutually exclusive to `expectFailure`, and `skip` or `todo`
will "win" when both are applied (`skip` wins against both, and `todo` wins
against `expectFailure`).
These tests will be skipped (and not run):
```js
it.expectFailure('should do the thing', { skip: true }, () => {
assert.strictEqual(doTheThing(), true);
});
it.skip('should do the thing', { expectFailure: true }, () => {
assert.strictEqual(doTheThing(), true);
});
```
These tests will be marked "todo" (silencing errors):
```js
it.expectFailure('should do the thing', { todo: true }, () => {
assert.strictEqual(doTheThing(), true);
});
it.todo('should do the thing', { expectFailure: true }, () => {
assert.strictEqual(doTheThing(), true);
});
```
## `describe()` and `it()` aliases
Suites and tests can also be written using the `describe()` and `it()`