rahad-media-downloader 2.1.12 → 2.1.14
Sign up to get free protection for your applications and to get access to all the features.
- package/.cache/replit/modules/nodejs-20.res +1 -0
- package/.cache/replit/modules/replit.res +1 -0
- package/.cache/typescript/5.4/node_modules/.package-lock.json +185 -0
- package/.cache/typescript/5.4/node_modules/@types/caseless/LICENSE +21 -0
- package/.cache/typescript/5.4/node_modules/@types/caseless/README.md +48 -0
- package/.cache/typescript/5.4/node_modules/@types/caseless/index.d.ts +29 -0
- package/.cache/typescript/5.4/node_modules/@types/caseless/package.json +35 -0
- package/.cache/typescript/5.4/node_modules/@types/domhandler/LICENSE +21 -0
- package/.cache/typescript/5.4/node_modules/@types/domhandler/README.md +92 -0
- package/.cache/typescript/5.4/node_modules/@types/domhandler/index.d.ts +73 -0
- package/.cache/typescript/5.4/node_modules/@types/domhandler/package.json +25 -0
- package/.cache/typescript/5.4/node_modules/@types/domutils/LICENSE +21 -0
- package/.cache/typescript/5.4/node_modules/@types/domutils/README.md +15 -0
- package/.cache/typescript/5.4/node_modules/@types/domutils/index.d.ts +124 -0
- package/.cache/typescript/5.4/node_modules/@types/domutils/package.json +27 -0
- package/.cache/typescript/5.4/node_modules/@types/htmlparser2/LICENSE +21 -0
- package/.cache/typescript/5.4/node_modules/@types/htmlparser2/README.md +15 -0
- package/.cache/typescript/5.4/node_modules/@types/htmlparser2/index.d.ts +120 -0
- package/.cache/typescript/5.4/node_modules/@types/htmlparser2/package.json +45 -0
- package/.cache/typescript/5.4/node_modules/@types/node/LICENSE +21 -0
- package/.cache/typescript/5.4/node_modules/@types/node/README.md +15 -0
- package/.cache/typescript/5.4/node_modules/@types/node/assert/strict.d.ts +8 -0
- package/.cache/typescript/5.4/node_modules/@types/node/assert.d.ts +1040 -0
- package/.cache/typescript/5.4/node_modules/@types/node/async_hooks.d.ts +541 -0
- package/.cache/typescript/5.4/node_modules/@types/node/buffer.d.ts +2363 -0
- package/.cache/typescript/5.4/node_modules/@types/node/child_process.d.ts +1542 -0
- package/.cache/typescript/5.4/node_modules/@types/node/cluster.d.ts +578 -0
- package/.cache/typescript/5.4/node_modules/@types/node/console.d.ts +452 -0
- package/.cache/typescript/5.4/node_modules/@types/node/constants.d.ts +19 -0
- package/.cache/typescript/5.4/node_modules/@types/node/crypto.d.ts +4522 -0
- package/.cache/typescript/5.4/node_modules/@types/node/dgram.d.ts +596 -0
- package/.cache/typescript/5.4/node_modules/@types/node/diagnostics_channel.d.ts +545 -0
- package/.cache/typescript/5.4/node_modules/@types/node/dns/promises.d.ts +473 -0
- package/.cache/typescript/5.4/node_modules/@types/node/dns.d.ts +853 -0
- package/.cache/typescript/5.4/node_modules/@types/node/dom-events.d.ts +124 -0
- package/.cache/typescript/5.4/node_modules/@types/node/domain.d.ts +170 -0
- package/.cache/typescript/5.4/node_modules/@types/node/events.d.ts +884 -0
- package/.cache/typescript/5.4/node_modules/@types/node/fs/promises.d.ts +1245 -0
- package/.cache/typescript/5.4/node_modules/@types/node/fs.d.ts +4317 -0
- package/.cache/typescript/5.4/node_modules/@types/node/globals.d.ts +411 -0
- package/.cache/typescript/5.4/node_modules/@types/node/globals.global.d.ts +1 -0
- package/.cache/typescript/5.4/node_modules/@types/node/http.d.ts +1889 -0
- package/.cache/typescript/5.4/node_modules/@types/node/http2.d.ts +2418 -0
- package/.cache/typescript/5.4/node_modules/@types/node/https.d.ts +550 -0
- package/.cache/typescript/5.4/node_modules/@types/node/index.d.ts +89 -0
- package/.cache/typescript/5.4/node_modules/@types/node/inspector.d.ts +2746 -0
- package/.cache/typescript/5.4/node_modules/@types/node/module.d.ts +315 -0
- package/.cache/typescript/5.4/node_modules/@types/node/net.d.ts +996 -0
- package/.cache/typescript/5.4/node_modules/@types/node/os.d.ts +495 -0
- package/.cache/typescript/5.4/node_modules/@types/node/package.json +217 -0
- package/.cache/typescript/5.4/node_modules/@types/node/path.d.ts +191 -0
- package/.cache/typescript/5.4/node_modules/@types/node/perf_hooks.d.ts +645 -0
- package/.cache/typescript/5.4/node_modules/@types/node/process.d.ts +1747 -0
- package/.cache/typescript/5.4/node_modules/@types/node/punycode.d.ts +117 -0
- package/.cache/typescript/5.4/node_modules/@types/node/querystring.d.ts +153 -0
- package/.cache/typescript/5.4/node_modules/@types/node/readline/promises.d.ts +150 -0
- package/.cache/typescript/5.4/node_modules/@types/node/readline.d.ts +540 -0
- package/.cache/typescript/5.4/node_modules/@types/node/repl.d.ts +430 -0
- package/.cache/typescript/5.4/node_modules/@types/node/sea.d.ts +153 -0
- package/.cache/typescript/5.4/node_modules/@types/node/stream/consumers.d.ts +12 -0
- package/.cache/typescript/5.4/node_modules/@types/node/stream/promises.d.ts +83 -0
- package/.cache/typescript/5.4/node_modules/@types/node/stream/web.d.ts +367 -0
- package/.cache/typescript/5.4/node_modules/@types/node/stream.d.ts +1707 -0
- package/.cache/typescript/5.4/node_modules/@types/node/string_decoder.d.ts +67 -0
- package/.cache/typescript/5.4/node_modules/@types/node/test.d.ts +1470 -0
- package/.cache/typescript/5.4/node_modules/@types/node/timers/promises.d.ts +97 -0
- package/.cache/typescript/5.4/node_modules/@types/node/timers.d.ts +240 -0
- package/.cache/typescript/5.4/node_modules/@types/node/tls.d.ts +1217 -0
- package/.cache/typescript/5.4/node_modules/@types/node/trace_events.d.ts +197 -0
- package/.cache/typescript/5.4/node_modules/@types/node/tty.d.ts +208 -0
- package/.cache/typescript/5.4/node_modules/@types/node/url.d.ts +944 -0
- package/.cache/typescript/5.4/node_modules/@types/node/util.d.ts +2276 -0
- package/.cache/typescript/5.4/node_modules/@types/node/v8.d.ts +764 -0
- package/.cache/typescript/5.4/node_modules/@types/node/vm.d.ts +921 -0
- package/.cache/typescript/5.4/node_modules/@types/node/wasi.d.ts +181 -0
- package/.cache/typescript/5.4/node_modules/@types/node/worker_threads.d.ts +691 -0
- package/.cache/typescript/5.4/node_modules/@types/node/zlib.d.ts +530 -0
- package/.cache/typescript/5.4/node_modules/@types/node-fetch/LICENSE +21 -0
- package/.cache/typescript/5.4/node_modules/@types/node-fetch/README.md +15 -0
- package/.cache/typescript/5.4/node_modules/@types/node-fetch/externals.d.ts +32 -0
- package/.cache/typescript/5.4/node_modules/@types/node-fetch/index.d.ts +238 -0
- package/.cache/typescript/5.4/node_modules/@types/node-fetch/package.json +83 -0
- package/.cache/typescript/5.4/node_modules/@types/qs/LICENSE +21 -0
- package/.cache/typescript/5.4/node_modules/@types/qs/README.md +15 -0
- package/.cache/typescript/5.4/node_modules/@types/qs/index.d.ts +79 -0
- package/.cache/typescript/5.4/node_modules/@types/qs/package.json +65 -0
- package/.cache/typescript/5.4/node_modules/@types/request/LICENSE +21 -0
- package/.cache/typescript/5.4/node_modules/@types/request/README.md +15 -0
- package/.cache/typescript/5.4/node_modules/@types/request/index.d.ts +395 -0
- package/.cache/typescript/5.4/node_modules/@types/request/node_modules/form-data/License +19 -0
- package/.cache/typescript/5.4/node_modules/@types/request/node_modules/form-data/README.md +350 -0
- package/.cache/typescript/5.4/node_modules/@types/request/node_modules/form-data/README.md.bak +350 -0
- package/.cache/typescript/5.4/node_modules/@types/request/node_modules/form-data/index.d.ts +51 -0
- package/.cache/typescript/5.4/node_modules/@types/request/node_modules/form-data/lib/browser.js +2 -0
- package/.cache/typescript/5.4/node_modules/@types/request/node_modules/form-data/lib/form_data.js +483 -0
- package/.cache/typescript/5.4/node_modules/@types/request/node_modules/form-data/lib/populate.js +10 -0
- package/.cache/typescript/5.4/node_modules/@types/request/node_modules/form-data/package.json +68 -0
- package/.cache/typescript/5.4/node_modules/@types/request/package.json +70 -0
- package/.cache/typescript/5.4/node_modules/@types/tough-cookie/LICENSE +21 -0
- package/.cache/typescript/5.4/node_modules/@types/tough-cookie/README.md +15 -0
- package/.cache/typescript/5.4/node_modules/@types/tough-cookie/index.d.ts +321 -0
- package/.cache/typescript/5.4/node_modules/@types/tough-cookie/package.json +35 -0
- package/.cache/typescript/5.4/node_modules/asynckit/LICENSE +21 -0
- package/.cache/typescript/5.4/node_modules/asynckit/README.md +233 -0
- package/.cache/typescript/5.4/node_modules/asynckit/bench.js +76 -0
- package/.cache/typescript/5.4/node_modules/asynckit/index.js +6 -0
- package/.cache/typescript/5.4/node_modules/asynckit/lib/abort.js +29 -0
- package/.cache/typescript/5.4/node_modules/asynckit/lib/async.js +34 -0
- package/.cache/typescript/5.4/node_modules/asynckit/lib/defer.js +26 -0
- package/.cache/typescript/5.4/node_modules/asynckit/lib/iterate.js +75 -0
- package/.cache/typescript/5.4/node_modules/asynckit/lib/readable_asynckit.js +91 -0
- package/.cache/typescript/5.4/node_modules/asynckit/lib/readable_parallel.js +25 -0
- package/.cache/typescript/5.4/node_modules/asynckit/lib/readable_serial.js +25 -0
- package/.cache/typescript/5.4/node_modules/asynckit/lib/readable_serial_ordered.js +29 -0
- package/.cache/typescript/5.4/node_modules/asynckit/lib/state.js +37 -0
- package/.cache/typescript/5.4/node_modules/asynckit/lib/streamify.js +141 -0
- package/.cache/typescript/5.4/node_modules/asynckit/lib/terminator.js +29 -0
- package/.cache/typescript/5.4/node_modules/asynckit/package.json +63 -0
- package/.cache/typescript/5.4/node_modules/asynckit/parallel.js +43 -0
- package/.cache/typescript/5.4/node_modules/asynckit/serial.js +17 -0
- package/.cache/typescript/5.4/node_modules/asynckit/serialOrdered.js +75 -0
- package/.cache/typescript/5.4/node_modules/asynckit/stream.js +21 -0
- package/.cache/typescript/5.4/node_modules/combined-stream/License +19 -0
- package/.cache/typescript/5.4/node_modules/combined-stream/Readme.md +138 -0
- package/.cache/typescript/5.4/node_modules/combined-stream/lib/combined_stream.js +208 -0
- package/.cache/typescript/5.4/node_modules/combined-stream/package.json +25 -0
- package/.cache/typescript/5.4/node_modules/combined-stream/yarn.lock +17 -0
- package/.cache/typescript/5.4/node_modules/delayed-stream/License +19 -0
- package/.cache/typescript/5.4/node_modules/delayed-stream/Makefile +7 -0
- package/.cache/typescript/5.4/node_modules/delayed-stream/Readme.md +141 -0
- package/.cache/typescript/5.4/node_modules/delayed-stream/lib/delayed_stream.js +107 -0
- package/.cache/typescript/5.4/node_modules/delayed-stream/package.json +27 -0
- package/.cache/typescript/5.4/node_modules/domelementtype/LICENSE +11 -0
- package/.cache/typescript/5.4/node_modules/domelementtype/index.js +15 -0
- package/.cache/typescript/5.4/node_modules/domelementtype/package.json +16 -0
- package/.cache/typescript/5.4/node_modules/domelementtype/readme.md +1 -0
- package/.cache/typescript/5.4/node_modules/domhandler/.travis.yml +6 -0
- package/.cache/typescript/5.4/node_modules/domhandler/LICENSE +11 -0
- package/.cache/typescript/5.4/node_modules/domhandler/index.js +217 -0
- package/.cache/typescript/5.4/node_modules/domhandler/lib/element.js +20 -0
- package/.cache/typescript/5.4/node_modules/domhandler/lib/node.js +44 -0
- package/.cache/typescript/5.4/node_modules/domhandler/package.json +41 -0
- package/.cache/typescript/5.4/node_modules/domhandler/readme.md +116 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/01-basic.json +57 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/02-single_tag_1.json +21 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/03-single_tag_2.json +21 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/04-unescaped_in_script.json +27 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/05-tags_in_comment.json +18 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/06-comment_in_script.json +18 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/07-unescaped_in_style.json +20 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/08-extra_spaces_in_tag.json +20 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/09-unquoted_attrib.json +20 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/10-singular_attribute.json +15 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/11-text_outside_tags.json +40 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/12-text_only.json +11 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/13-comment_in_text.json +19 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/14-comment_in_text_in_script.json +18 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/15-non-verbose.json +22 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/16-normalize_whitespace.json +47 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/17-xml_namespace.json +18 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/18-enforce_empty_tags.json +16 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/19-ignore_empty_tags.json +20 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/20-template_script_tags.json +20 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/21-conditional_comments.json +15 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/22-lowercase_tags.json +41 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/23-dom-lvl1.json +131 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/24-with-start-indices.json +85 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/cases/25-with-end-indices.json +86 -0
- package/.cache/typescript/5.4/node_modules/domhandler/test/tests.js +60 -0
- package/.cache/typescript/5.4/node_modules/form-data/License +19 -0
- package/.cache/typescript/5.4/node_modules/form-data/README.md.bak +358 -0
- package/.cache/typescript/5.4/node_modules/form-data/Readme.md +358 -0
- package/.cache/typescript/5.4/node_modules/form-data/index.d.ts +62 -0
- package/.cache/typescript/5.4/node_modules/form-data/lib/browser.js +2 -0
- package/.cache/typescript/5.4/node_modules/form-data/lib/form_data.js +501 -0
- package/.cache/typescript/5.4/node_modules/form-data/lib/populate.js +10 -0
- package/.cache/typescript/5.4/node_modules/form-data/package.json +68 -0
- package/.cache/typescript/5.4/node_modules/mime-db/HISTORY.md +507 -0
- package/.cache/typescript/5.4/node_modules/mime-db/LICENSE +23 -0
- package/.cache/typescript/5.4/node_modules/mime-db/README.md +100 -0
- package/.cache/typescript/5.4/node_modules/mime-db/db.json +8519 -0
- package/.cache/typescript/5.4/node_modules/mime-db/index.js +12 -0
- package/.cache/typescript/5.4/node_modules/mime-db/package.json +60 -0
- package/.cache/typescript/5.4/node_modules/mime-types/HISTORY.md +397 -0
- package/.cache/typescript/5.4/node_modules/mime-types/LICENSE +23 -0
- package/.cache/typescript/5.4/node_modules/mime-types/README.md +113 -0
- package/.cache/typescript/5.4/node_modules/mime-types/index.js +188 -0
- package/.cache/typescript/5.4/node_modules/mime-types/package.json +44 -0
- package/.cache/typescript/5.4/node_modules/types-registry/README.md +2 -0
- package/.cache/typescript/5.4/node_modules/types-registry/index.json +1 -0
- package/.cache/typescript/5.4/node_modules/types-registry/package.json +20 -0
- package/.cache/typescript/5.4/node_modules/undici-types/README.md +6 -0
- package/.cache/typescript/5.4/node_modules/undici-types/agent.d.ts +31 -0
- package/.cache/typescript/5.4/node_modules/undici-types/api.d.ts +43 -0
- package/.cache/typescript/5.4/node_modules/undici-types/balanced-pool.d.ts +18 -0
- package/.cache/typescript/5.4/node_modules/undici-types/cache.d.ts +36 -0
- package/.cache/typescript/5.4/node_modules/undici-types/client.d.ts +97 -0
- package/.cache/typescript/5.4/node_modules/undici-types/connector.d.ts +34 -0
- package/.cache/typescript/5.4/node_modules/undici-types/content-type.d.ts +21 -0
- package/.cache/typescript/5.4/node_modules/undici-types/cookies.d.ts +28 -0
- package/.cache/typescript/5.4/node_modules/undici-types/diagnostics-channel.d.ts +67 -0
- package/.cache/typescript/5.4/node_modules/undici-types/dispatcher.d.ts +241 -0
- package/.cache/typescript/5.4/node_modules/undici-types/errors.d.ts +128 -0
- package/.cache/typescript/5.4/node_modules/undici-types/fetch.d.ts +209 -0
- package/.cache/typescript/5.4/node_modules/undici-types/file.d.ts +39 -0
- package/.cache/typescript/5.4/node_modules/undici-types/filereader.d.ts +54 -0
- package/.cache/typescript/5.4/node_modules/undici-types/formdata.d.ts +108 -0
- package/.cache/typescript/5.4/node_modules/undici-types/global-dispatcher.d.ts +9 -0
- package/.cache/typescript/5.4/node_modules/undici-types/global-origin.d.ts +7 -0
- package/.cache/typescript/5.4/node_modules/undici-types/handlers.d.ts +9 -0
- package/.cache/typescript/5.4/node_modules/undici-types/header.d.ts +4 -0
- package/.cache/typescript/5.4/node_modules/undici-types/index.d.ts +63 -0
- package/.cache/typescript/5.4/node_modules/undici-types/interceptors.d.ts +5 -0
- package/.cache/typescript/5.4/node_modules/undici-types/mock-agent.d.ts +50 -0
- package/.cache/typescript/5.4/node_modules/undici-types/mock-client.d.ts +25 -0
- package/.cache/typescript/5.4/node_modules/undici-types/mock-errors.d.ts +12 -0
- package/.cache/typescript/5.4/node_modules/undici-types/mock-interceptor.d.ts +93 -0
- package/.cache/typescript/5.4/node_modules/undici-types/mock-pool.d.ts +25 -0
- package/.cache/typescript/5.4/node_modules/undici-types/package.json +55 -0
- package/.cache/typescript/5.4/node_modules/undici-types/patch.d.ts +71 -0
- package/.cache/typescript/5.4/node_modules/undici-types/pool-stats.d.ts +19 -0
- package/.cache/typescript/5.4/node_modules/undici-types/pool.d.ts +28 -0
- package/.cache/typescript/5.4/node_modules/undici-types/proxy-agent.d.ts +30 -0
- package/.cache/typescript/5.4/node_modules/undici-types/readable.d.ts +61 -0
- package/.cache/typescript/5.4/node_modules/undici-types/webidl.d.ts +220 -0
- package/.cache/typescript/5.4/node_modules/undici-types/websocket.d.ts +131 -0
- package/.cache/typescript/5.4/package-lock.json +197 -0
- package/.cache/typescript/5.4/package.json +1 -0
- package/README.md +61 -2
- package/index.js +1 -1
- package/package.json +11 -2
- package/.cache/replit/modules/nodejs-20:v36-20240502-f4453db.res +0 -1
- package/.cache/replit/modules/replit:v9-20240429-0325cbb.res +0 -1
@@ -0,0 +1,1470 @@
|
|
1
|
+
/**
|
2
|
+
* The `node:test` module facilitates the creation of JavaScript tests.
|
3
|
+
* To access it:
|
4
|
+
*
|
5
|
+
* ```js
|
6
|
+
* import test from 'node:test';
|
7
|
+
* ```
|
8
|
+
*
|
9
|
+
* This module is only available under the `node:` scheme. The following will not
|
10
|
+
* work:
|
11
|
+
*
|
12
|
+
* ```js
|
13
|
+
* import test from 'test';
|
14
|
+
* ```
|
15
|
+
*
|
16
|
+
* Tests created via the `test` module consist of a single function that is
|
17
|
+
* processed in one of three ways:
|
18
|
+
*
|
19
|
+
* 1. A synchronous function that is considered failing if it throws an exception,
|
20
|
+
* and is considered passing otherwise.
|
21
|
+
* 2. A function that returns a `Promise` that is considered failing if the `Promise` rejects, and is considered passing if the `Promise` fulfills.
|
22
|
+
* 3. A function that receives a callback function. If the callback receives any
|
23
|
+
* truthy value as its first argument, the test is considered failing. If a
|
24
|
+
* falsy value is passed as the first argument to the callback, the test is
|
25
|
+
* considered passing. If the test function receives a callback function and
|
26
|
+
* also returns a `Promise`, the test will fail.
|
27
|
+
*
|
28
|
+
* The following example illustrates how tests are written using the `test` module.
|
29
|
+
*
|
30
|
+
* ```js
|
31
|
+
* test('synchronous passing test', (t) => {
|
32
|
+
* // This test passes because it does not throw an exception.
|
33
|
+
* assert.strictEqual(1, 1);
|
34
|
+
* });
|
35
|
+
*
|
36
|
+
* test('synchronous failing test', (t) => {
|
37
|
+
* // This test fails because it throws an exception.
|
38
|
+
* assert.strictEqual(1, 2);
|
39
|
+
* });
|
40
|
+
*
|
41
|
+
* test('asynchronous passing test', async (t) => {
|
42
|
+
* // This test passes because the Promise returned by the async
|
43
|
+
* // function is settled and not rejected.
|
44
|
+
* assert.strictEqual(1, 1);
|
45
|
+
* });
|
46
|
+
*
|
47
|
+
* test('asynchronous failing test', async (t) => {
|
48
|
+
* // This test fails because the Promise returned by the async
|
49
|
+
* // function is rejected.
|
50
|
+
* assert.strictEqual(1, 2);
|
51
|
+
* });
|
52
|
+
*
|
53
|
+
* test('failing test using Promises', (t) => {
|
54
|
+
* // Promises can be used directly as well.
|
55
|
+
* return new Promise((resolve, reject) => {
|
56
|
+
* setImmediate(() => {
|
57
|
+
* reject(new Error('this will cause the test to fail'));
|
58
|
+
* });
|
59
|
+
* });
|
60
|
+
* });
|
61
|
+
*
|
62
|
+
* test('callback passing test', (t, done) => {
|
63
|
+
* // done() is the callback function. When the setImmediate() runs, it invokes
|
64
|
+
* // done() with no arguments.
|
65
|
+
* setImmediate(done);
|
66
|
+
* });
|
67
|
+
*
|
68
|
+
* test('callback failing test', (t, done) => {
|
69
|
+
* // When the setImmediate() runs, done() is invoked with an Error object and
|
70
|
+
* // the test fails.
|
71
|
+
* setImmediate(() => {
|
72
|
+
* done(new Error('callback failure'));
|
73
|
+
* });
|
74
|
+
* });
|
75
|
+
* ```
|
76
|
+
*
|
77
|
+
* If any tests fail, the process exit code is set to `1`.
|
78
|
+
* @since v18.0.0, v16.17.0
|
79
|
+
* @see [source](https://github.com/nodejs/node/blob/v20.12.2/lib/test.js)
|
80
|
+
*/
|
81
|
+
declare module "node:test" {
|
82
|
+
import { Readable } from "node:stream";
|
83
|
+
import { AsyncResource } from "node:async_hooks";
|
84
|
+
/**
|
85
|
+
* **Note:** `shard` is used to horizontally parallelize test running across
|
86
|
+
* machines or processes, ideal for large-scale executions across varied
|
87
|
+
* environments. It's incompatible with `watch` mode, tailored for rapid
|
88
|
+
* code iteration by automatically rerunning tests on file changes.
|
89
|
+
*
|
90
|
+
* ```js
|
91
|
+
* import { tap } from 'node:test/reporters';
|
92
|
+
* import { run } from 'node:test';
|
93
|
+
* import process from 'node:process';
|
94
|
+
* import path from 'node:path';
|
95
|
+
*
|
96
|
+
* run({ files: [path.resolve('./tests/test.js')] })
|
97
|
+
* .compose(tap)
|
98
|
+
* .pipe(process.stdout);
|
99
|
+
* ```
|
100
|
+
* @since v18.9.0, v16.19.0
|
101
|
+
* @param options Configuration options for running tests. The following properties are supported:
|
102
|
+
*/
|
103
|
+
function run(options?: RunOptions): TestsStream;
|
104
|
+
/**
|
105
|
+
* The `test()` function is the value imported from the `test` module. Each
|
106
|
+
* invocation of this function results in reporting the test to the `TestsStream`.
|
107
|
+
*
|
108
|
+
* The `TestContext` object passed to the `fn` argument can be used to perform
|
109
|
+
* actions related to the current test. Examples include skipping the test, adding
|
110
|
+
* additional diagnostic information, or creating subtests.
|
111
|
+
*
|
112
|
+
* `test()` returns a `Promise` that fulfills once the test completes.
|
113
|
+
* if `test()` is called within a `describe()` block, it fulfills immediately.
|
114
|
+
* The return value can usually be discarded for top level tests.
|
115
|
+
* However, the return value from subtests should be used to prevent the parent
|
116
|
+
* test from finishing first and cancelling the subtest
|
117
|
+
* as shown in the following example.
|
118
|
+
*
|
119
|
+
* ```js
|
120
|
+
* test('top level test', async (t) => {
|
121
|
+
* // The setTimeout() in the following subtest would cause it to outlive its
|
122
|
+
* // parent test if 'await' is removed on the next line. Once the parent test
|
123
|
+
* // completes, it will cancel any outstanding subtests.
|
124
|
+
* await t.test('longer running subtest', async (t) => {
|
125
|
+
* return new Promise((resolve, reject) => {
|
126
|
+
* setTimeout(resolve, 1000);
|
127
|
+
* });
|
128
|
+
* });
|
129
|
+
* });
|
130
|
+
* ```
|
131
|
+
*
|
132
|
+
* The `timeout` option can be used to fail the test if it takes longer than `timeout` milliseconds to complete. However, it is not a reliable mechanism for
|
133
|
+
* canceling tests because a running test might block the application thread and
|
134
|
+
* thus prevent the scheduled cancellation.
|
135
|
+
* @since v18.0.0, v16.17.0
|
136
|
+
* @param [name='The name'] The name of the test, which is displayed when reporting test results.
|
137
|
+
* @param options Configuration options for the test. The following properties are supported:
|
138
|
+
* @param [fn='A no-op function'] The function under test. The first argument to this function is a {@link TestContext} object. If the test uses callbacks, the
|
139
|
+
* callback function is passed as the second argument.
|
140
|
+
* @return Fulfilled with `undefined` once the test completes, or immediately if the test runs within {@link describe}.
|
141
|
+
*/
|
142
|
+
function test(name?: string, fn?: TestFn): Promise<void>;
|
143
|
+
function test(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
|
144
|
+
function test(options?: TestOptions, fn?: TestFn): Promise<void>;
|
145
|
+
function test(fn?: TestFn): Promise<void>;
|
146
|
+
namespace test {
|
147
|
+
export { after, afterEach, before, beforeEach, describe, it, mock, only, run, skip, test, todo };
|
148
|
+
}
|
149
|
+
/**
|
150
|
+
* The `describe()` function imported from the `node:test` module. Each
|
151
|
+
* invocation of this function results in the creation of a Subtest.
|
152
|
+
* After invocation of top level `describe` functions,
|
153
|
+
* all top level tests and suites will execute.
|
154
|
+
* @param [name='The name'] The name of the suite, which is displayed when reporting test results.
|
155
|
+
* @param options Configuration options for the suite. supports the same options as `test([name][, options][, fn])`.
|
156
|
+
* @param [fn='A no-op function'] The function under suite declaring all subtests and subsuites. The first argument to this function is a {@link SuiteContext} object.
|
157
|
+
* @return Immediately fulfilled with `undefined`.
|
158
|
+
*/
|
159
|
+
function describe(name?: string, options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
160
|
+
function describe(name?: string, fn?: SuiteFn): Promise<void>;
|
161
|
+
function describe(options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
162
|
+
function describe(fn?: SuiteFn): Promise<void>;
|
163
|
+
namespace describe {
|
164
|
+
/**
|
165
|
+
* Shorthand for skipping a suite, same as `describe([name], { skip: true }[, fn])`.
|
166
|
+
*/
|
167
|
+
function skip(name?: string, options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
168
|
+
function skip(name?: string, fn?: SuiteFn): Promise<void>;
|
169
|
+
function skip(options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
170
|
+
function skip(fn?: SuiteFn): Promise<void>;
|
171
|
+
/**
|
172
|
+
* Shorthand for marking a suite as `TODO`, same as `describe([name], { todo: true }[, fn])`.
|
173
|
+
*/
|
174
|
+
function todo(name?: string, options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
175
|
+
function todo(name?: string, fn?: SuiteFn): Promise<void>;
|
176
|
+
function todo(options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
177
|
+
function todo(fn?: SuiteFn): Promise<void>;
|
178
|
+
/**
|
179
|
+
* Shorthand for marking a suite as `only`, same as `describe([name], { only: true }[, fn])`.
|
180
|
+
* @since v18.15.0
|
181
|
+
*/
|
182
|
+
function only(name?: string, options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
183
|
+
function only(name?: string, fn?: SuiteFn): Promise<void>;
|
184
|
+
function only(options?: TestOptions, fn?: SuiteFn): Promise<void>;
|
185
|
+
function only(fn?: SuiteFn): Promise<void>;
|
186
|
+
}
|
187
|
+
/**
|
188
|
+
* Shorthand for `test()`.
|
189
|
+
*
|
190
|
+
* The `it()` function is imported from the `node:test` module.
|
191
|
+
* @since v18.6.0, v16.17.0
|
192
|
+
*/
|
193
|
+
function it(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
|
194
|
+
function it(name?: string, fn?: TestFn): Promise<void>;
|
195
|
+
function it(options?: TestOptions, fn?: TestFn): Promise<void>;
|
196
|
+
function it(fn?: TestFn): Promise<void>;
|
197
|
+
namespace it {
|
198
|
+
/**
|
199
|
+
* Shorthand for skipping a test, same as `it([name], { skip: true }[, fn])`.
|
200
|
+
*/
|
201
|
+
function skip(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
|
202
|
+
function skip(name?: string, fn?: TestFn): Promise<void>;
|
203
|
+
function skip(options?: TestOptions, fn?: TestFn): Promise<void>;
|
204
|
+
function skip(fn?: TestFn): Promise<void>;
|
205
|
+
/**
|
206
|
+
* Shorthand for marking a test as `TODO`, same as `it([name], { todo: true }[, fn])`.
|
207
|
+
*/
|
208
|
+
function todo(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
|
209
|
+
function todo(name?: string, fn?: TestFn): Promise<void>;
|
210
|
+
function todo(options?: TestOptions, fn?: TestFn): Promise<void>;
|
211
|
+
function todo(fn?: TestFn): Promise<void>;
|
212
|
+
/**
|
213
|
+
* Shorthand for marking a test as `only`, same as `it([name], { only: true }[, fn])`.
|
214
|
+
* @since v18.15.0
|
215
|
+
*/
|
216
|
+
function only(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
|
217
|
+
function only(name?: string, fn?: TestFn): Promise<void>;
|
218
|
+
function only(options?: TestOptions, fn?: TestFn): Promise<void>;
|
219
|
+
function only(fn?: TestFn): Promise<void>;
|
220
|
+
}
|
221
|
+
/**
|
222
|
+
* Shorthand for skipping a test, same as `test([name], { skip: true }[, fn])`.
|
223
|
+
* @since v20.2.0
|
224
|
+
*/
|
225
|
+
function skip(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
|
226
|
+
function skip(name?: string, fn?: TestFn): Promise<void>;
|
227
|
+
function skip(options?: TestOptions, fn?: TestFn): Promise<void>;
|
228
|
+
function skip(fn?: TestFn): Promise<void>;
|
229
|
+
/**
|
230
|
+
* Shorthand for marking a test as `TODO`, same as `test([name], { todo: true }[, fn])`.
|
231
|
+
* @since v20.2.0
|
232
|
+
*/
|
233
|
+
function todo(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
|
234
|
+
function todo(name?: string, fn?: TestFn): Promise<void>;
|
235
|
+
function todo(options?: TestOptions, fn?: TestFn): Promise<void>;
|
236
|
+
function todo(fn?: TestFn): Promise<void>;
|
237
|
+
/**
|
238
|
+
* Shorthand for marking a test as `only`, same as `test([name], { only: true }[, fn])`.
|
239
|
+
* @since v20.2.0
|
240
|
+
*/
|
241
|
+
function only(name?: string, options?: TestOptions, fn?: TestFn): Promise<void>;
|
242
|
+
function only(name?: string, fn?: TestFn): Promise<void>;
|
243
|
+
function only(options?: TestOptions, fn?: TestFn): Promise<void>;
|
244
|
+
function only(fn?: TestFn): Promise<void>;
|
245
|
+
/**
|
246
|
+
* The type of a function under test. The first argument to this function is a
|
247
|
+
* {@link TestContext} object. If the test uses callbacks, the callback function is passed as
|
248
|
+
* the second argument.
|
249
|
+
*/
|
250
|
+
type TestFn = (t: TestContext, done: (result?: any) => void) => void | Promise<void>;
|
251
|
+
/**
|
252
|
+
* The type of a function under Suite.
|
253
|
+
*/
|
254
|
+
type SuiteFn = (s: SuiteContext) => void | Promise<void>;
|
255
|
+
interface TestShard {
|
256
|
+
/**
|
257
|
+
* A positive integer between 1 and `<total>` that specifies the index of the shard to run.
|
258
|
+
*/
|
259
|
+
index: number;
|
260
|
+
/**
|
261
|
+
* A positive integer that specifies the total number of shards to split the test files to.
|
262
|
+
*/
|
263
|
+
total: number;
|
264
|
+
}
|
265
|
+
interface RunOptions {
|
266
|
+
/**
|
267
|
+
* If a number is provided, then that many files would run in parallel.
|
268
|
+
* If truthy, it would run (number of cpu cores - 1) files in parallel.
|
269
|
+
* If falsy, it would only run one file at a time.
|
270
|
+
* If unspecified, subtests inherit this value from their parent.
|
271
|
+
* @default true
|
272
|
+
*/
|
273
|
+
concurrency?: number | boolean | undefined;
|
274
|
+
/**
|
275
|
+
* An array containing the list of files to run.
|
276
|
+
* If unspecified, the test runner execution model will be used.
|
277
|
+
*/
|
278
|
+
files?: readonly string[] | undefined;
|
279
|
+
/**
|
280
|
+
* Allows aborting an in-progress test execution.
|
281
|
+
* @default undefined
|
282
|
+
*/
|
283
|
+
signal?: AbortSignal | undefined;
|
284
|
+
/**
|
285
|
+
* A number of milliseconds the test will fail after.
|
286
|
+
* If unspecified, subtests inherit this value from their parent.
|
287
|
+
* @default Infinity
|
288
|
+
*/
|
289
|
+
timeout?: number | undefined;
|
290
|
+
/**
|
291
|
+
* Sets inspector port of test child process.
|
292
|
+
* If a nullish value is provided, each process gets its own port,
|
293
|
+
* incremented from the primary's `process.debugPort`.
|
294
|
+
*/
|
295
|
+
inspectPort?: number | (() => number) | undefined;
|
296
|
+
/**
|
297
|
+
* That can be used to only run tests whose name matches the provided pattern.
|
298
|
+
* Test name patterns are interpreted as JavaScript regular expressions.
|
299
|
+
* For each test that is executed, any corresponding test hooks, such as `beforeEach()`, are also run.
|
300
|
+
*/
|
301
|
+
testNamePatterns?: string | RegExp | string[] | RegExp[];
|
302
|
+
/**
|
303
|
+
* If truthy, the test context will only run tests that have the `only` option set
|
304
|
+
*/
|
305
|
+
only?: boolean;
|
306
|
+
/**
|
307
|
+
* A function that accepts the TestsStream instance and can be used to setup listeners before any tests are run.
|
308
|
+
*/
|
309
|
+
setup?: (root: Test) => void | Promise<void>;
|
310
|
+
/**
|
311
|
+
* Whether to run in watch mode or not.
|
312
|
+
* @default false
|
313
|
+
*/
|
314
|
+
watch?: boolean | undefined;
|
315
|
+
/**
|
316
|
+
* Running tests in a specific shard.
|
317
|
+
* @default undefined
|
318
|
+
*/
|
319
|
+
shard?: TestShard | undefined;
|
320
|
+
}
|
321
|
+
class Test extends AsyncResource {
|
322
|
+
concurrency: number;
|
323
|
+
nesting: number;
|
324
|
+
only: boolean;
|
325
|
+
reporter: TestsStream;
|
326
|
+
runOnlySubtests: boolean;
|
327
|
+
testNumber: number;
|
328
|
+
timeout: number | null;
|
329
|
+
}
|
330
|
+
/**
|
331
|
+
* A successful call to `run()` method will return a new `TestsStream` object, streaming a series of events representing the execution of the tests. `TestsStream` will emit events, in the
|
332
|
+
* order of the tests definition
|
333
|
+
* @since v18.9.0, v16.19.0
|
334
|
+
*/
|
335
|
+
class TestsStream extends Readable implements NodeJS.ReadableStream {
|
336
|
+
addListener(event: "test:diagnostic", listener: (data: DiagnosticData) => void): this;
|
337
|
+
addListener(event: "test:fail", listener: (data: TestFail) => void): this;
|
338
|
+
addListener(event: "test:pass", listener: (data: TestPass) => void): this;
|
339
|
+
addListener(event: "test:plan", listener: (data: TestPlan) => void): this;
|
340
|
+
addListener(event: "test:start", listener: (data: TestStart) => void): this;
|
341
|
+
addListener(event: "test:stderr", listener: (data: TestStderr) => void): this;
|
342
|
+
addListener(event: "test:stdout", listener: (data: TestStdout) => void): this;
|
343
|
+
addListener(event: string, listener: (...args: any[]) => void): this;
|
344
|
+
emit(event: "test:diagnostic", data: DiagnosticData): boolean;
|
345
|
+
emit(event: "test:fail", data: TestFail): boolean;
|
346
|
+
emit(event: "test:pass", data: TestPass): boolean;
|
347
|
+
emit(event: "test:plan", data: TestPlan): boolean;
|
348
|
+
emit(event: "test:start", data: TestStart): boolean;
|
349
|
+
emit(event: "test:stderr", data: TestStderr): boolean;
|
350
|
+
emit(event: "test:stdout", data: TestStdout): boolean;
|
351
|
+
emit(event: string | symbol, ...args: any[]): boolean;
|
352
|
+
on(event: "test:diagnostic", listener: (data: DiagnosticData) => void): this;
|
353
|
+
on(event: "test:fail", listener: (data: TestFail) => void): this;
|
354
|
+
on(event: "test:pass", listener: (data: TestPass) => void): this;
|
355
|
+
on(event: "test:plan", listener: (data: TestPlan) => void): this;
|
356
|
+
on(event: "test:start", listener: (data: TestStart) => void): this;
|
357
|
+
on(event: "test:stderr", listener: (data: TestStderr) => void): this;
|
358
|
+
on(event: "test:stdout", listener: (data: TestStdout) => void): this;
|
359
|
+
on(event: string, listener: (...args: any[]) => void): this;
|
360
|
+
once(event: "test:diagnostic", listener: (data: DiagnosticData) => void): this;
|
361
|
+
once(event: "test:fail", listener: (data: TestFail) => void): this;
|
362
|
+
once(event: "test:pass", listener: (data: TestPass) => void): this;
|
363
|
+
once(event: "test:plan", listener: (data: TestPlan) => void): this;
|
364
|
+
once(event: "test:start", listener: (data: TestStart) => void): this;
|
365
|
+
once(event: "test:stderr", listener: (data: TestStderr) => void): this;
|
366
|
+
once(event: "test:stdout", listener: (data: TestStdout) => void): this;
|
367
|
+
once(event: string, listener: (...args: any[]) => void): this;
|
368
|
+
prependListener(event: "test:diagnostic", listener: (data: DiagnosticData) => void): this;
|
369
|
+
prependListener(event: "test:fail", listener: (data: TestFail) => void): this;
|
370
|
+
prependListener(event: "test:pass", listener: (data: TestPass) => void): this;
|
371
|
+
prependListener(event: "test:plan", listener: (data: TestPlan) => void): this;
|
372
|
+
prependListener(event: "test:start", listener: (data: TestStart) => void): this;
|
373
|
+
prependListener(event: "test:stderr", listener: (data: TestStderr) => void): this;
|
374
|
+
prependListener(event: "test:stdout", listener: (data: TestStdout) => void): this;
|
375
|
+
prependListener(event: string, listener: (...args: any[]) => void): this;
|
376
|
+
prependOnceListener(event: "test:diagnostic", listener: (data: DiagnosticData) => void): this;
|
377
|
+
prependOnceListener(event: "test:fail", listener: (data: TestFail) => void): this;
|
378
|
+
prependOnceListener(event: "test:pass", listener: (data: TestPass) => void): this;
|
379
|
+
prependOnceListener(event: "test:plan", listener: (data: TestPlan) => void): this;
|
380
|
+
prependOnceListener(event: "test:start", listener: (data: TestStart) => void): this;
|
381
|
+
prependOnceListener(event: "test:stderr", listener: (data: TestStderr) => void): this;
|
382
|
+
prependOnceListener(event: "test:stdout", listener: (data: TestStdout) => void): this;
|
383
|
+
prependOnceListener(event: string, listener: (...args: any[]) => void): this;
|
384
|
+
}
|
385
|
+
/**
|
386
|
+
* An instance of `TestContext` is passed to each test function in order to
|
387
|
+
* interact with the test runner. However, the `TestContext` constructor is not
|
388
|
+
* exposed as part of the API.
|
389
|
+
* @since v18.0.0, v16.17.0
|
390
|
+
*/
|
391
|
+
class TestContext {
|
392
|
+
/**
|
393
|
+
* This function is used to create a hook running before subtest of the current test.
|
394
|
+
* @param fn The hook function. If the hook uses callbacks, the callback function is passed as
|
395
|
+
* the second argument. **Default:** A no-op function.
|
396
|
+
* @param options Configuration options for the hook.
|
397
|
+
* @since v20.1.0
|
398
|
+
*/
|
399
|
+
before: typeof before;
|
400
|
+
/**
|
401
|
+
* This function is used to create a hook running before each subtest of the current test.
|
402
|
+
* @param fn The hook function. If the hook uses callbacks, the callback function is passed as
|
403
|
+
* the second argument. **Default:** A no-op function.
|
404
|
+
* @param options Configuration options for the hook.
|
405
|
+
* @since v18.8.0
|
406
|
+
*/
|
407
|
+
beforeEach: typeof beforeEach;
|
408
|
+
/**
|
409
|
+
* This function is used to create a hook that runs after the current test finishes.
|
410
|
+
* @param [fn='A no-op function'] The hook function. If the hook uses callbacks, the callback function is passed as
|
411
|
+
* the second argument. Default: A no-op function.
|
412
|
+
* @param options Configuration options for the hook.
|
413
|
+
* @since v18.13.0
|
414
|
+
*/
|
415
|
+
after: typeof after;
|
416
|
+
/**
|
417
|
+
* This function is used to create a hook running after each subtest of the current test.
|
418
|
+
* @param fn The hook function. If the hook uses callbacks, the callback function is passed as
|
419
|
+
* the second argument. **Default:** A no-op function.
|
420
|
+
* @param options Configuration options for the hook.
|
421
|
+
* @since v18.8.0
|
422
|
+
*/
|
423
|
+
afterEach: typeof afterEach;
|
424
|
+
/**
|
425
|
+
* This function is used to write diagnostics to the output. Any diagnostic
|
426
|
+
* information is included at the end of the test's results. This function does
|
427
|
+
* not return a value.
|
428
|
+
*
|
429
|
+
* ```js
|
430
|
+
* test('top level test', (t) => {
|
431
|
+
* t.diagnostic('A diagnostic message');
|
432
|
+
* });
|
433
|
+
* ```
|
434
|
+
* @since v18.0.0, v16.17.0
|
435
|
+
* @param message Message to be reported.
|
436
|
+
*/
|
437
|
+
diagnostic(message: string): void;
|
438
|
+
/**
|
439
|
+
* The name of the test.
|
440
|
+
* @since v18.8.0, v16.18.0
|
441
|
+
*/
|
442
|
+
readonly name: string;
|
443
|
+
/**
|
444
|
+
* If `shouldRunOnlyTests` is truthy, the test context will only run tests that
|
445
|
+
* have the `only` option set. Otherwise, all tests are run. If Node.js was not
|
446
|
+
* started with the `--test-only` command-line option, this function is a
|
447
|
+
* no-op.
|
448
|
+
*
|
449
|
+
* ```js
|
450
|
+
* test('top level test', (t) => {
|
451
|
+
* // The test context can be set to run subtests with the 'only' option.
|
452
|
+
* t.runOnly(true);
|
453
|
+
* return Promise.all([
|
454
|
+
* t.test('this subtest is now skipped'),
|
455
|
+
* t.test('this subtest is run', { only: true }),
|
456
|
+
* ]);
|
457
|
+
* });
|
458
|
+
* ```
|
459
|
+
* @since v18.0.0, v16.17.0
|
460
|
+
* @param shouldRunOnlyTests Whether or not to run `only` tests.
|
461
|
+
*/
|
462
|
+
runOnly(shouldRunOnlyTests: boolean): void;
|
463
|
+
/**
|
464
|
+
* ```js
|
465
|
+
* test('top level test', async (t) => {
|
466
|
+
* await fetch('some/uri', { signal: t.signal });
|
467
|
+
* });
|
468
|
+
* ```
|
469
|
+
* @since v18.7.0, v16.17.0
|
470
|
+
*/
|
471
|
+
readonly signal: AbortSignal;
|
472
|
+
/**
|
473
|
+
* This function causes the test's output to indicate the test as skipped. If `message` is provided, it is included in the output. Calling `skip()` does
|
474
|
+
* not terminate execution of the test function. This function does not return a
|
475
|
+
* value.
|
476
|
+
*
|
477
|
+
* ```js
|
478
|
+
* test('top level test', (t) => {
|
479
|
+
* // Make sure to return here as well if the test contains additional logic.
|
480
|
+
* t.skip('this is skipped');
|
481
|
+
* });
|
482
|
+
* ```
|
483
|
+
* @since v18.0.0, v16.17.0
|
484
|
+
* @param message Optional skip message.
|
485
|
+
*/
|
486
|
+
skip(message?: string): void;
|
487
|
+
/**
|
488
|
+
* This function adds a `TODO` directive to the test's output. If `message` is
|
489
|
+
* provided, it is included in the output. Calling `todo()` does not terminate
|
490
|
+
* execution of the test function. This function does not return a value.
|
491
|
+
*
|
492
|
+
* ```js
|
493
|
+
* test('top level test', (t) => {
|
494
|
+
* // This test is marked as `TODO`
|
495
|
+
* t.todo('this is a todo');
|
496
|
+
* });
|
497
|
+
* ```
|
498
|
+
* @since v18.0.0, v16.17.0
|
499
|
+
* @param message Optional `TODO` message.
|
500
|
+
*/
|
501
|
+
todo(message?: string): void;
|
502
|
+
/**
|
503
|
+
* This function is used to create subtests under the current test. This function behaves in
|
504
|
+
* the same fashion as the top level {@link test} function.
|
505
|
+
* @since v18.0.0
|
506
|
+
* @param name The name of the test, which is displayed when reporting test results.
|
507
|
+
* Default: The `name` property of fn, or `'<anonymous>'` if `fn` does not have a name.
|
508
|
+
* @param options Configuration options for the test
|
509
|
+
* @param fn The function under test. This first argument to this function is a
|
510
|
+
* {@link TestContext} object. If the test uses callbacks, the callback function is
|
511
|
+
* passed as the second argument. **Default:** A no-op function.
|
512
|
+
* @returns A {@link Promise} resolved with `undefined` once the test completes.
|
513
|
+
*/
|
514
|
+
test: typeof test;
|
515
|
+
/**
|
516
|
+
* Each test provides its own MockTracker instance.
|
517
|
+
*/
|
518
|
+
readonly mock: MockTracker;
|
519
|
+
}
|
520
|
+
|
521
|
+
/**
|
522
|
+
* An instance of `SuiteContext` is passed to each suite function in order to
|
523
|
+
* interact with the test runner. However, the `SuiteContext` constructor is not
|
524
|
+
* exposed as part of the API.
|
525
|
+
* @since v18.7.0, v16.17.0
|
526
|
+
*/
|
527
|
+
class SuiteContext {
|
528
|
+
/**
|
529
|
+
* The name of the suite.
|
530
|
+
* @since v18.8.0, v16.18.0
|
531
|
+
*/
|
532
|
+
readonly name: string;
|
533
|
+
/**
|
534
|
+
* Can be used to abort test subtasks when the test has been aborted.
|
535
|
+
* @since v18.7.0, v16.17.0
|
536
|
+
*/
|
537
|
+
readonly signal: AbortSignal;
|
538
|
+
}
|
539
|
+
interface TestOptions {
|
540
|
+
/**
|
541
|
+
* If a number is provided, then that many tests would run in parallel.
|
542
|
+
* If truthy, it would run (number of cpu cores - 1) tests in parallel.
|
543
|
+
* For subtests, it will be `Infinity` tests in parallel.
|
544
|
+
* If falsy, it would only run one test at a time.
|
545
|
+
* If unspecified, subtests inherit this value from their parent.
|
546
|
+
* @default false
|
547
|
+
*/
|
548
|
+
concurrency?: number | boolean | undefined;
|
549
|
+
/**
|
550
|
+
* If truthy, and the test context is configured to run `only` tests, then this test will be
|
551
|
+
* run. Otherwise, the test is skipped.
|
552
|
+
* @default false
|
553
|
+
*/
|
554
|
+
only?: boolean | undefined;
|
555
|
+
/**
|
556
|
+
* Allows aborting an in-progress test.
|
557
|
+
* @since v18.8.0
|
558
|
+
*/
|
559
|
+
signal?: AbortSignal | undefined;
|
560
|
+
/**
|
561
|
+
* If truthy, the test is skipped. If a string is provided, that string is displayed in the
|
562
|
+
* test results as the reason for skipping the test.
|
563
|
+
* @default false
|
564
|
+
*/
|
565
|
+
skip?: boolean | string | undefined;
|
566
|
+
/**
|
567
|
+
* A number of milliseconds the test will fail after. If unspecified, subtests inherit this
|
568
|
+
* value from their parent.
|
569
|
+
* @default Infinity
|
570
|
+
* @since v18.7.0
|
571
|
+
*/
|
572
|
+
timeout?: number | undefined;
|
573
|
+
/**
|
574
|
+
* If truthy, the test marked as `TODO`. If a string is provided, that string is displayed in
|
575
|
+
* the test results as the reason why the test is `TODO`.
|
576
|
+
* @default false
|
577
|
+
*/
|
578
|
+
todo?: boolean | string | undefined;
|
579
|
+
}
|
580
|
+
/**
|
581
|
+
* This function is used to create a hook running before running a suite.
|
582
|
+
*
|
583
|
+
* ```js
|
584
|
+
* describe('tests', async () => {
|
585
|
+
* before(() => console.log('about to run some test'));
|
586
|
+
* it('is a subtest', () => {
|
587
|
+
* assert.ok('some relevant assertion here');
|
588
|
+
* });
|
589
|
+
* });
|
590
|
+
* ```
|
591
|
+
* @since v18.8.0, v16.18.0
|
592
|
+
* @param [fn='A no-op function'] The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
|
593
|
+
* @param options Configuration options for the hook. The following properties are supported:
|
594
|
+
*/
|
595
|
+
function before(fn?: HookFn, options?: HookOptions): void;
|
596
|
+
/**
|
597
|
+
* This function is used to create a hook running after running a suite.
|
598
|
+
*
|
599
|
+
* ```js
|
600
|
+
* describe('tests', async () => {
|
601
|
+
* after(() => console.log('finished running tests'));
|
602
|
+
* it('is a subtest', () => {
|
603
|
+
* assert.ok('some relevant assertion here');
|
604
|
+
* });
|
605
|
+
* });
|
606
|
+
* ```
|
607
|
+
* @since v18.8.0, v16.18.0
|
608
|
+
* @param [fn='A no-op function'] The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
|
609
|
+
* @param options Configuration options for the hook. The following properties are supported:
|
610
|
+
*/
|
611
|
+
function after(fn?: HookFn, options?: HookOptions): void;
|
612
|
+
/**
|
613
|
+
* This function is used to create a hook running
|
614
|
+
* before each subtest of the current suite.
|
615
|
+
*
|
616
|
+
* ```js
|
617
|
+
* describe('tests', async () => {
|
618
|
+
* beforeEach(() => console.log('about to run a test'));
|
619
|
+
* it('is a subtest', () => {
|
620
|
+
* assert.ok('some relevant assertion here');
|
621
|
+
* });
|
622
|
+
* });
|
623
|
+
* ```
|
624
|
+
* @since v18.8.0, v16.18.0
|
625
|
+
* @param [fn='A no-op function'] The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
|
626
|
+
* @param options Configuration options for the hook. The following properties are supported:
|
627
|
+
*/
|
628
|
+
function beforeEach(fn?: HookFn, options?: HookOptions): void;
|
629
|
+
/**
|
630
|
+
* This function is used to create a hook running
|
631
|
+
* after each subtest of the current test.
|
632
|
+
*
|
633
|
+
* ```js
|
634
|
+
* describe('tests', async () => {
|
635
|
+
* afterEach(() => console.log('finished running a test'));
|
636
|
+
* it('is a subtest', () => {
|
637
|
+
* assert.ok('some relevant assertion here');
|
638
|
+
* });
|
639
|
+
* });
|
640
|
+
* ```
|
641
|
+
* @since v18.8.0, v16.18.0
|
642
|
+
* @param [fn='A no-op function'] The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
|
643
|
+
* @param options Configuration options for the hook. The following properties are supported:
|
644
|
+
*/
|
645
|
+
function afterEach(fn?: HookFn, options?: HookOptions): void;
|
646
|
+
/**
|
647
|
+
* The hook function. If the hook uses callbacks, the callback function is passed as the
|
648
|
+
* second argument.
|
649
|
+
*/
|
650
|
+
type HookFn = (s: SuiteContext, done: (result?: any) => void) => any;
|
651
|
+
/**
|
652
|
+
* Configuration options for hooks.
|
653
|
+
* @since v18.8.0
|
654
|
+
*/
|
655
|
+
interface HookOptions {
|
656
|
+
/**
|
657
|
+
* Allows aborting an in-progress hook.
|
658
|
+
*/
|
659
|
+
signal?: AbortSignal | undefined;
|
660
|
+
/**
|
661
|
+
* A number of milliseconds the hook will fail after. If unspecified, subtests inherit this
|
662
|
+
* value from their parent.
|
663
|
+
* @default Infinity
|
664
|
+
*/
|
665
|
+
timeout?: number | undefined;
|
666
|
+
}
|
667
|
+
interface MockFunctionOptions {
|
668
|
+
/**
|
669
|
+
* The number of times that the mock will use the behavior of `implementation`.
|
670
|
+
* Once the mock function has been called `times` times,
|
671
|
+
* it will automatically restore the behavior of `original`.
|
672
|
+
* This value must be an integer greater than zero.
|
673
|
+
* @default Infinity
|
674
|
+
*/
|
675
|
+
times?: number | undefined;
|
676
|
+
}
|
677
|
+
interface MockMethodOptions extends MockFunctionOptions {
|
678
|
+
/**
|
679
|
+
* If `true`, `object[methodName]` is treated as a getter.
|
680
|
+
* This option cannot be used with the `setter` option.
|
681
|
+
*/
|
682
|
+
getter?: boolean | undefined;
|
683
|
+
/**
|
684
|
+
* If `true`, `object[methodName]` is treated as a setter.
|
685
|
+
* This option cannot be used with the `getter` option.
|
686
|
+
*/
|
687
|
+
setter?: boolean | undefined;
|
688
|
+
}
|
689
|
+
type Mock<F extends Function> = F & {
|
690
|
+
mock: MockFunctionContext<F>;
|
691
|
+
};
|
692
|
+
type NoOpFunction = (...args: any[]) => undefined;
|
693
|
+
type FunctionPropertyNames<T> = {
|
694
|
+
[K in keyof T]: T[K] extends Function ? K : never;
|
695
|
+
}[keyof T];
|
696
|
+
/**
|
697
|
+
* The `MockTracker` class is used to manage mocking functionality. The test runner
|
698
|
+
* module provides a top level `mock` export which is a `MockTracker` instance.
|
699
|
+
* Each test also provides its own `MockTracker` instance via the test context's `mock` property.
|
700
|
+
* @since v19.1.0, v18.13.0
|
701
|
+
*/
|
702
|
+
class MockTracker {
|
703
|
+
/**
|
704
|
+
* This function is used to create a mock function.
|
705
|
+
*
|
706
|
+
* The following example creates a mock function that increments a counter by one
|
707
|
+
* on each invocation. The `times` option is used to modify the mock behavior such
|
708
|
+
* that the first two invocations add two to the counter instead of one.
|
709
|
+
*
|
710
|
+
* ```js
|
711
|
+
* test('mocks a counting function', (t) => {
|
712
|
+
* let cnt = 0;
|
713
|
+
*
|
714
|
+
* function addOne() {
|
715
|
+
* cnt++;
|
716
|
+
* return cnt;
|
717
|
+
* }
|
718
|
+
*
|
719
|
+
* function addTwo() {
|
720
|
+
* cnt += 2;
|
721
|
+
* return cnt;
|
722
|
+
* }
|
723
|
+
*
|
724
|
+
* const fn = t.mock.fn(addOne, addTwo, { times: 2 });
|
725
|
+
*
|
726
|
+
* assert.strictEqual(fn(), 2);
|
727
|
+
* assert.strictEqual(fn(), 4);
|
728
|
+
* assert.strictEqual(fn(), 5);
|
729
|
+
* assert.strictEqual(fn(), 6);
|
730
|
+
* });
|
731
|
+
* ```
|
732
|
+
* @since v19.1.0, v18.13.0
|
733
|
+
* @param [original='A no-op function'] An optional function to create a mock on.
|
734
|
+
* @param implementation An optional function used as the mock implementation for `original`. This is useful for creating mocks that exhibit one behavior for a specified number of calls and
|
735
|
+
* then restore the behavior of `original`.
|
736
|
+
* @param options Optional configuration options for the mock function. The following properties are supported:
|
737
|
+
* @return The mocked function. The mocked function contains a special `mock` property, which is an instance of {@link MockFunctionContext}, and can be used for inspecting and changing the
|
738
|
+
* behavior of the mocked function.
|
739
|
+
*/
|
740
|
+
fn<F extends Function = NoOpFunction>(original?: F, options?: MockFunctionOptions): Mock<F>;
|
741
|
+
fn<F extends Function = NoOpFunction, Implementation extends Function = F>(
|
742
|
+
original?: F,
|
743
|
+
implementation?: Implementation,
|
744
|
+
options?: MockFunctionOptions,
|
745
|
+
): Mock<F | Implementation>;
|
746
|
+
/**
|
747
|
+
* This function is used to create a mock on an existing object method. The
|
748
|
+
* following example demonstrates how a mock is created on an existing object
|
749
|
+
* method.
|
750
|
+
*
|
751
|
+
* ```js
|
752
|
+
* test('spies on an object method', (t) => {
|
753
|
+
* const number = {
|
754
|
+
* value: 5,
|
755
|
+
* subtract(a) {
|
756
|
+
* return this.value - a;
|
757
|
+
* },
|
758
|
+
* };
|
759
|
+
*
|
760
|
+
* t.mock.method(number, 'subtract');
|
761
|
+
* assert.strictEqual(number.subtract.mock.calls.length, 0);
|
762
|
+
* assert.strictEqual(number.subtract(3), 2);
|
763
|
+
* assert.strictEqual(number.subtract.mock.calls.length, 1);
|
764
|
+
*
|
765
|
+
* const call = number.subtract.mock.calls[0];
|
766
|
+
*
|
767
|
+
* assert.deepStrictEqual(call.arguments, [3]);
|
768
|
+
* assert.strictEqual(call.result, 2);
|
769
|
+
* assert.strictEqual(call.error, undefined);
|
770
|
+
* assert.strictEqual(call.target, undefined);
|
771
|
+
* assert.strictEqual(call.this, number);
|
772
|
+
* });
|
773
|
+
* ```
|
774
|
+
* @since v19.1.0, v18.13.0
|
775
|
+
* @param object The object whose method is being mocked.
|
776
|
+
* @param methodName The identifier of the method on `object` to mock. If `object[methodName]` is not a function, an error is thrown.
|
777
|
+
* @param implementation An optional function used as the mock implementation for `object[methodName]`.
|
778
|
+
* @param options Optional configuration options for the mock method. The following properties are supported:
|
779
|
+
* @return The mocked method. The mocked method contains a special `mock` property, which is an instance of {@link MockFunctionContext}, and can be used for inspecting and changing the
|
780
|
+
* behavior of the mocked method.
|
781
|
+
*/
|
782
|
+
method<
|
783
|
+
MockedObject extends object,
|
784
|
+
MethodName extends FunctionPropertyNames<MockedObject>,
|
785
|
+
>(
|
786
|
+
object: MockedObject,
|
787
|
+
methodName: MethodName,
|
788
|
+
options?: MockFunctionOptions,
|
789
|
+
): MockedObject[MethodName] extends Function ? Mock<MockedObject[MethodName]>
|
790
|
+
: never;
|
791
|
+
method<
|
792
|
+
MockedObject extends object,
|
793
|
+
MethodName extends FunctionPropertyNames<MockedObject>,
|
794
|
+
Implementation extends Function,
|
795
|
+
>(
|
796
|
+
object: MockedObject,
|
797
|
+
methodName: MethodName,
|
798
|
+
implementation: Implementation,
|
799
|
+
options?: MockFunctionOptions,
|
800
|
+
): MockedObject[MethodName] extends Function ? Mock<MockedObject[MethodName] | Implementation>
|
801
|
+
: never;
|
802
|
+
method<MockedObject extends object>(
|
803
|
+
object: MockedObject,
|
804
|
+
methodName: keyof MockedObject,
|
805
|
+
options: MockMethodOptions,
|
806
|
+
): Mock<Function>;
|
807
|
+
method<MockedObject extends object>(
|
808
|
+
object: MockedObject,
|
809
|
+
methodName: keyof MockedObject,
|
810
|
+
implementation: Function,
|
811
|
+
options: MockMethodOptions,
|
812
|
+
): Mock<Function>;
|
813
|
+
|
814
|
+
/**
|
815
|
+
* This function is syntax sugar for `MockTracker.method` with `options.getter` set to `true`.
|
816
|
+
* @since v19.3.0, v18.13.0
|
817
|
+
*/
|
818
|
+
getter<
|
819
|
+
MockedObject extends object,
|
820
|
+
MethodName extends keyof MockedObject,
|
821
|
+
>(
|
822
|
+
object: MockedObject,
|
823
|
+
methodName: MethodName,
|
824
|
+
options?: MockFunctionOptions,
|
825
|
+
): Mock<() => MockedObject[MethodName]>;
|
826
|
+
getter<
|
827
|
+
MockedObject extends object,
|
828
|
+
MethodName extends keyof MockedObject,
|
829
|
+
Implementation extends Function,
|
830
|
+
>(
|
831
|
+
object: MockedObject,
|
832
|
+
methodName: MethodName,
|
833
|
+
implementation?: Implementation,
|
834
|
+
options?: MockFunctionOptions,
|
835
|
+
): Mock<(() => MockedObject[MethodName]) | Implementation>;
|
836
|
+
/**
|
837
|
+
* This function is syntax sugar for `MockTracker.method` with `options.setter` set to `true`.
|
838
|
+
* @since v19.3.0, v18.13.0
|
839
|
+
*/
|
840
|
+
setter<
|
841
|
+
MockedObject extends object,
|
842
|
+
MethodName extends keyof MockedObject,
|
843
|
+
>(
|
844
|
+
object: MockedObject,
|
845
|
+
methodName: MethodName,
|
846
|
+
options?: MockFunctionOptions,
|
847
|
+
): Mock<(value: MockedObject[MethodName]) => void>;
|
848
|
+
setter<
|
849
|
+
MockedObject extends object,
|
850
|
+
MethodName extends keyof MockedObject,
|
851
|
+
Implementation extends Function,
|
852
|
+
>(
|
853
|
+
object: MockedObject,
|
854
|
+
methodName: MethodName,
|
855
|
+
implementation?: Implementation,
|
856
|
+
options?: MockFunctionOptions,
|
857
|
+
): Mock<((value: MockedObject[MethodName]) => void) | Implementation>;
|
858
|
+
/**
|
859
|
+
* This function restores the default behavior of all mocks that were previously
|
860
|
+
* created by this `MockTracker` and disassociates the mocks from the `MockTracker` instance. Once disassociated, the mocks can still be used, but the `MockTracker` instance can no longer be
|
861
|
+
* used to reset their behavior or
|
862
|
+
* otherwise interact with them.
|
863
|
+
*
|
864
|
+
* After each test completes, this function is called on the test context's `MockTracker`. If the global `MockTracker` is used extensively, calling this
|
865
|
+
* function manually is recommended.
|
866
|
+
* @since v19.1.0, v18.13.0
|
867
|
+
*/
|
868
|
+
reset(): void;
|
869
|
+
/**
|
870
|
+
* This function restores the default behavior of all mocks that were previously
|
871
|
+
* created by this `MockTracker`. Unlike `mock.reset()`, `mock.restoreAll()` does
|
872
|
+
* not disassociate the mocks from the `MockTracker` instance.
|
873
|
+
* @since v19.1.0, v18.13.0
|
874
|
+
*/
|
875
|
+
restoreAll(): void;
|
876
|
+
timers: MockTimers;
|
877
|
+
}
|
878
|
+
const mock: MockTracker;
|
879
|
+
interface MockFunctionCall<
|
880
|
+
F extends Function,
|
881
|
+
ReturnType = F extends (...args: any) => infer T ? T
|
882
|
+
: F extends abstract new(...args: any) => infer T ? T
|
883
|
+
: unknown,
|
884
|
+
Args = F extends (...args: infer Y) => any ? Y
|
885
|
+
: F extends abstract new(...args: infer Y) => any ? Y
|
886
|
+
: unknown[],
|
887
|
+
> {
|
888
|
+
/**
|
889
|
+
* An array of the arguments passed to the mock function.
|
890
|
+
*/
|
891
|
+
arguments: Args;
|
892
|
+
/**
|
893
|
+
* If the mocked function threw then this property contains the thrown value.
|
894
|
+
*/
|
895
|
+
error: unknown | undefined;
|
896
|
+
/**
|
897
|
+
* The value returned by the mocked function.
|
898
|
+
*
|
899
|
+
* If the mocked function threw, it will be `undefined`.
|
900
|
+
*/
|
901
|
+
result: ReturnType | undefined;
|
902
|
+
/**
|
903
|
+
* An `Error` object whose stack can be used to determine the callsite of the mocked function invocation.
|
904
|
+
*/
|
905
|
+
stack: Error;
|
906
|
+
/**
|
907
|
+
* If the mocked function is a constructor, this field contains the class being constructed.
|
908
|
+
* Otherwise this will be `undefined`.
|
909
|
+
*/
|
910
|
+
target: F extends abstract new(...args: any) => any ? F : undefined;
|
911
|
+
/**
|
912
|
+
* The mocked function's `this` value.
|
913
|
+
*/
|
914
|
+
this: unknown;
|
915
|
+
}
|
916
|
+
/**
|
917
|
+
* The `MockFunctionContext` class is used to inspect or manipulate the behavior of
|
918
|
+
* mocks created via the `MockTracker` APIs.
|
919
|
+
* @since v19.1.0, v18.13.0
|
920
|
+
*/
|
921
|
+
class MockFunctionContext<F extends Function> {
|
922
|
+
/**
|
923
|
+
* A getter that returns a copy of the internal array used to track calls to the
|
924
|
+
* mock. Each entry in the array is an object with the following properties.
|
925
|
+
* @since v19.1.0, v18.13.0
|
926
|
+
*/
|
927
|
+
readonly calls: Array<MockFunctionCall<F>>;
|
928
|
+
/**
|
929
|
+
* This function returns the number of times that this mock has been invoked. This
|
930
|
+
* function is more efficient than checking `ctx.calls.length` because `ctx.calls` is a getter that creates a copy of the internal call tracking array.
|
931
|
+
* @since v19.1.0, v18.13.0
|
932
|
+
* @return The number of times that this mock has been invoked.
|
933
|
+
*/
|
934
|
+
callCount(): number;
|
935
|
+
/**
|
936
|
+
* This function is used to change the behavior of an existing mock.
|
937
|
+
*
|
938
|
+
* The following example creates a mock function using `t.mock.fn()`, calls the
|
939
|
+
* mock function, and then changes the mock implementation to a different function.
|
940
|
+
*
|
941
|
+
* ```js
|
942
|
+
* test('changes a mock behavior', (t) => {
|
943
|
+
* let cnt = 0;
|
944
|
+
*
|
945
|
+
* function addOne() {
|
946
|
+
* cnt++;
|
947
|
+
* return cnt;
|
948
|
+
* }
|
949
|
+
*
|
950
|
+
* function addTwo() {
|
951
|
+
* cnt += 2;
|
952
|
+
* return cnt;
|
953
|
+
* }
|
954
|
+
*
|
955
|
+
* const fn = t.mock.fn(addOne);
|
956
|
+
*
|
957
|
+
* assert.strictEqual(fn(), 1);
|
958
|
+
* fn.mock.mockImplementation(addTwo);
|
959
|
+
* assert.strictEqual(fn(), 3);
|
960
|
+
* assert.strictEqual(fn(), 5);
|
961
|
+
* });
|
962
|
+
* ```
|
963
|
+
* @since v19.1.0, v18.13.0
|
964
|
+
* @param implementation The function to be used as the mock's new implementation.
|
965
|
+
*/
|
966
|
+
mockImplementation(implementation: Function): void;
|
967
|
+
/**
|
968
|
+
* This function is used to change the behavior of an existing mock for a single
|
969
|
+
* invocation. Once invocation `onCall` has occurred, the mock will revert to
|
970
|
+
* whatever behavior it would have used had `mockImplementationOnce()` not been
|
971
|
+
* called.
|
972
|
+
*
|
973
|
+
* The following example creates a mock function using `t.mock.fn()`, calls the
|
974
|
+
* mock function, changes the mock implementation to a different function for the
|
975
|
+
* next invocation, and then resumes its previous behavior.
|
976
|
+
*
|
977
|
+
* ```js
|
978
|
+
* test('changes a mock behavior once', (t) => {
|
979
|
+
* let cnt = 0;
|
980
|
+
*
|
981
|
+
* function addOne() {
|
982
|
+
* cnt++;
|
983
|
+
* return cnt;
|
984
|
+
* }
|
985
|
+
*
|
986
|
+
* function addTwo() {
|
987
|
+
* cnt += 2;
|
988
|
+
* return cnt;
|
989
|
+
* }
|
990
|
+
*
|
991
|
+
* const fn = t.mock.fn(addOne);
|
992
|
+
*
|
993
|
+
* assert.strictEqual(fn(), 1);
|
994
|
+
* fn.mock.mockImplementationOnce(addTwo);
|
995
|
+
* assert.strictEqual(fn(), 3);
|
996
|
+
* assert.strictEqual(fn(), 4);
|
997
|
+
* });
|
998
|
+
* ```
|
999
|
+
* @since v19.1.0, v18.13.0
|
1000
|
+
* @param implementation The function to be used as the mock's implementation for the invocation number specified by `onCall`.
|
1001
|
+
* @param onCall The invocation number that will use `implementation`. If the specified invocation has already occurred then an exception is thrown.
|
1002
|
+
*/
|
1003
|
+
mockImplementationOnce(implementation: Function, onCall?: number): void;
|
1004
|
+
/**
|
1005
|
+
* Resets the call history of the mock function.
|
1006
|
+
* @since v19.3.0, v18.13.0
|
1007
|
+
*/
|
1008
|
+
resetCalls(): void;
|
1009
|
+
/**
|
1010
|
+
* Resets the implementation of the mock function to its original behavior. The
|
1011
|
+
* mock can still be used after calling this function.
|
1012
|
+
* @since v19.1.0, v18.13.0
|
1013
|
+
*/
|
1014
|
+
restore(): void;
|
1015
|
+
}
|
1016
|
+
type Timer = "setInterval" | "setTimeout" | "setImmediate" | "Date";
|
1017
|
+
|
1018
|
+
interface MockTimersOptions {
|
1019
|
+
apis: Timer[];
|
1020
|
+
now?: number | Date;
|
1021
|
+
}
|
1022
|
+
/**
|
1023
|
+
* Mocking timers is a technique commonly used in software testing to simulate and
|
1024
|
+
* control the behavior of timers, such as `setInterval` and `setTimeout`,
|
1025
|
+
* without actually waiting for the specified time intervals.
|
1026
|
+
*
|
1027
|
+
* The MockTimers API also allows for mocking of the `Date` constructor and
|
1028
|
+
* `setImmediate`/`clearImmediate` functions.
|
1029
|
+
*
|
1030
|
+
* The `MockTracker` provides a top-level `timers` export
|
1031
|
+
* which is a `MockTimers` instance.
|
1032
|
+
* @since v20.4.0
|
1033
|
+
* @experimental
|
1034
|
+
*/
|
1035
|
+
class MockTimers {
|
1036
|
+
/**
|
1037
|
+
* Enables timer mocking for the specified timers.
|
1038
|
+
*
|
1039
|
+
* **Note:** When you enable mocking for a specific timer, its associated
|
1040
|
+
* clear function will also be implicitly mocked.
|
1041
|
+
*
|
1042
|
+
* **Note:** Mocking `Date` will affect the behavior of the mocked timers
|
1043
|
+
* as they use the same internal clock.
|
1044
|
+
*
|
1045
|
+
* Example usage without setting initial time:
|
1046
|
+
*
|
1047
|
+
* ```js
|
1048
|
+
* import { mock } from 'node:test';
|
1049
|
+
* mock.timers.enable({ apis: ['setInterval', 'Date'], now: 1234 });
|
1050
|
+
* ```
|
1051
|
+
*
|
1052
|
+
* The above example enables mocking for the `Date` constructor, `setInterval` timer and
|
1053
|
+
* implicitly mocks the `clearInterval` function. Only the `Date` constructor from `globalThis`,
|
1054
|
+
* `setInterval` and `clearInterval` functions from `node:timers`, `node:timers/promises`, and `globalThis` will be mocked.
|
1055
|
+
*
|
1056
|
+
* Example usage with initial time set
|
1057
|
+
*
|
1058
|
+
* ```js
|
1059
|
+
* import { mock } from 'node:test';
|
1060
|
+
* mock.timers.enable({ apis: ['Date'], now: 1000 });
|
1061
|
+
* ```
|
1062
|
+
*
|
1063
|
+
* Example usage with initial Date object as time set
|
1064
|
+
*
|
1065
|
+
* ```js
|
1066
|
+
* import { mock } from 'node:test';
|
1067
|
+
* mock.timers.enable({ apis: ['Date'], now: new Date() });
|
1068
|
+
* ```
|
1069
|
+
*
|
1070
|
+
* Alternatively, if you call `mock.timers.enable()` without any parameters:
|
1071
|
+
*
|
1072
|
+
* All timers (`'setInterval'`, `'clearInterval'`, `'Date'`, `'setImmediate'`, `'clearImmediate'`, `'setTimeout'`, and `'clearTimeout'`)
|
1073
|
+
* will be mocked.
|
1074
|
+
*
|
1075
|
+
* The `setInterval`, `clearInterval`, `setTimeout`, and `clearTimeout` functions from `node:timers`, `node:timers/promises`,
|
1076
|
+
* and `globalThis` will be mocked.
|
1077
|
+
* The `Date` constructor from `globalThis` will be mocked.
|
1078
|
+
*
|
1079
|
+
* If there is no initial epoch set, the initial date will be based on 0 in the Unix epoch. This is `January 1st, 1970, 00:00:00 UTC`. You can
|
1080
|
+
* set an initial date by passing a now property to the `.enable()` method. This value will be used as the initial date for the mocked Date
|
1081
|
+
* object. It can either be a positive integer, or another Date object.
|
1082
|
+
* @since v20.4.0
|
1083
|
+
*/
|
1084
|
+
enable(options?: MockTimersOptions): void;
|
1085
|
+
/**
|
1086
|
+
* You can use the `.setTime()` method to manually move the mocked date to another time. This method only accepts a positive integer.
|
1087
|
+
* Note: This method will execute any mocked timers that are in the past from the new time.
|
1088
|
+
* In the below example we are setting a new time for the mocked date.
|
1089
|
+
* ```js
|
1090
|
+
* import assert from 'node:assert';
|
1091
|
+
* import { test } from 'node:test';
|
1092
|
+
* test('sets the time of a date object', (context) => {
|
1093
|
+
* // Optionally choose what to mock
|
1094
|
+
* context.mock.timers.enable({ apis: ['Date'], now: 100 });
|
1095
|
+
* assert.strictEqual(Date.now(), 100);
|
1096
|
+
* // Advance in time will also advance the date
|
1097
|
+
* context.mock.timers.setTime(1000);
|
1098
|
+
* context.mock.timers.tick(200);
|
1099
|
+
* assert.strictEqual(Date.now(), 1200);
|
1100
|
+
* });
|
1101
|
+
* ```
|
1102
|
+
*/
|
1103
|
+
setTime(time: number): void;
|
1104
|
+
/**
|
1105
|
+
* This function restores the default behavior of all mocks that were previously
|
1106
|
+
* created by this `MockTimers` instance and disassociates the mocks
|
1107
|
+
* from the `MockTracker` instance.
|
1108
|
+
*
|
1109
|
+
* **Note:** After each test completes, this function is called on
|
1110
|
+
* the test context's `MockTracker`.
|
1111
|
+
*
|
1112
|
+
* ```js
|
1113
|
+
* import { mock } from 'node:test';
|
1114
|
+
* mock.timers.reset();
|
1115
|
+
* ```
|
1116
|
+
* @since v20.4.0
|
1117
|
+
*/
|
1118
|
+
reset(): void;
|
1119
|
+
/**
|
1120
|
+
* Advances time for all mocked timers.
|
1121
|
+
*
|
1122
|
+
* **Note:** This diverges from how `setTimeout` in Node.js behaves and accepts
|
1123
|
+
* only positive numbers. In Node.js, `setTimeout` with negative numbers is
|
1124
|
+
* only supported for web compatibility reasons.
|
1125
|
+
*
|
1126
|
+
* The following example mocks a `setTimeout` function and
|
1127
|
+
* by using `.tick` advances in
|
1128
|
+
* time triggering all pending timers.
|
1129
|
+
*
|
1130
|
+
* ```js
|
1131
|
+
* import assert from 'node:assert';
|
1132
|
+
* import { test } from 'node:test';
|
1133
|
+
*
|
1134
|
+
* test('mocks setTimeout to be executed synchronously without having to actually wait for it', (context) => {
|
1135
|
+
* const fn = context.mock.fn();
|
1136
|
+
*
|
1137
|
+
* context.mock.timers.enable({ apis: ['setTimeout'] });
|
1138
|
+
*
|
1139
|
+
* setTimeout(fn, 9999);
|
1140
|
+
*
|
1141
|
+
* assert.strictEqual(fn.mock.callCount(), 0);
|
1142
|
+
*
|
1143
|
+
* // Advance in time
|
1144
|
+
* context.mock.timers.tick(9999);
|
1145
|
+
*
|
1146
|
+
* assert.strictEqual(fn.mock.callCount(), 1);
|
1147
|
+
* });
|
1148
|
+
* ```
|
1149
|
+
*
|
1150
|
+
* Alternativelly, the `.tick` function can be called many times
|
1151
|
+
*
|
1152
|
+
* ```js
|
1153
|
+
* import assert from 'node:assert';
|
1154
|
+
* import { test } from 'node:test';
|
1155
|
+
*
|
1156
|
+
* test('mocks setTimeout to be executed synchronously without having to actually wait for it', (context) => {
|
1157
|
+
* const fn = context.mock.fn();
|
1158
|
+
* context.mock.timers.enable({ apis: ['setTimeout'] });
|
1159
|
+
* const nineSecs = 9000;
|
1160
|
+
* setTimeout(fn, nineSecs);
|
1161
|
+
*
|
1162
|
+
* const twoSeconds = 3000;
|
1163
|
+
* context.mock.timers.tick(twoSeconds);
|
1164
|
+
* context.mock.timers.tick(twoSeconds);
|
1165
|
+
* context.mock.timers.tick(twoSeconds);
|
1166
|
+
*
|
1167
|
+
* assert.strictEqual(fn.mock.callCount(), 1);
|
1168
|
+
* });
|
1169
|
+
* ```
|
1170
|
+
*
|
1171
|
+
* Advancing time using `.tick` will also advance the time for any `Date` object
|
1172
|
+
* created after the mock was enabled (if `Date` was also set to be mocked).
|
1173
|
+
*
|
1174
|
+
* ```js
|
1175
|
+
* import assert from 'node:assert';
|
1176
|
+
* import { test } from 'node:test';
|
1177
|
+
*
|
1178
|
+
* test('mocks setTimeout to be executed synchronously without having to actually wait for it', (context) => {
|
1179
|
+
* const fn = context.mock.fn();
|
1180
|
+
*
|
1181
|
+
* context.mock.timers.enable({ apis: ['setTimeout', 'Date'] });
|
1182
|
+
* setTimeout(fn, 9999);
|
1183
|
+
*
|
1184
|
+
* assert.strictEqual(fn.mock.callCount(), 0);
|
1185
|
+
* assert.strictEqual(Date.now(), 0);
|
1186
|
+
*
|
1187
|
+
* // Advance in time
|
1188
|
+
* context.mock.timers.tick(9999);
|
1189
|
+
* assert.strictEqual(fn.mock.callCount(), 1);
|
1190
|
+
* assert.strictEqual(Date.now(), 9999);
|
1191
|
+
* });
|
1192
|
+
* ```
|
1193
|
+
* @since v20.4.0
|
1194
|
+
*/
|
1195
|
+
tick(milliseconds: number): void;
|
1196
|
+
/**
|
1197
|
+
* Triggers all pending mocked timers immediately. If the `Date` object is also
|
1198
|
+
* mocked, it will also advance the `Date` object to the furthest timer's time.
|
1199
|
+
*
|
1200
|
+
* The example below triggers all pending timers immediately,
|
1201
|
+
* causing them to execute without any delay.
|
1202
|
+
*
|
1203
|
+
* ```js
|
1204
|
+
* import assert from 'node:assert';
|
1205
|
+
* import { test } from 'node:test';
|
1206
|
+
*
|
1207
|
+
* test('runAll functions following the given order', (context) => {
|
1208
|
+
* context.mock.timers.enable({ apis: ['setTimeout', 'Date'] });
|
1209
|
+
* const results = [];
|
1210
|
+
* setTimeout(() => results.push(1), 9999);
|
1211
|
+
*
|
1212
|
+
* // Notice that if both timers have the same timeout,
|
1213
|
+
* // the order of execution is guaranteed
|
1214
|
+
* setTimeout(() => results.push(3), 8888);
|
1215
|
+
* setTimeout(() => results.push(2), 8888);
|
1216
|
+
*
|
1217
|
+
* assert.deepStrictEqual(results, []);
|
1218
|
+
*
|
1219
|
+
* context.mock.timers.runAll();
|
1220
|
+
* assert.deepStrictEqual(results, [3, 2, 1]);
|
1221
|
+
* // The Date object is also advanced to the furthest timer's time
|
1222
|
+
* assert.strictEqual(Date.now(), 9999);
|
1223
|
+
* });
|
1224
|
+
* ```
|
1225
|
+
*
|
1226
|
+
* **Note:** The `runAll()` function is specifically designed for
|
1227
|
+
* triggering timers in the context of timer mocking.
|
1228
|
+
* It does not have any effect on real-time system
|
1229
|
+
* clocks or actual timers outside of the mocking environment.
|
1230
|
+
* @since v20.4.0
|
1231
|
+
*/
|
1232
|
+
runAll(): void;
|
1233
|
+
/**
|
1234
|
+
* Calls {@link MockTimers.reset()}.
|
1235
|
+
*/
|
1236
|
+
[Symbol.dispose](): void;
|
1237
|
+
}
|
1238
|
+
export {
|
1239
|
+
after,
|
1240
|
+
afterEach,
|
1241
|
+
before,
|
1242
|
+
beforeEach,
|
1243
|
+
describe,
|
1244
|
+
it,
|
1245
|
+
Mock,
|
1246
|
+
mock,
|
1247
|
+
only,
|
1248
|
+
run,
|
1249
|
+
skip,
|
1250
|
+
SuiteContext,
|
1251
|
+
test,
|
1252
|
+
test as default,
|
1253
|
+
TestContext,
|
1254
|
+
todo,
|
1255
|
+
};
|
1256
|
+
}
|
1257
|
+
|
1258
|
+
interface TestLocationInfo {
|
1259
|
+
/**
|
1260
|
+
* The column number where the test is defined, or
|
1261
|
+
* `undefined` if the test was run through the REPL.
|
1262
|
+
*/
|
1263
|
+
column?: number;
|
1264
|
+
/**
|
1265
|
+
* The path of the test file, `undefined` if test is not ran through a file.
|
1266
|
+
*/
|
1267
|
+
file?: string;
|
1268
|
+
/**
|
1269
|
+
* The line number where the test is defined, or
|
1270
|
+
* `undefined` if the test was run through the REPL.
|
1271
|
+
*/
|
1272
|
+
line?: number;
|
1273
|
+
}
|
1274
|
+
interface DiagnosticData extends TestLocationInfo {
|
1275
|
+
/**
|
1276
|
+
* The diagnostic message.
|
1277
|
+
*/
|
1278
|
+
message: string;
|
1279
|
+
/**
|
1280
|
+
* The nesting level of the test.
|
1281
|
+
*/
|
1282
|
+
nesting: number;
|
1283
|
+
}
|
1284
|
+
interface TestFail extends TestLocationInfo {
|
1285
|
+
/**
|
1286
|
+
* Additional execution metadata.
|
1287
|
+
*/
|
1288
|
+
details: {
|
1289
|
+
/**
|
1290
|
+
* The duration of the test in milliseconds.
|
1291
|
+
*/
|
1292
|
+
duration_ms: number;
|
1293
|
+
/**
|
1294
|
+
* The error thrown by the test.
|
1295
|
+
*/
|
1296
|
+
error: Error;
|
1297
|
+
/**
|
1298
|
+
* The type of the test, used to denote whether this is a suite.
|
1299
|
+
* @since 20.0.0, 19.9.0, 18.17.0
|
1300
|
+
*/
|
1301
|
+
type?: "suite";
|
1302
|
+
};
|
1303
|
+
/**
|
1304
|
+
* The test name.
|
1305
|
+
*/
|
1306
|
+
name: string;
|
1307
|
+
/**
|
1308
|
+
* The nesting level of the test.
|
1309
|
+
*/
|
1310
|
+
nesting: number;
|
1311
|
+
/**
|
1312
|
+
* The ordinal number of the test.
|
1313
|
+
*/
|
1314
|
+
testNumber: number;
|
1315
|
+
/**
|
1316
|
+
* Present if `context.todo` is called.
|
1317
|
+
*/
|
1318
|
+
todo?: string | boolean;
|
1319
|
+
/**
|
1320
|
+
* Present if `context.skip` is called.
|
1321
|
+
*/
|
1322
|
+
skip?: string | boolean;
|
1323
|
+
}
|
1324
|
+
interface TestPass extends TestLocationInfo {
|
1325
|
+
/**
|
1326
|
+
* Additional execution metadata.
|
1327
|
+
*/
|
1328
|
+
details: {
|
1329
|
+
/**
|
1330
|
+
* The duration of the test in milliseconds.
|
1331
|
+
*/
|
1332
|
+
duration_ms: number;
|
1333
|
+
/**
|
1334
|
+
* The type of the test, used to denote whether this is a suite.
|
1335
|
+
* @since 20.0.0, 19.9.0, 18.17.0
|
1336
|
+
*/
|
1337
|
+
type?: "suite";
|
1338
|
+
};
|
1339
|
+
/**
|
1340
|
+
* The test name.
|
1341
|
+
*/
|
1342
|
+
name: string;
|
1343
|
+
/**
|
1344
|
+
* The nesting level of the test.
|
1345
|
+
*/
|
1346
|
+
nesting: number;
|
1347
|
+
/**
|
1348
|
+
* The ordinal number of the test.
|
1349
|
+
*/
|
1350
|
+
testNumber: number;
|
1351
|
+
/**
|
1352
|
+
* Present if `context.todo` is called.
|
1353
|
+
*/
|
1354
|
+
todo?: string | boolean;
|
1355
|
+
/**
|
1356
|
+
* Present if `context.skip` is called.
|
1357
|
+
*/
|
1358
|
+
skip?: string | boolean;
|
1359
|
+
}
|
1360
|
+
interface TestPlan extends TestLocationInfo {
|
1361
|
+
/**
|
1362
|
+
* The nesting level of the test.
|
1363
|
+
*/
|
1364
|
+
nesting: number;
|
1365
|
+
/**
|
1366
|
+
* The number of subtests that have ran.
|
1367
|
+
*/
|
1368
|
+
count: number;
|
1369
|
+
}
|
1370
|
+
interface TestStart extends TestLocationInfo {
|
1371
|
+
/**
|
1372
|
+
* The test name.
|
1373
|
+
*/
|
1374
|
+
name: string;
|
1375
|
+
/**
|
1376
|
+
* The nesting level of the test.
|
1377
|
+
*/
|
1378
|
+
nesting: number;
|
1379
|
+
}
|
1380
|
+
interface TestStderr extends TestLocationInfo {
|
1381
|
+
/**
|
1382
|
+
* The message written to `stderr`
|
1383
|
+
*/
|
1384
|
+
message: string;
|
1385
|
+
}
|
1386
|
+
interface TestStdout extends TestLocationInfo {
|
1387
|
+
/**
|
1388
|
+
* The message written to `stdout`
|
1389
|
+
*/
|
1390
|
+
message: string;
|
1391
|
+
}
|
1392
|
+
interface TestEnqueue extends TestLocationInfo {
|
1393
|
+
/**
|
1394
|
+
* The test name
|
1395
|
+
*/
|
1396
|
+
name: string;
|
1397
|
+
/**
|
1398
|
+
* The nesting level of the test.
|
1399
|
+
*/
|
1400
|
+
nesting: number;
|
1401
|
+
}
|
1402
|
+
interface TestDequeue extends TestLocationInfo {
|
1403
|
+
/**
|
1404
|
+
* The test name
|
1405
|
+
*/
|
1406
|
+
name: string;
|
1407
|
+
/**
|
1408
|
+
* The nesting level of the test.
|
1409
|
+
*/
|
1410
|
+
nesting: number;
|
1411
|
+
}
|
1412
|
+
|
1413
|
+
/**
|
1414
|
+
* The `node:test/reporters` module exposes the builtin-reporters for `node:test`.
|
1415
|
+
* To access it:
|
1416
|
+
*
|
1417
|
+
* ```js
|
1418
|
+
* import test from 'node:test/reporters';
|
1419
|
+
* ```
|
1420
|
+
*
|
1421
|
+
* This module is only available under the `node:` scheme. The following will not
|
1422
|
+
* work:
|
1423
|
+
*
|
1424
|
+
* ```js
|
1425
|
+
* import test from 'test/reporters';
|
1426
|
+
* ```
|
1427
|
+
* @since v19.9.0
|
1428
|
+
* @see [source](https://github.com/nodejs/node/blob/v20.12.2/lib/test/reporters.js)
|
1429
|
+
*/
|
1430
|
+
declare module "node:test/reporters" {
|
1431
|
+
import { Transform, TransformOptions } from "node:stream";
|
1432
|
+
|
1433
|
+
type TestEvent =
|
1434
|
+
| { type: "test:diagnostic"; data: DiagnosticData }
|
1435
|
+
| { type: "test:fail"; data: TestFail }
|
1436
|
+
| { type: "test:pass"; data: TestPass }
|
1437
|
+
| { type: "test:plan"; data: TestPlan }
|
1438
|
+
| { type: "test:start"; data: TestStart }
|
1439
|
+
| { type: "test:stderr"; data: TestStderr }
|
1440
|
+
| { type: "test:stdout"; data: TestStdout }
|
1441
|
+
| { type: "test:enqueue"; data: TestEnqueue }
|
1442
|
+
| { type: "test:dequeue"; data: TestDequeue }
|
1443
|
+
| { type: "test:watch:drained" };
|
1444
|
+
type TestEventGenerator = AsyncGenerator<TestEvent, void>;
|
1445
|
+
|
1446
|
+
/**
|
1447
|
+
* The `dot` reporter outputs the test results in a compact format,
|
1448
|
+
* where each passing test is represented by a `.`,
|
1449
|
+
* and each failing test is represented by a `X`.
|
1450
|
+
*/
|
1451
|
+
function dot(source: TestEventGenerator): AsyncGenerator<"\n" | "." | "X", void>;
|
1452
|
+
/**
|
1453
|
+
* The `tap` reporter outputs the test results in the [TAP](https://testanything.org/) format.
|
1454
|
+
*/
|
1455
|
+
function tap(source: TestEventGenerator): AsyncGenerator<string, void>;
|
1456
|
+
/**
|
1457
|
+
* The `spec` reporter outputs the test results in a human-readable format.
|
1458
|
+
*/
|
1459
|
+
class Spec extends Transform {
|
1460
|
+
constructor();
|
1461
|
+
}
|
1462
|
+
/**
|
1463
|
+
* The `junit` reporter outputs test results in a jUnit XML format
|
1464
|
+
*/
|
1465
|
+
function junit(source: TestEventGenerator): AsyncGenerator<string, void>;
|
1466
|
+
class Lcov extends Transform {
|
1467
|
+
constructor(opts?: TransformOptions);
|
1468
|
+
}
|
1469
|
+
export { dot, junit, Lcov as lcov, Spec as spec, tap, TestEvent };
|
1470
|
+
}
|