Skip to content

Commit d16c69b

Browse files
authored
🤖 Merge PR DefinitelyTyped#70292 [node] Update typings to v20.15.0 by @Semigradsky
1 parent 98cd778 commit d16c69b

File tree

5 files changed

+178
-6
lines changed

5 files changed

+178
-6
lines changed

types/node/v20/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"private": true,
33
"name": "@types/node",
4-
"version": "20.14.9999",
4+
"version": "20.15.9999",
55
"nonNpm": "conflict",
66
"nonNpmDescription": "Node.js",
77
"projects": [
88
"https://nodejs.org/"
99
],
1010
"tsconfigs": ["tsconfig.dom.json", "tsconfig.non-dom.json"],
1111
"dependencies": {
12-
"undici-types": "~5.26.4"
12+
"undici-types": "~6.13.0"
1313
},
1414
"devDependencies": {
1515
"@types/node": "workspace:."

types/node/v20/test.d.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,12 @@ declare module "node:test" {
452452
* @since v18.0.0, v16.17.0
453453
*/
454454
class TestContext {
455+
/**
456+
* An object containing assertion methods bound to the test context.
457+
* The top-level functions from the `node:assert` module are exposed here for the purpose of creating test plans.
458+
* @since v20.15.0
459+
*/
460+
readonly assert: TestContextAssert;
455461
/**
456462
* This function is used to create a hook running before subtest of the current test.
457463
* @param fn The hook function. If the hook uses callbacks, the callback function is passed as the second argument.
@@ -499,6 +505,42 @@ declare module "node:test" {
499505
* @since v18.8.0, v16.18.0
500506
*/
501507
readonly name: string;
508+
/**
509+
* Used to set the number of assertions and subtests that are expected to run within the test.
510+
* If the number of assertions and subtests that run does not match the expected count, the test will fail.
511+
*
512+
* To make sure assertions are tracked, the assert functions on `context.assert` must be used,
513+
* instead of importing from the `node:assert` module.
514+
* ```js
515+
* test('top level test', (t) => {
516+
* t.plan(2);
517+
* t.assert.ok('some relevant assertion here');
518+
* t.test('subtest', () => {});
519+
* });
520+
* ```
521+
*
522+
* When working with asynchronous code, the `plan` function can be used to ensure that the correct number of assertions are run:
523+
* ```js
524+
* test('planning with streams', (t, done) => {
525+
* function* generate() {
526+
* yield 'a';
527+
* yield 'b';
528+
* yield 'c';
529+
* }
530+
* const expected = ['a', 'b', 'c'];
531+
* t.plan(expected.length);
532+
* const stream = Readable.from(generate());
533+
* stream.on('data', (chunk) => {
534+
* t.assert.strictEqual(chunk, expected.shift());
535+
* });
536+
* stream.on('end', () => {
537+
* done();
538+
* });
539+
* });
540+
* ```
541+
* @since v20.15.0
542+
*/
543+
plan(count: number): void;
502544
/**
503545
* If `shouldRunOnlyTests` is truthy, the test context will only run tests that
504546
* have the `only` option set. Otherwise, all tests are run. If Node.js was not
@@ -575,6 +617,76 @@ declare module "node:test" {
575617
*/
576618
readonly mock: MockTracker;
577619
}
620+
interface TestContextAssert {
621+
/**
622+
* Identical to the `deepEqual` function from the `node:assert` module, but bound to the test context.
623+
*/
624+
deepEqual: typeof import("node:assert").deepEqual;
625+
/**
626+
* Identical to the `deepStrictEqual` function from the `node:assert` module, but bound to the test context.
627+
*/
628+
deepStrictEqual: typeof import("node:assert").deepStrictEqual;
629+
/**
630+
* Identical to the `doesNotMatch` function from the `node:assert` module, but bound to the test context.
631+
*/
632+
doesNotMatch: typeof import("node:assert").doesNotMatch;
633+
/**
634+
* Identical to the `doesNotReject` function from the `node:assert` module, but bound to the test context.
635+
*/
636+
doesNotReject: typeof import("node:assert").doesNotReject;
637+
/**
638+
* Identical to the `doesNotThrow` function from the `node:assert` module, but bound to the test context.
639+
*/
640+
doesNotThrow: typeof import("node:assert").doesNotThrow;
641+
/**
642+
* Identical to the `equal` function from the `node:assert` module, but bound to the test context.
643+
*/
644+
equal: typeof import("node:assert").equal;
645+
/**
646+
* Identical to the `fail` function from the `node:assert` module, but bound to the test context.
647+
*/
648+
fail: typeof import("node:assert").fail;
649+
/**
650+
* Identical to the `ifError` function from the `node:assert` module, but bound to the test context.
651+
*/
652+
ifError: typeof import("node:assert").ifError;
653+
/**
654+
* Identical to the `match` function from the `node:assert` module, but bound to the test context.
655+
*/
656+
match: typeof import("node:assert").match;
657+
/**
658+
* Identical to the `notDeepEqual` function from the `node:assert` module, but bound to the test context.
659+
*/
660+
notDeepEqual: typeof import("node:assert").notDeepEqual;
661+
/**
662+
* Identical to the `notDeepStrictEqual` function from the `node:assert` module, but bound to the test context.
663+
*/
664+
notDeepStrictEqual: typeof import("node:assert").notDeepStrictEqual;
665+
/**
666+
* Identical to the `notEqual` function from the `node:assert` module, but bound to the test context.
667+
*/
668+
notEqual: typeof import("node:assert").notEqual;
669+
/**
670+
* Identical to the `notStrictEqual` function from the `node:assert` module, but bound to the test context.
671+
*/
672+
notStrictEqual: typeof import("node:assert").notStrictEqual;
673+
/**
674+
* Identical to the `ok` function from the `node:assert` module, but bound to the test context.
675+
*/
676+
ok: typeof import("node:assert").ok;
677+
/**
678+
* Identical to the `rejects` function from the `node:assert` module, but bound to the test context.
679+
*/
680+
rejects: typeof import("node:assert").rejects;
681+
/**
682+
* Identical to the `strictEqual` function from the `node:assert` module, but bound to the test context.
683+
*/
684+
strictEqual: typeof import("node:assert").strictEqual;
685+
/**
686+
* Identical to the `throws` function from the `node:assert` module, but bound to the test context.
687+
*/
688+
throws: typeof import("node:assert").throws;
689+
}
578690

579691
/**
580692
* An instance of `SuiteContext` is passed to each suite function in order to
@@ -634,6 +746,14 @@ declare module "node:test" {
634746
* @default false
635747
*/
636748
todo?: boolean | string | undefined;
749+
/**
750+
* The number of assertions and subtests expected to be run in the test.
751+
* If the number of assertions run in the test does not match the number
752+
* specified in the plan, the test will fail.
753+
* @default undefined
754+
* @since v20.15.0
755+
*/
756+
plan?: number | undefined;
637757
}
638758
/**
639759
* This function creates a hook that runs before executing a suite.

types/node/v20/test/test.ts

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Transform, TransformCallback, TransformOptions } from "node:stream";
1+
import { Readable, Transform, TransformCallback, TransformOptions } from "node:stream";
22
import {
33
after,
44
afterEach,
@@ -39,7 +39,10 @@ run({
3939
inspectPort: () => 8081,
4040
testNamePatterns: ["executed", /^core-/],
4141
only: true,
42-
setup: (root) => {},
42+
setup: (reporter) => {
43+
// $ExpectType TestsStream
44+
reporter;
45+
},
4346
watch: true,
4447
shard: {
4548
index: 1,
@@ -97,7 +100,7 @@ test((t, cb) => {
97100
cb({ x: "anything" });
98101
});
99102

100-
// Test the context's methods
103+
// Test the context's methods/properties
101104
test(undefined, undefined, t => {
102105
// $ExpectType void
103106
t.diagnostic("tap diagnostic");
@@ -119,6 +122,13 @@ test(undefined, undefined, t => {
119122
t.beforeEach(() => {});
120123
// $ExpectType void
121124
t.before(() => {});
125+
126+
// $ExpectType string
127+
t.name;
128+
// $ExpectType AbortSignal
129+
t.signal;
130+
// $ExpectType MockTracker
131+
t.mock;
122132
});
123133

124134
// Test the subtest approach.
@@ -144,14 +154,14 @@ test.describe("describe", () => {});
144154
test.it("it", () => {});
145155
// $ExpectType MockTracker
146156
test.mock;
147-
test.suite("suite", () => {});
148157
// $ExpectType typeof test
149158
test.test;
150159
test.test.test("chained self ref", (t) => {
151160
// $ExpectType typeof test
152161
t.test;
153162
});
154163
test.skip("skip", () => {});
164+
test.suite("suite", () => {});
155165
test.todo("todo", () => {});
156166
test.only("only", () => {});
157167

@@ -823,6 +833,11 @@ class TestReporter extends Transform {
823833
);
824834
break;
825835
}
836+
case "test:coverage": {
837+
const { summary, nesting } = event.data;
838+
callback(null, `${nesting}/${summary.workingDirectory}/${summary.totals.totalLineCount}`);
839+
break;
840+
}
826841
case "test:dequeue": {
827842
const { file, column, line, name, nesting } = event.data;
828843
callback(null, `${name}/${nesting}/${file}/${column}/${line}`);
@@ -920,3 +935,22 @@ const invalidTestContext = new TestContext();
920935

921936
// @ts-expect-error Should not be able to instantiate a SuiteContext
922937
const invalidSuiteContext = new SuiteContext();
938+
939+
test("planning with streams", (t: TestContext, done) => {
940+
function* generate() {
941+
yield "a";
942+
yield "b";
943+
yield "c";
944+
}
945+
const expected = ["a", "b", "c"];
946+
t.plan(expected.length);
947+
948+
const stream = Readable.from(generate());
949+
stream.on("data", (chunk) => {
950+
t.assert.strictEqual(chunk, expected.shift());
951+
});
952+
953+
stream.on("end", () => {
954+
done();
955+
});
956+
});

types/node/v20/test/zlib.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
brotliDecompressSync,
88
BrotliOptions,
99
constants,
10+
crc32,
1011
createBrotliCompress,
1112
createBrotliDecompress,
1213
deflate,
@@ -189,3 +190,11 @@ brotliDecompress(
189190
await pUnzip(Buffer.from("buf"), { flush: constants.Z_NO_FLUSH }); // $ExpectType Buffer
190191
})();
191192
}
193+
194+
{
195+
let crc = crc32("hello");
196+
crc = crc32("world", crc); // $ExpectType number
197+
198+
crc = crc32(Buffer.from("hello", "utf16le")); // $ExpectType number
199+
crc = crc32(Buffer.from("world", "utf16le"), crc); // $ExpectType number
200+
}

types/node/v20/zlib.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ declare module "zlib" {
172172
interface DeflateRaw extends stream.Transform, Zlib, ZlibReset, ZlibParams {}
173173
interface InflateRaw extends stream.Transform, Zlib, ZlibReset {}
174174
interface Unzip extends stream.Transform, Zlib {}
175+
/**
176+
* Computes a 32-bit [Cyclic Redundancy Check](https://en.wikipedia.org/wiki/Cyclic_redundancy_check) checksum of `data`.
177+
* If `value` is specified, it is used as the starting value of the checksum, otherwise, 0 is used as the starting value.
178+
* @param data When `data` is a string, it will be encoded as UTF-8 before being used for computation.
179+
* @param value An optional starting value. It must be a 32-bit unsigned integer. @default 0
180+
* @returns A 32-bit unsigned integer containing the checksum.
181+
* @since v20.15.0
182+
*/
183+
function crc32(data: string | Buffer | NodeJS.ArrayBufferView, value?: number): number;
175184
/**
176185
* Creates and returns a new `BrotliCompress` object.
177186
* @since v11.7.0, v10.16.0

0 commit comments

Comments
 (0)