@types/node 18.14.0 → 18.14.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. node/README.md +1 -1
  2. node/package.json +2 -2
  3. node/test.d.ts +238 -1
  4. node/ts4.8/test.d.ts +247 -1
node/README.md CHANGED
@@ -8,7 +8,7 @@ This package contains type definitions for Node.js (https://nodejs.org/).
8
8
  Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node.
9
9
 
10
10
  ### Additional Details
11
- * Last updated: Fri, 17 Feb 2023 20:32:39 GMT
11
+ * Last updated: Thu, 23 Feb 2023 11:02:31 GMT
12
12
  * Dependencies: none
13
13
  * Global values: `AbortController`, `AbortSignal`, `__dirname`, `__filename`, `console`, `exports`, `gc`, `global`, `module`, `process`, `require`, `structuredClone`
14
14
 
node/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@types/node",
3
- "version": "18.14.0",
3
+ "version": "18.14.1",
4
4
  "description": "TypeScript definitions for Node.js",
5
5
  "homepage": "https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node",
6
6
  "license": "MIT",
@@ -232,6 +232,6 @@
232
232
  },
233
233
  "scripts": {},
234
234
  "dependencies": {},
235
- "typesPublisherContentHash": "c162056afaa72720d23d051bc83320f677f9fe17ead37974cc874e1723ec3bf0",
235
+ "typesPublisherContentHash": "4b700d8e69578ec73360139a785868405bb684e4fe62cd395573dccfde90213e",
236
236
  "typeScriptVersion": "4.2"
237
237
  }
node/test.d.ts CHANGED
@@ -342,6 +342,10 @@ declare module 'node:test' {
342
342
  * @returns A {@link Promise} resolved with `undefined` once the test completes.
343
343
  */
344
344
  test: typeof test;
345
+ /**
346
+ * Each test provides its own MockTracker instance.
347
+ */
348
+ readonly mock: MockTracker;
345
349
  }
346
350
 
