From 8bd4c3036aba5f1ebaa2420ffd27ad9fbc9138a0 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Wed, 13 Mar 2024 19:19:32 -0400 Subject: [PATCH] test_runner: support test plans Co-Authored-By: Colin Ihrig --- doc/api/test.md | 33 +++++++- lib/internal/test_runner/runner.js | 3 +- lib/internal/test_runner/test.js | 78 ++++++++++++++++- .../test-runner/output/test-runner-plan.js | 49 +++++++++++ .../output/test-runner-plan.snapshot | 84 +++++++++++++++++++ test/parallel/test-runner-output.mjs | 1 + 6 files changed, 242 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/test-runner/output/test-runner-plan.js create mode 100644 test/fixtures/test-runner/output/test-runner-plan.snapshot diff --git a/doc/api/test.md b/doc/api/test.md index d86c2cfc0bfb13..f7ce098125e4e2 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -1364,6 +1364,10 @@ changes: * `timeout` {number} A number of milliseconds the test will fail after. If unspecified, subtests inherit this value from their parent. **Default:** `Infinity`. + * `plan` {number} The number of assertions expected to be run in the test. + If the number of assertions run in the test does not match the number + specified in the plan, the test will fail. + **Default:** `undefined`. * `fn` {Function|AsyncFunction} The function under test. The first argument to this function is a [`TestContext`][] object. If the test uses callbacks, the callback function is passed as the second argument. **Default:** A no-op @@ -2965,6 +2969,29 @@ added: The name of the test. +### `context.plan(count)` + + + +* `count` {number} The number of assertions that are expected to run. + +This function is used to set the number of assertions that are expected to run +within the test. If the number of assertions that run does not match the +expected count, the test will fail. + +> Note: To make sure assertion are tracked, it must be used `t.assert` instead of `assert` directly. + +```js +test('top level test', (t) => { + t.plan(2); + t.assert.ok('some relevant assertion here'); + t.assert.ok('another relevant assertion here'); +}); +``` + ### `context.runOnly(shouldRunOnlyTests)`