|
| 1 | +# Test runner |
| 2 | + |
| 3 | +<!--introduced_in=REPLACEME--> |
| 4 | + |
| 5 | +> Stability: 1 - Experimental |
| 6 | +
|
| 7 | +<!-- source_link=lib/test.js --> |
| 8 | + |
| 9 | +The `node:test` module facilitates the creation of JavaScript tests that |
| 10 | +report results in [TAP][] format. To access it: |
| 11 | + |
| 12 | +```mjs |
| 13 | +import test from 'node:test'; |
| 14 | +``` |
| 15 | + |
| 16 | +```cjs |
| 17 | +const test = require('node:test'); |
| 18 | +``` |
| 19 | + |
| 20 | +This module is only available under the `node:` scheme. The following will not |
| 21 | +work: |
| 22 | + |
| 23 | +```mjs |
| 24 | +import test from 'test'; |
| 25 | +``` |
| 26 | + |
| 27 | +```cjs |
| 28 | +const test = require('test'); |
| 29 | +``` |
| 30 | + |
| 31 | +Tests created via the `test` module consist of a single function that is |
| 32 | +processed in one of three ways: |
| 33 | + |
| 34 | +1. A synchronous function that is considered failing if it throws an exception, |
| 35 | + and is considered passing otherwise. |
| 36 | +2. A function that returns a `Promise` that is considered failing if the |
| 37 | + `Promise` rejects, and is considered passing if the `Promise` resolves. |
| 38 | +3. A function that receives a callback function. If the callback receives any |
| 39 | + truthy value as its first argument, the test is considered failing. If a |
| 40 | + falsy value is passed as the first argument to the callback, the test is |
| 41 | + considered passing. If the test function receives a callback function and |
| 42 | + also returns a `Promise`, the test will fail. |
| 43 | + |
| 44 | +The following example illustrates how tests are written using the |
| 45 | +`test` module. |
| 46 | + |
| 47 | +```js |
| 48 | +test('synchronous passing test', (t) => { |
| 49 | + // This test passes because it does not throw an exception. |
| 50 | + assert.strictEqual(1, 1); |
| 51 | +}); |
| 52 | + |
| 53 | +test('synchronous failing test', (t) => { |
| 54 | + // This test fails because it throws an exception. |
| 55 | + assert.strictEqual(1, 2); |
| 56 | +}); |
| 57 | + |
| 58 | +test('asynchronous passing test', async (t) => { |
| 59 | + // This test passes because the Promise returned by the async |
| 60 | + // function is not rejected. |
| 61 | + assert.strictEqual(1, 1); |
| 62 | +}); |
| 63 | + |
| 64 | +test('asynchronous failing test', async (t) => { |
| 65 | + // This test fails because the Promise returned by the async |
| 66 | + // function is rejected. |
| 67 | + assert.strictEqual(1, 2); |
| 68 | +}); |
| 69 | + |
| 70 | +test('failing test using Promises', (t) => { |
| 71 | + // Promises can be used directly as well. |
| 72 | + return new Promise((resolve, reject) => { |
| 73 | + setImmediate(() => { |
| 74 | + reject(new Error('this will cause the test to fail')); |
| 75 | + }); |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +test('callback passing test', (t, done) => { |
| 80 | + // done() is the callback function. When the setImmediate() runs, it invokes |
| 81 | + // done() with no arguments. |
| 82 | + setImmediate(done); |
| 83 | +}); |
| 84 | + |
| 85 | +test('callback failing test', (t, done) => { |
| 86 | + // When the setImmediate() runs, done() is invoked with an Error object and |
| 87 | + // the test fails. |
| 88 | + setImmediate(() => { |
| 89 | + done(new Error('callback failure')); |
| 90 | + }); |
| 91 | +}); |
| 92 | +``` |
| 93 | + |
| 94 | +As a test file executes, TAP is written to the standard output of the Node.js |
| 95 | +process. This output can be interpreted by any test harness that understands |
| 96 | +the TAP format. If any tests fail, the process exit code is set to `1`. |
| 97 | + |
| 98 | +## Subtests |
| 99 | + |
| 100 | +The test context's `test()` method allows subtests to be created. This method |
| 101 | +behaves identically to the top level `test()` function. The following example |
| 102 | +demonstrates the creation of a top level test with two subtests. |
| 103 | + |
| 104 | +```js |
| 105 | +test('top level test', async (t) => { |
| 106 | + await t.test('subtest 1', (t) => { |
| 107 | + assert.strictEqual(1, 1); |
| 108 | + }); |
| 109 | + |
| 110 | + await t.test('subtest 2', (t) => { |
| 111 | + assert.strictEqual(2, 2); |
| 112 | + }); |
| 113 | +}); |
| 114 | +``` |
| 115 | + |
| 116 | +In this example, `await` is used to ensure that both subtests have completed. |
| 117 | +This is necessary because parent tests do not wait for their subtests to |
| 118 | +complete. Any subtests that are still outstanding when their parent finishes |
| 119 | +are cancelled and treated as failures. Any subtest failures cause the parent |
| 120 | +test to fail. |
| 121 | + |
| 122 | +## Skipping tests |
| 123 | + |
| 124 | +Individual tests can be skipped by passing the `skip` option to the test, or by |
| 125 | +calling the test context's `skip()` method. Both of these options support |
| 126 | +including a message that is displayed in the TAP output as shown in the |
| 127 | +following example. |
| 128 | + |
| 129 | +```js |
| 130 | +// The skip option is used, but no message is provided. |
| 131 | +test('skip option', { skip: true }, (t) => { |
| 132 | + // This code is never executed. |
| 133 | +}); |
| 134 | + |
| 135 | +// The skip option is used, and a message is provided. |
| 136 | +test('skip option with message', { skip: 'this is skipped' }, (t) => { |
| 137 | + // This code is never executed. |
| 138 | +}); |
| 139 | + |
| 140 | +test('skip() method', (t) => { |
| 141 | + // Make sure to return here as well if the test contains additional logic. |
| 142 | + t.skip(); |
| 143 | +}); |
| 144 | + |
| 145 | +test('skip() method with message', (t) => { |
| 146 | + // Make sure to return here as well if the test contains additional logic. |
| 147 | + t.skip('this is skipped'); |
| 148 | +}); |
| 149 | +``` |
| 150 | + |
| 151 | +## Extraneous asynchronous activity |
| 152 | + |
| 153 | +Once a test function finishes executing, the TAP results are output as quickly |
| 154 | +as possible while maintaining the order of the tests. However, it is possible |
| 155 | +for the test function to generate asynchronous activity that outlives the test |
| 156 | +itself. The test runner handles this type of activity, but does not delay the |
| 157 | +reporting of test results in order to accommodate it. |
| 158 | + |
| 159 | +In the following example, a test completes with two `setImmediate()` |
| 160 | +operations still outstanding. The first `setImmediate()` attempts to create a |
| 161 | +new subtest. Because the parent test has already finished and output its |
| 162 | +results, the new subtest is immediately marked as failed, and reported in the |
| 163 | +top level of the file's TAP output. |
| 164 | + |
| 165 | +The second `setImmediate()` creates an `uncaughtException` event. |
| 166 | +`uncaughtException` and `unhandledRejection` events originating from a completed |
| 167 | +test are handled by the `test` module and reported as diagnostic warnings in |
| 168 | +the top level of the file's TAP output. |
| 169 | + |
| 170 | +```js |
| 171 | +test('a test that creates asynchronous activity', (t) => { |
| 172 | + setImmediate(() => { |
| 173 | + t.test('subtest that is created too late', (t) => { |
| 174 | + throw new Error('error1'); |
| 175 | + }); |
| 176 | + }); |
| 177 | + |
| 178 | + setImmediate(() => { |
| 179 | + throw new Error('error2'); |
| 180 | + }); |
| 181 | + |
| 182 | + // The test finishes after this line. |
| 183 | +}); |
| 184 | +``` |
| 185 | + |
| 186 | +## `test([name][, options][, fn])` |
| 187 | + |
| 188 | +<!-- YAML |
| 189 | +added: REPLACEME |
| 190 | +--> |
| 191 | + |
| 192 | +* `name` {string} The name of the test, which is displayed when reporting test |
| 193 | + results. **Default:** The `name` property of `fn`, or `'<anonymous>'` if `fn` |
| 194 | + does not have a name. |
| 195 | +* `options` {Object} Configuration options for the test. The following |
| 196 | + properties are supported: |
| 197 | + * `concurrency` {number} The number of tests that can be run at the same time. |
| 198 | + If unspecified, subtests inherit this value from their parent. |
| 199 | + **Default:** `1`. |
| 200 | + * `skip` {boolean|string} If truthy, the test is skipped. If a string is |
| 201 | + provided, that string is displayed in the test results as the reason for |
| 202 | + skipping the test. **Default:** `false`. |
| 203 | + * `todo` {boolean|string} If truthy, the test marked as `TODO`. If a string |
| 204 | + is provided, that string is displayed in the test results as the reason why |
| 205 | + the test is `TODO`. **Default:** `false`. |
| 206 | +* `fn` {Function|AsyncFunction} The function under test. This first argument |
| 207 | + to this function is a [`TestContext`][] object. If the test uses callbacks, |
| 208 | + the callback function is passed as the second argument. **Default:** A no-op |
| 209 | + function. |
| 210 | +* Returns: {Promise} Resolved with `undefined` once the test completes. |
| 211 | + |
| 212 | +The `test()` function is the value imported from the `test` module. Each |
| 213 | +invocation of this function results in the creation of a test point in the TAP |
| 214 | +output. |
| 215 | + |
| 216 | +The `TestContext` object passed to the `fn` argument can be used to perform |
| 217 | +actions related to the current test. Examples include skipping the test, adding |
| 218 | +additional TAP diagnostic information, or creating subtests. |
| 219 | + |
| 220 | +`test()` returns a `Promise` that resolves once the test completes. The return |
| 221 | +value can usually be discarded for top level tests. However, the return value |
| 222 | +from subtests should be used to prevent the parent test from finishing first |
| 223 | +and cancelling the subtest as shown in the following example. |
| 224 | + |
| 225 | +```js |
| 226 | +test('top level test', async (t) => { |
| 227 | + // The setTimeout() in the following subtest would cause it to outlive its |
| 228 | + // parent test if 'await' is removed on the next line. Once the parent test |
| 229 | + // completes, it will cancel any outstanding subtests. |
| 230 | + await t.test('longer running subtest', async (t) => { |
| 231 | + return new Promise((resolve, reject) => { |
| 232 | + setTimeout(resolve, 1000); |
| 233 | + }); |
| 234 | + }); |
| 235 | +}); |
| 236 | +``` |
| 237 | + |
| 238 | +## Class: `TestContext` |
| 239 | + |
| 240 | +<!-- YAML |
| 241 | +added: REPLACEME |
| 242 | +--> |
| 243 | + |
| 244 | +An instance of `TestContext` is passed to each test function in order to |
| 245 | +interact with the test runner. However, the `TestContext` constructor is not |
| 246 | +exposed as part of the API. |
| 247 | + |
| 248 | +### `context.diagnostic(message)` |
| 249 | + |
| 250 | +<!-- YAML |
| 251 | +added: REPLACEME |
| 252 | +--> |
| 253 | + |
| 254 | +* `message` {string} Message to be displayed as a TAP diagnostic. |
| 255 | + |
| 256 | +This function is used to write TAP diagnostics to the output. Any diagnostic |
| 257 | +information is included at the end of the test's results. This function does |
| 258 | +not return a value. |
| 259 | + |
| 260 | +### `context.skip([message])` |
| 261 | + |
| 262 | +<!-- YAML |
| 263 | +added: REPLACEME |
| 264 | +--> |
| 265 | + |
| 266 | +* `message` {string} Optional skip message to be displayed in TAP output. |
| 267 | + |
| 268 | +This function causes the test's output to indicate the test as skipped. If |
| 269 | +`message` is provided, it is included in the TAP output. Calling `skip()` does |
| 270 | +not terminate execution of the test function. This function does not return a |
| 271 | +value. |
| 272 | + |
| 273 | +### `context.todo([message])` |
| 274 | + |
| 275 | +<!-- YAML |
| 276 | +added: REPLACEME |
| 277 | +--> |
| 278 | + |
| 279 | +* `message` {string} Optional `TODO` message to be displayed in TAP output. |
| 280 | + |
| 281 | +This function adds a `TODO` directive to the test's output. If `message` is |
| 282 | +provided, it is included in the TAP output. Calling `todo()` does not terminate |
| 283 | +execution of the test function. This function does not return a value. |
| 284 | + |
| 285 | +### `context.test([name][, options][, fn])` |
| 286 | + |
| 287 | +<!-- YAML |
| 288 | +added: REPLACEME |
| 289 | +--> |
| 290 | + |
| 291 | +* `name` {string} The name of the subtest, which is displayed when reporting |
| 292 | + test results. **Default:** The `name` property of `fn`, or `'<anonymous>'` if |
| 293 | + `fn` does not have a name. |
| 294 | +* `options` {Object} Configuration options for the subtest. The following |
| 295 | + properties are supported: |
| 296 | + * `concurrency` {number} The number of tests that can be run at the same time. |
| 297 | + If unspecified, subtests inherit this value from their parent. |
| 298 | + **Default:** `1`. |
| 299 | + * `skip` {boolean|string} If truthy, the test is skipped. If a string is |
| 300 | + provided, that string is displayed in the test results as the reason for |
| 301 | + skipping the test. **Default:** `false`. |
| 302 | + * `todo` {boolean|string} If truthy, the test marked as `TODO`. If a string |
| 303 | + is provided, that string is displayed in the test results as the reason why |
| 304 | + the test is `TODO`. **Default:** `false`. |
| 305 | +* `fn` {Function|AsyncFunction} The function under test. This first argument |
| 306 | + to this function is a [`TestContext`][] object. If the test uses callbacks, |
| 307 | + the callback function is passed as the second argument. **Default:** A no-op |
| 308 | + function. |
| 309 | +* Returns: {Promise} Resolved with `undefined` once the test completes. |
| 310 | + |
| 311 | +This function is used to create subtests under the current test. This function |
| 312 | +behaves in the same fashion as the top level [`test()`][] function. |
| 313 | + |
| 314 | +[TAP]: https://testanything.org/ |
| 315 | +[`TestContext`]: #class-testcontext |
| 316 | +[`test()`]: #testname-options-fn |
0 commit comments