347
351
  interface TestOptions {
@@ -451,5 +455,238 @@ declare module 'node:test' {
451
455
  timeout?: number | undefined;
452
456
  }
453
457
 
454
- export { test as default, run, test, describe, it, before, after, beforeEach, afterEach };
458
+ interface MockFunctionOptions {
459
+ /**
460
+ * The number of times that the mock will use the behavior of `implementation`.
461
+ * Once the mock function has been called `times` times,
462
+ * it will automatically restore the behavior of `original`.
463
+ * This value must be an integer greater than zero.
464
+ * @default Infinity
465
+ */
466
+ times?: number | undefined;
467
+ }
468
+
469
+ interface MockMethodOptions extends MockFunctionOptions {
470
+ /**
471
+ * If `true`, `object[methodName]` is treated as a getter.
472
+ * This option cannot be used with the `setter` option.
473
+ */
474
+ getter?: boolean | undefined;
475
+
476
+ /**
477
+ * If `true`, `object[methodName]` is treated as a setter.
478
+ * This option cannot be used with the `getter` option.
479
+ */
480
+ setter?: boolean | undefined;
481
+ }
482
+
483
+ type Mock<F extends Function> = F & {
484
+ mock: MockFunctionContext<F>;
485
+ };
486
+
487
+ type NoOpFunction = (...args: any[]) => undefined;
488
+
489
+ type FunctionPropertyNames<T> = {
490
+ [K in keyof T]: T[K] extends Function ? K : never;
491
+ }[keyof T];
492
+
493
+ interface MockTracker {
494
+ /**
495
+ * This function is used to create a mock function.
496
+ * @param original An optional function to create a mock on.
497
+ * @param implementation An optional function used as the mock implementation for `original`.
498
+ * This is useful for creating mocks that exhibit one behavior for a specified number of calls and then restore the behavior of `original`.
499
+ * @param options Optional configuration options for the mock function.
500
+ */
501
+ fn<F extends Function = NoOpFunction>(original?: F, options?: MockFunctionOptions): Mock<F>;
502
+ fn<F extends Function = NoOpFunction, Implementation extends Function = F>(original?: F, implementation?: Implementation, options?: MockFunctionOptions): Mock<F | Implementation>;
503
+
504
+ /**
505
+ * This function is used to create a mock on an existing object method.
506
+ * @param object The object whose method is being mocked.
507
+ * @param methodName The identifier of the method on `object` to mock. If `object[methodName]` is not a function, an error is thrown.
508
+ * @param implementation An optional function used as the mock implementation for `object[methodName]`.
509
+ * @param options Optional configuration options for the mock method.
510
+ */
511
+ method<
512
+ MockedObject extends object,
513
+ MethodName extends FunctionPropertyNames<MockedObject>,
514
+ >(
515
+ object: MockedObject,
516
+ methodName: MethodName,
517
+ options?: MockFunctionOptions,
518
+ ): MockedObject[MethodName] extends Function
519
+ ? Mock<MockedObject[MethodName]>
520
+ : never;
521
+ method<
522
+ MockedObject extends object,
523
+ MethodName extends FunctionPropertyNames<MockedObject>,
524
+ Implementation extends Function,
525
+ >(
526
+ object: MockedObject,
527
+ methodName: MethodName,
528
+ implementation: Implementation,
529
+ options?: MockFunctionOptions,
530
+ ): MockedObject[MethodName] extends Function
531
+ ? Mock<MockedObject[MethodName] | Implementation>
532
+ : never;
533
+ method<MockedObject extends object>(
534
+ object: MockedObject,
535
+ methodName: keyof MockedObject,
536
+ options: MockMethodOptions,
537
+ ): Mock<Function>;
538
+ method<MockedObject extends object>(
539
+ object: MockedObject,
540
+ methodName: keyof MockedObject,
541
+ implementation: Function,
542
+ options: MockMethodOptions,
543
+ ): Mock<Function>;
544
+
545
+ /**
546
+ * This function is syntax sugar for {@link MockTracker.method} with `options.getter` set to `true`.
547
+ */
548
+ getter<
549
+ MockedObject extends object,
550
+ MethodName extends keyof MockedObject,
551
+ >(
552
+ object: MockedObject,
553
+ methodName: MethodName,
554
+ options?: MockFunctionOptions,
555
+ ): Mock<() => MockedObject[MethodName]>;
556
+ getter<
557
+ MockedObject extends object,
558
+ MethodName extends keyof MockedObject,
559
+ Implementation extends Function,
560
+ >(
561
+ object: MockedObject,
562
+ methodName: MethodName,
563
+ implementation?: Implementation,
564
+ options?: MockFunctionOptions,
565
+ ): Mock<(() => MockedObject[MethodName]) | Implementation>;
566
+
567
+ /**
568
+ * This function is syntax sugar for {@link MockTracker.method} with `options.setter` set to `true`.
569
+ */
570
+ setter<
571
+ MockedObject extends object,
572
+ MethodName extends keyof MockedObject,
573
+ >(
574
+ object: MockedObject,
575
+ methodName: MethodName,
576
+ options?: MockFunctionOptions,
577
+ ): Mock<(value: MockedObject[MethodName]) => void>;
578
+ setter<
579
+ MockedObject extends object,
580
+ MethodName extends keyof MockedObject,
581
+ Implementation extends Function,
582
+ >(
583
+ object: MockedObject,
584
+ methodName: MethodName,
585
+ implementation?: Implementation,
586
+ options?: MockFunctionOptions,
587
+ ): Mock<((value: MockedObject[MethodName]) => void) | Implementation>;
588
+
589
+ /**
590
+ * This function restores the default behavior of all mocks that were previously created by this `MockTracker`
591
+ * and disassociates the mocks from the `MockTracker` instance. Once disassociated, the mocks can still be used,
592
+ * but the `MockTracker` instance can no longer be used to reset their behavior or otherwise interact with them.
593
+ *
594
+ * After each test completes, this function is called on the test context's `MockTracker`.
595
+ * If the global `MockTracker` is used extensively, calling this function manually is recommended.
596
+ */
597
+ reset(): void;
598
+
599
+ /**
600
+ * This function restores the default behavior of all mocks that were previously created by this `MockTracker`.
601
+ * Unlike `mock.reset()`, `mock.restoreAll()` does not disassociate the mocks from the `MockTracker` instance.
602
+ */
603
+ restoreAll(): void;
604
+ }
605
+
606
+ const mock: MockTracker;
607
+
608
+ interface MockFunctionCall<
609
+ F extends Function,
610
+ ReturnType = F extends (...args: any) => infer T
611
+ ? T
612
+ : F extends abstract new (...args: any) => infer T
613
+ ? T
614
+ : unknown,
615
+ Args = F extends (...args: infer Y) => any
616
+ ? Y
617
+ : F extends abstract new (...args: infer Y) => any
618
+ ? Y
619
+ : unknown[],
620
+ > {
621
+ /**
622
+ * An array of the arguments passed to the mock function.
623
+ */
624
+ arguments: Args;
625
+ /**
626
+ * If the mocked function threw then this property contains the thrown value.
627
+ */
628
+ error: unknown | undefined;
629
+ /**
630
+ * The value returned by the mocked function.
631
+ *
632
+ * If the mocked function threw, it will be `undefined`.
633
+ */
634
+ result: ReturnType | undefined;
635
+ /**
636
+ * An `Error` object whose stack can be used to determine the callsite of the mocked function invocation.
637
+ */
638
+ stack: Error;
639
+ /**
640
+ * If the mocked function is a constructor, this field contains the class being constructed.
641
+ * Otherwise this will be `undefined`.
642
+ */
643
+ target: F extends abstract new (...args: any) => any ? F : undefined;
644
+ /**
645
+ * The mocked function's `this` value.
646
+ */
647
+ this: unknown;
648
+ }
649
+
650
+ interface MockFunctionContext<F extends Function> {
651
+ /**
652
+ * A getter that returns a copy of the internal array used to track calls to the mock.
653
+ */
654
+ readonly calls: Array<MockFunctionCall<F>>;
655
+
656
+ /**
657
+ * This function returns the number of times that this mock has been invoked.
658
+ * This function is more efficient than checking `ctx.calls.length`
659
+ * because `ctx.calls` is a getter that creates a copy of the internal call tracking array.
660
+ */
661
+ callCount(): number;
662
+
663
+ /**
664
+ * This function is used to change the behavior of an existing mock.
665
+ * @param implementation The function to be used as the mock's new implementation.
666
+ */
667
+ mockImplementation(implementation: Function): void;
668
+
669
+ /**
670
+ * This function is used to change the behavior of an existing mock for a single invocation.
671
+ * Once invocation `onCall` has occurred, the mock will revert to whatever behavior
672
+ * it would have used had `mockImplementationOnce()` not been called.
673
+ * @param implementation The function to be used as the mock's implementation for the invocation number specified by `onCall`.
674
+ * @param onCall The invocation number that will use `implementation`.
675
+ * If the specified invocation has already occurred then an exception is thrown.
676
+ */
677
+ mockImplementationOnce(implementation: Function, onCall?: number): void;
678
+
679
+ /**
680
+ * Resets the call history of the mock function.
681
+ */
682
+ resetCalls(): void;
683
+
684
+ /**
685
+ * Resets the implementation of the mock function to its original behavior.
686
+ * The mock can still be used after calling this function.
687
+ */
688
+ restore(): void;
689
+ }
690
+
691
+ export { test as default, run, test, describe, it, before, after, beforeEach, afterEach, mock };
455
692
  }
