Skip to content

Commit 41646d9

Browse files
cjihrigmarco-ippolito
authored andcommittedMay 3, 2024
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 bda3cde commit 41646d9

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';
@@ -1200,6 +1199,51 @@ run({ files: [path.resolve('./tests/test.js')] })
12001199
.pipe(process.stdout);
12011200
```
12021201

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

12051249
<!-- YAML
@@ -1251,7 +1295,7 @@ changes:
12511295
the callback function is passed as the second argument. **Default:** A no-op
12521296
function.
12531297
* Returns: {Promise} Fulfilled with `undefined` once
1254-
the test completes, or immediately if the test runs within [`describe()`][].
1298+
the test completes, or immediately if the test runs within a suite.
12551299

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

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

13031347
## `describe([name][, options][, fn])`
13041348

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

1316-
The `describe()` function imported from the `node:test` module. Each
1317-
invocation of this function results in the creation of a Subtest.
1318-
After invocation of top level `describe` functions,
1319-
all top level tests and suites will execute.
1351+
The `describe()` function is imported from the `node:test` module.
13201352

13211353
## `describe.skip([name][, options][, fn])`
13221354

1323-
Shorthand for skipping a suite, same as [`describe([name], { skip: true }[, fn])`][describe options].
1355+
Shorthand for skipping a suite. This is the same as
1356+
[`describe([name], { skip: true }[, fn])`][describe options].
13241357

13251358
## `describe.todo([name][, options][, fn])`
13261359

1327-
Shorthand for marking a suite as `TODO`, same as
1360+
Shorthand for marking a suite as `TODO`. This is the same as
13281361
[`describe([name], { todo: true }[, fn])`][describe options].
13291362

13301363
## `describe.only([name][, options][, fn])`
@@ -1335,7 +1368,7 @@ added:
13351368
- v18.15.0
13361369
-->
13371370

1338-
Shorthand for marking a suite as `only`, same as
1371+
Shorthand for marking a suite as `only`. This is the same as
13391372
[`describe([name], { only: true }[, fn])`][describe options].
13401373

13411374
## `it([name][, options][, fn])`
@@ -1350,7 +1383,7 @@ changes:
13501383
description: Calling `it()` is now equivalent to calling `test()`.
13511384
-->
13521385

1353-
Shorthand for [`test()`][].
1386+
Alias for [`test()`][].
13541387

13551388
The `it()` function is imported from the `node:test` module.
13561389

@@ -1394,7 +1427,7 @@ added:
13941427
If unspecified, subtests inherit this value from their parent.
13951428
**Default:** `Infinity`.
13961429

1397-
This function is used to create a hook running before running a suite.
1430+
This function creates a hook that runs before executing a suite.
13981431

13991432
```js
14001433
describe('tests', async () => {
@@ -1424,7 +1457,7 @@ added:
14241457
If unspecified, subtests inherit this value from their parent.
14251458
**Default:** `Infinity`.
14261459

1427-
This function is used to create a hook running after running a suite.
1460+
This function creates a hook that runs after executing a suite.
14281461

14291462
```js
14301463
describe('tests', async () => {
@@ -1457,8 +1490,7 @@ added:
14571490
If unspecified, subtests inherit this value from their parent.
14581491
**Default:** `Infinity`.
14591492

1460-
This function is used to create a hook running
1461-
before each subtest of the current suite.
1493+
This function creates a hook that runs before each test in the current suite.
14621494

14631495
```js
14641496
describe('tests', async () => {
@@ -1488,11 +1520,8 @@ added:
14881520
If unspecified, subtests inherit this value from their parent.
14891521
**Default:** `Infinity`.
14901522

1491-
This function is used to create a hook running
1492-
after each subtest of the current test.
1493-
1494-
**Note:** The `afterEach` hook is guaranteed to run after every test,
1495-
even if any of the tests fail.
1523+
This function creates a hook that runs after each test in the current suite.
1524+
The `afterEach()` hook is run even if the test fails.
14961525

14971526
```js
14981527
describe('tests', async () => {
@@ -3054,10 +3083,13 @@ Can be used to abort test subtasks when the test has been aborted.
30543083
[`context.skip`]: #contextskipmessage
30553084
[`context.todo`]: #contexttodomessage
30563085
[`describe()`]: #describename-options-fn
3086+
[`it()`]: #itname-options-fn
30573087
[`run()`]: #runoptions
3088+
[`suite()`]: #suitename-options-fn
30583089
[`test()`]: #testname-options-fn
30593090
[describe options]: #describename-options-fn
30603091
[it options]: #testname-options-fn
30613092
[stream.compose]: stream.md#streamcomposestreams
3093+
[suite options]: #suitename-options-fn
30623094
[test reporters]: #test-reporters
30633095
[test runner execution model]: #test-runner-execution-model

‎lib/internal/test_runner/harness.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,7 @@ function hook(hook) {
270270
module.exports = {
271271
createTestTree,
272272
test: runInParentContext(Test),
273-
describe: runInParentContext(Suite),
274-
it: runInParentContext(Test),
273+
suite: runInParentContext(Suite),
275274
before: hook('before'),
276275
after: hook('after'),
277276
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)
Please sign in to comment.