Skip to content

Commit 5276c0d

Browse files
authored
test_runner: add suite()
This commit adds a suite() function to the test runner and makes describe() an alias for it. This matches the it() alias for test(). Fixes: #51430 PR-URL: #52127 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent 7071554 commit 5276c0d

File tree

4 files changed

+82
-42
lines changed

4 files changed

+82
-42
lines changed

doc/api/test.md

+69-37
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ test('top level test', async (t) => {
128128
> between each subtest execution.
129129
130130
In this example, `await` is used to ensure that both subtests have completed.
131-
This is necessary because parent tests do not wait for their subtests to
132-
complete, unlike tests created with the `describe` and `it` syntax.
131+
This is necessary because tests do not wait for their subtests to
132+
complete, unlike tests created within suites.
133133
Any subtests that are still outstanding when their parent finishes
134134
are cancelled and treated as failures. Any subtest failures cause the parent
135135
test to fail.
@@ -162,12 +162,11 @@ test('skip() method with message', (t) => {
162162
});
163163
```
164164

165-
## `describe`/`it` syntax
165+
## `describe()` and `it()` aliases
166166

167-
Running tests can also be done using `describe` to declare a suite
168-
and `it` to declare a test.
169-
A suite is used to organize and group related tests together.
170-
`it` is a shorthand for [`test()`][].
167+
Suites and tests can also be written using the `describe()` and `it()`
168+
functions. [`describe()`][] is an alias for [`suite()`][], and [`it()`][] is an
169+
alias for [`test()`][].
171170

172171
```js
173172
describe('A thing', () => {
@@ -187,7 +186,7 @@ describe('A thing', () => {
187186
});
188187
```
189188

190-
`describe` and `it` are imported from the `node:test` module.
189+
`describe()` and `it()` are imported from the `node:test` module.
191190

192191
```mjs
193192
import { describe, it } from 'node:test';
@@ -1204,6 +1203,51 @@ run({ files: [path.resolve('./tests/test.js')] })
12041203
.pipe(process.stdout);
12051204
```
12061205

1206+
## `suite([name][, options][, fn])`
1207+
1208+
<!-- YAML
1209+
added: REPLACEME
1210+
-->
1211+
1212+
* `name` {string} The name of the suite, which is displayed when reporting test
1213+
results. **Default:** The `name` property of `fn`, or `'<anonymous>'` if `fn`
1214+
does not have a name.
1215+
* `options` {Object} Optional configuration options for the suite.
1216+
This supports the same options as `test([name][, options][, fn])`.
1217+
* `fn` {Function|AsyncFunction} The suite function declaring nested tests and
1218+
suites. The first argument to this function is a [`SuiteContext`][] object.
1219+
**Default:** A no-op function.
1220+
* Returns: {Promise} Immediately fulfilled with `undefined`.
1221+
1222+
The `suite()` function is imported from the `node:test` module.
1223+
1224+
## `suite.skip([name][, options][, fn])`
1225+
1226+
<!-- YAML
1227+
added: REPLACEME
1228+
-->
1229+
1230+
Shorthand for skipping a suite. This is the same as
1231+
[`suite([name], { skip: true }[, fn])`][suite options].
1232+
1233+
## `suite.todo([name][, options][, fn])`
1234+
1235+
<!-- YAML
1236+
added: REPLACEME
1237+
-->
1238+
1239+
Shorthand for marking a suite as `TODO`. This is the same as
1240+
[`suite([name], { todo: true }[, fn])`][suite options].
1241+
1242+
## `suite.only([name][, options][, fn])`
1243+
1244+
<!-- YAML
1245+
added: REPLACEME
1246+
-->
1247+
1248+
Shorthand for marking a suite as `only`. This is the same as
1249+
[`suite([name], { only: true }[, fn])`][suite options].
1250+
12071251
## `test([name][, options][, fn])`
12081252

12091253
<!-- YAML
@@ -1257,7 +1301,7 @@ changes:
12571301
the callback function is passed as the second argument. **Default:** A no-op
12581302
function.
12591303
* Returns: {Promise} Fulfilled with `undefined` once
1260-
the test completes, or immediately if the test runs within [`describe()`][].
1304+
the test completes, or immediately if the test runs within a suite.
12611305

12621306
The `test()` function is the value imported from the `test` module. Each
12631307
invocation of this function results in reporting the test to the {TestsStream}.
@@ -1267,7 +1311,7 @@ actions related to the current test. Examples include skipping the test, adding
12671311
additional diagnostic information, or creating subtests.
12681312

12691313
`test()` returns a `Promise` that fulfills once the test completes.
1270-
if `test()` is called within a `describe()` block, it fulfills immediately.
1314+
if `test()` is called within a suite, it fulfills immediately.
12711315
The return value can usually be discarded for top level tests.
12721316
However, the return value from subtests should be used to prevent the parent
12731317
test from finishing first and cancelling the subtest
@@ -1308,29 +1352,18 @@ same as [`test([name], { only: true }[, fn])`][it options].
13081352

13091353
## `describe([name][, options][, fn])`
13101354

1311-
* `name` {string} The name of the suite, which is displayed when reporting test
1312-
results. **Default:** The `name` property of `fn`, or `'<anonymous>'` if `fn`
1313-
does not have a name.
1314-
* `options` {Object} Configuration options for the suite.
1315-
supports the same options as `test([name][, options][, fn])`.
1316-
* `fn` {Function|AsyncFunction} The function under suite
1317-
declaring all subtests and subsuites.
1318-
The first argument to this function is a [`SuiteContext`][] object.
1319-
**Default:** A no-op function.
1320-
* Returns: {Promise} Immediately fulfilled with `undefined`.
1355+
Alias for [`suite()`][].
13211356

1322-
The `describe()` function imported from the `node:test` module. Each
1323-
invocation of this function results in the creation of a Subtest.
1324-
After invocation of top level `describe` functions,
1325-
all top level tests and suites will execute.
1357+
The `describe()` function is imported from the `node:test` module.
13261358

13271359
## `describe.skip([name][, options][, fn])`
13281360

1329-
Shorthand for skipping a suite, same as [`describe([name], { skip: true }[, fn])`][describe options].
1361+
Shorthand for skipping a suite. This is the same as
1362+
[`describe([name], { skip: true }[, fn])`][describe options].
13301363

13311364
## `describe.todo([name][, options][, fn])`
13321365

1333-
Shorthand for marking a suite as `TODO`, same as
1366+
Shorthand for marking a suite as `TODO`. This is the same as
13341367
[`describe([name], { todo: true }[, fn])`][describe options].
13351368

13361369
## `describe.only([name][, options][, fn])`
@@ -1341,7 +1374,7 @@ added:
13411374
- v18.15.0
13421375
-->
13431376

1344-
Shorthand for marking a suite as `only`, same as
1377+
Shorthand for marking a suite as `only`. This is the same as
13451378
[`describe([name], { only: true }[, fn])`][describe options].
13461379

13471380
## `it([name][, options][, fn])`
@@ -1358,7 +1391,7 @@ changes:
13581391
description: Calling `it()` is now equivalent to calling `test()`.
13591392
-->
13601393

1361-
Shorthand for [`test()`][].
1394+
Alias for [`test()`][].
13621395

13631396
The `it()` function is imported from the `node:test` module.
13641397

@@ -1402,7 +1435,7 @@ added:
14021435
If unspecified, subtests inherit this value from their parent.
14031436
**Default:** `Infinity`.
14041437

1405-
This function is used to create a hook running before running a suite.
1438+
This function creates a hook that runs before executing a suite.
14061439

14071440
```js
14081441
describe('tests', async () => {
@@ -1432,7 +1465,7 @@ added:
14321465
If unspecified, subtests inherit this value from their parent.
14331466
**Default:** `Infinity`.
14341467

1435-
This function is used to create a hook running after running a suite.
1468+
This function creates a hook that runs after executing a suite.
14361469

14371470
```js
14381471
describe('tests', async () => {
@@ -1465,8 +1498,7 @@ added:
14651498
If unspecified, subtests inherit this value from their parent.
14661499
**Default:** `Infinity`.
14671500

1468-
This function is used to create a hook running
1469-
before each subtest of the current suite.
1501+
This function creates a hook that runs before each test in the current suite.
14701502

14711503
```js
14721504
describe('tests', async () => {
@@ -1496,11 +1528,8 @@ added:
14961528
If unspecified, subtests inherit this value from their parent.
14971529
**Default:** `Infinity`.
14981530

1499-
This function is used to create a hook running
1500-
after each subtest of the current test.
1501-
1502-
**Note:** The `afterEach` hook is guaranteed to run after every test,
1503-
even if any of the tests fail.
1531+
This function creates a hook that runs after each test in the current suite.
1532+
The `afterEach()` hook is run even if the test fails.
15041533

15051534
```js
15061535
describe('tests', async () => {
@@ -3075,10 +3104,13 @@ Can be used to abort test subtasks when the test has been aborted.
30753104
[`context.todo`]: #contexttodomessage
30763105
[`describe()`]: #describename-options-fn
30773106
[`glob(7)`]: https://man7.org/linux/man-pages/man7/glob.7.html
3107+
[`it()`]: #itname-options-fn
30783108
[`run()`]: #runoptions
3109+
[`suite()`]: #suitename-options-fn
30793110
[`test()`]: #testname-options-fn
30803111
[describe options]: #describename-options-fn
30813112
[it options]: #testname-options-fn
30823113
[stream.compose]: stream.md#streamcomposestreams
3114+
[suite options]: #suitename-options-fn
30833115
[test reporters]: #test-reporters
30843116
[test runner execution model]: #test-runner-execution-model

lib/internal/test_runner/harness.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,7 @@ function hook(hook) {
282282
module.exports = {
283283
createTestTree,
284284
test: runInParentContext(Test),
285-
describe: runInParentContext(Suite),
286-
it: runInParentContext(Test),
285+
suite: runInParentContext(Suite),
287286
before: hook('before'),
288287
after: hook('after'),
289288
beforeEach: hook('beforeEach'),

lib/test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
const { ObjectAssign, ObjectDefineProperty } = primordials;
3-
const { test, describe, it, before, after, beforeEach, afterEach } = require('internal/test_runner/harness');
3+
const { test, suite, before, after, beforeEach, afterEach } = require('internal/test_runner/harness');
44
const { run } = require('internal/test_runner/runner');
55

66
module.exports = test;
@@ -9,9 +9,10 @@ ObjectAssign(module.exports, {
99
afterEach,
1010
before,
1111
beforeEach,
12-
describe,
13-
it,
12+
describe: suite,
13+
it: test,
1414
run,
15+
suite,
1516
test,
1617
});
1718

test/parallel/test-runner-aliases.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
require('../common');
3+
const { strictEqual } = require('node:assert');
4+
const test = require('node:test');
5+
6+
strictEqual(test.test, test);
7+
strictEqual(test.it, test);
8+
strictEqual(test.describe, test.suite);

0 commit comments

Comments
 (0)