node/ts4.8/test.d.ts CHANGED
@@ -264,6 +264,15 @@ declare module 'node:test' {
264
264
  */
265
265
  beforeEach: typeof beforeEach;
266
266
 
267
+ /**
268
+ * This function is used to create a hook that runs after the current test finishes.
269
+ * @param fn The hook function. If the hook uses callbacks, the callback function is passed as
270
+ * the second argument. Default: A no-op function.
271
+ * @param options Configuration options for the hook.
272
+ * @since v18.13.0
273
+ */
274
+ after: typeof after;
275
+
267
276
  /**
268
277
  * This function is used to create a hook running after each subtest of the current test.
269
278
  * @param fn The hook function. If the hook uses callbacks, the callback function is passed as
@@ -333,6 +342,10 @@ declare module 'node:test' {
333
342
  * @returns A {@link Promise} resolved with `undefined` once the test completes.
334
343
  */
335
344
  test: typeof test;
345
+ /**
346
+ * Each test provides its own MockTracker instance.
347
+ */
348
+ readonly mock: MockTracker;
336
349
  }
337
350
 
338
351
  interface TestOptions {
@@ -442,5 +455,238 @@ declare module 'node:test' {
442
455
  timeout?: number | undefined;
443
456
  }
444
457
 
445
- export { test as default, run, test, describe, it, before, after, beforeEach, afterEach };
458
+ interface MockFunctionOptions {
459
+ /**
460
+ * The number of times that the mock will use the behavior of `implementation`.
461
+ * Once the mock function has been called `times` times,
462
+ * it will automatically restore the behavior of `original`.
463
+ * This value must be an integer greater than zero.
464
+ * @default Infinity
465
+ */
466
+ times?: number | undefined;
467
+ }
468
+
469
+ interface MockMethodOptions extends MockFunctionOptions {
470
+ /**
471
+ * If `true`, `object[methodName]` is treated as a getter.
472
+ * This option cannot be used with the `setter` option.
473
+ */
474
+ getter?: boolean | undefined;
475
+
476
+ /**
477
+ * If `true`, `object[methodName]` is treated as a setter.
478
+ * This option cannot be used with the `getter` option.
479
+ */
480
+ setter?: boolean | undefined;
481
+ }
482
+
483
+ type Mock<F extends Function> = F & {
484
+ mock: MockFunctionContext<F>;
485
+ };
486
+
487
+ type NoOpFunction = (...args: any[]) => undefined;
488
+
489
+ type FunctionPropertyNames<T> = {
490
+ [K in keyof T]: T[K] extends Function ? K : never;
491
+ }[keyof T];
492
+
493
+ interface MockTracker {
494
+ /**
495
+ * This function is used to create a mock function.
496
+ * @param original An optional function to create a mock on.
497
+ * @param implementation An optional function used as the mock implementation for `original`.
498
+ * This is useful for creating mocks that exhibit one behavior for a specified number of calls and then restore the behavior of `original`.
499
+ * @param options Optional configuration options for the mock function.
500
+ */
501
+ fn<F extends Function = NoOpFunction>(original?: F, options?: MockFunctionOptions): Mock<F>;
502
+ fn<F extends Function = NoOpFunction, Implementation extends Function = F>(original?: F, implementation?: Implementation, options?: MockFunctionOptions): Mock<F | Implementation>;
503
+
504
+ /**
505
+ * This function is used to create a mock on an existing object method.
506
+ * @param object The object whose method is being mocked.
507
+ * @param methodName The identifier of the method on `object` to mock. If `object[methodName]` is not a function, an error is thrown.
508
+ * @param implementation An optional function used as the mock implementation for `object[methodName]`.
509
+ * @param options Optional configuration options for the mock method.
510
+ */
511
+ method<
512
+ MockedObject extends object,
513
+ MethodName extends FunctionPropertyNames<MockedObject>,
514
+ >(
515
+ object: MockedObject,
516
+ methodName: MethodName,
517
+ options?: MockFunctionOptions,
518
+ ): MockedObject[MethodName] extends Function
519
+ ? Mock<MockedObject[MethodName]>
520
+ : never;
521
+ method<
522
+ MockedObject extends object,
523
+ MethodName extends FunctionPropertyNames<MockedObject>,
524
+ Implementation extends Function,
525
+ >(
526
+ object: MockedObject,
527
+ methodName: MethodName,
528
+ implementation: Implementation,
529
+ options?: MockFunctionOptions,
530
+ ): MockedObject[MethodName] extends Function
531
+ ? Mock<MockedObject[MethodName] | Implementation>
532
+ : never;
533
+ method<MockedObject extends object>(
534
+ object: MockedObject,
535
+ methodName: keyof MockedObject,
536
+ options: MockMethodOptions,
537
+ ): Mock<Function>;
538
+ method<MockedObject extends object>(
539
+ object: MockedObject,
540
+ methodName: keyof MockedObject,
541
+ implementation: Function,
542
+ options: MockMethodOptions,
543
+ ): Mock<Function>;
544
+
545
+ /**
546
+ * This function is syntax sugar for {@link MockTracker.method} with `options.getter` set to `true`.
547
+ */
548
+ getter<
549
+ MockedObject extends object,
550
+ MethodName extends keyof MockedObject,
551
+ >(
552
+ object: MockedObject,
553
+ methodName: MethodName,
554
+ options?: MockFunctionOptions,
555
+ ): Mock<() => MockedObject[MethodName]>;
556
+ getter<
557
+ MockedObject extends object,
558
+ MethodName extends keyof MockedObject,
559
+ Implementation extends Function,
560
+ >(
561
+ object: MockedObject,
562
+ methodName: MethodName,
563
+ implementation?: Implementation,
564
+ options?: MockFunctionOptions,
565
+ ): Mock<(() => MockedObject[MethodName]) | Implementation>;
566
+
567
+ /**
568
+ * This function is syntax sugar for {@link MockTracker.method} with `options.setter` set to `true`.
569
+ */
570
+ setter<
571
+ MockedObject extends object,
572
+ MethodName extends keyof MockedObject,
573
+ >(
574
+ object: MockedObject,
575
+ methodName: MethodName,
576
+ options?: MockFunctionOptions,
577
+ ): Mock<(value: MockedObject[MethodName]) => void>;
578
+ setter<
579
+ MockedObject extends object,
580
+ MethodName extends keyof MockedObject,
581
+ Implementation extends Function,
582
+ >(
583
+ object: MockedObject,
584
+ methodName: MethodName,
585
+ implementation?: Implementation,
586
+ options?: MockFunctionOptions,
587
+ ): Mock<((value: MockedObject[MethodName]) => void) | Implementation>;
588
+
589
+ /**
590
+ * This function restores the default behavior of all mocks that were previously created by this `MockTracker`
591
+ * and disassociates the mocks from the `MockTracker` instance. Once disassociated, the mocks can still be used,
592
+ * but the `MockTracker` instance can no longer be used to reset their behavior or otherwise interact with them.
593
+ *
594
+ * After each test completes, this function is called on the test context's `MockTracker`.
595
+ * If the global `MockTracker` is used extensively, calling this function manually is recommended.
596
+ */
597
+ reset(): void;
598
+
599
+ /**
600
+ * This function restores the default behavior of all mocks that were previously created by this `MockTracker`.
601
+ * Unlike `mock.reset()`, `mock.restoreAll()` does not disassociate the mocks from the `MockTracker` instance.
602
+ */
603
+ restoreAll(): void;
604
+ }
605
+
606
+ const mock: MockTracker;
607
+
608
+ interface MockFunctionCall<
609
+ F extends Function,
610
+ ReturnType = F extends (...args: any) => infer T
611
+ ? T
612
+ : F extends abstract new (...args: any) => infer T
613
+ ? T
614
+ : unknown,
615
+ Args = F extends (...args: infer Y) => any
616
+ ? Y
617
+ : F extends abstract new (...args: infer Y) => any
618
+ ? Y
619
+ : unknown[],
620
+ > {
621
+ /**
622
+ * An array of the arguments passed to the mock function.
623
+ */
624
+ arguments: Args;
625
+ /**
626
+ * If the mocked function threw then this property contains the thrown value.
627
+ */
628
+ error: unknown | undefined;
629
+ /**
630
+ * The value returned by the mocked function.
631
+ *
632
+ * If the mocked function threw, it will be `undefined`.
633
+ */
634
+ result: ReturnType | undefined;
635
+ /**
636
+ * An `Error` object whose stack can be used to determine the callsite of the mocked function invocation.
637
+ */
638
+ stack: Error;
639
+ /**
640
+ * If the mocked function is a constructor, this field contains the class being constructed.
641
+ * Otherwise this will be `undefined`.
642
+ */
643
+ target: F extends abstract new (...args: any) => any ? F : undefined;
644
+ /**
645
+ * The mocked function's `this` value.
646
+ */
647
+ this: unknown;
648
+ }
649
+
650
+ interface MockFunctionContext<F extends Function> {
651
+ /**
652
+ * A getter that returns a copy of the internal array used to track calls to the mock.
653
+ */
654
+ readonly calls: Array<MockFunctionCall<F>>;
655
+
656
+ /**
657
+ * This function returns the number of times that this mock has been invoked.
658
+ * This function is more efficient than checking `ctx.calls.length`
659
+ * because `ctx.calls` is a getter that creates a copy of the internal call tracking array.
660
+ */
661
+ callCount(): number;
662
+
663
+ /**
664
+ * This function is used to change the behavior of an existing mock.
665
+ * @param implementation The function to be used as the mock's new implementation.
666
+ */
667
+ mockImplementation(implementation: Function): void;
668
+
669
+ /**
670
+ * This function is used to change the behavior of an existing mock for a single invocation.
671
+ * Once invocation `onCall` has occurred, the mock will revert to whatever behavior
672
+ * it would have used had `mockImplementationOnce()` not been called.
673
+ * @param implementation The function to be used as the mock's implementation for the invocation number specified by `onCall`.
674
+ * @param onCall The invocation number that will use `implementation`.
675
+ * If the specified invocation has already occurred then an exception is thrown.
676
+ */
677
+ mockImplementationOnce(implementation: Function, onCall?: number): void;
678
+
679
+ /**
680
+ * Resets the call history of the mock function.
681
+ */
682
+ resetCalls(): void;
683
+
684
+ /**
685
+ * Resets the implementation of the mock function to its original behavior.
686
+ * The mock can still be used after calling this function.
687
+ */
688
+ restore(): void;
689
+ }
690
+
691
+ export { test as default, run, test, describe, it, before, after, beforeEach, afterEach, mock };
446
692
  }