◐ Shell
clean mode source ↗

test_runner: expose expectFailure message · nodejs/node@fce2930

@@ -265,11 +265,12 @@ added:

265265

- v25.5.0

266266

-->

267267268-

This flips the pass/fail reporting for a specific test or suite: A flagged test/test-case must throw

269-

in order to "pass"; a test/test-case that does not throw, fails.

268+

This flips the pass/fail reporting for a specific test or suite: a flagged test

269+

case must throw in order to pass, and a flagged test case that does not throw

270+

fails.

270271271-

In the following, `doTheThing()` returns _currently_ `false` (`false` does not equal `true`, causing

272-

`strictEqual` to throw, so the test-case passes).

272+

In each of the following, `doTheThing()` fails to return `true`, but since the

273+

tests are flagged `expectFailure`, they pass.

273274274275

```js

275276

it.expectFailure('should do the thing', () => {

@@ -279,6 +280,50 @@ it.expectFailure('should do the thing', () => {

279280

it('should do the thing', { expectFailure: true }, () => {

280281

assert.strictEqual(doTheThing(), true);

281282

});

283+284+

it('should do the thing', { expectFailure: 'feature not implemented' }, () => {

285+

assert.strictEqual(doTheThing(), true);

286+

});

287+

```

288+289+

If the value of `expectFailure` is a

290+

[<RegExp>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) |

291+

[<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) |

292+

[<Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) |

293+

[<Error>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error),

294+

the tests will pass only if they throw a matching value.

295+

See [`assert.throws`][] for how each value type is handled.

296+297+

Each of the following tests fails _despite_ being flagged `expectFailure`

298+

because the failure does not match the specific **expected** failure.

299+300+

```js

301+

it('fails because regex does not match', {

302+

expectFailure: /expected message/,

303+

}, () => {

304+

throw new Error('different message');

305+

});

306+307+

it('fails because object matcher does not match', {

308+

expectFailure: { code: 'ERR_EXPECTED' },

309+

}, () => {

310+

const err = new Error('boom');

311+

err.code = 'ERR_ACTUAL';

312+

throw err;

313+

});

314+

```

315+316+

To supply both a reason and specific error for `expectFailure`, use `{ label, match }`.

317+318+

```js

319+

it('should fail with specific error and reason', {

320+

expectFailure: {

321+

label: 'reason for failure',

322+

match: /error message/,

323+

},

324+

}, () => {

325+

assert.strictEqual(doTheThing(), true);

326+

});

282327

```

283328284329

`skip` and/or `todo` are mutually exclusive to `expectFailure`, and `skip` or `todo`

@@ -1684,6 +1729,18 @@ changes:

16841729

thread. If `false`, only one test runs at a time.

16851730

If unspecified, subtests inherit this value from their parent.

16861731

**Default:** `false`.

1732+

* `expectFailure` {boolean|string|RegExp|Function|Object|Error} If truthy, the

1733+

test is expected to fail. If a non-empty string is provided, that string is displayed

1734+

in the test results as the reason why the test is expected to fail. If a

1735+

[<RegExp>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp),

1736+

[<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function),

1737+

[<Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object), or

1738+

[<Error>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)

1739+

is provided directly (without wrapping in `{ match: … }`), the test passes

1740+

only if the thrown error matches, following the behavior of

1741+

[`assert.throws`][]. To provide both a reason and validation, pass an object

1742+

with `label` (string) and `match` (RegExp, Function, Object, or Error).

1743+

**Default:** `false`.

16871744

* `only` {boolean} If truthy, and the test context is configured to run

16881745

`only` tests, then this test will be run. Otherwise, the test is skipped.

16891746

**Default:** `false`.

@@ -4181,6 +4238,7 @@ Can be used to abort test subtasks when the test has been aborted.

41814238

[`NODE_V8_COVERAGE`]: cli.md#node_v8_coveragedir

41824239

[`SuiteContext`]: #class-suitecontext

41834240

[`TestContext`]: #class-testcontext

4241+

[`assert.throws`]: assert.md#assertthrowsfn-error-message

41844242

[`context.diagnostic`]: #contextdiagnosticmessage

41854243

[`context.skip`]: #contextskipmessage

41864244

[`context.todo`]: #contexttodomessage