Skip to content

Commit

Permalink
[node] v20.11 updates
Browse files Browse the repository at this point in the history
- test_runner: Add Date to the supported mock APIs // copied from DefinitelyTyped#67035
- esm: add import.meta.dirname and import.meta.filename // changed to non-optional fields
  • Loading branch information
Semigradsky committed Jan 13, 2024
1 parent 15024fd commit 5ccf4ab
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 11 deletions.
4 changes: 2 additions & 2 deletions types/node/module.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,13 @@ declare module "module" {
* The directory name of the current module. This is the same as the `path.dirname()` of the `import.meta.filename`.
* **Caveat:** only present on `file:` modules.
*/
dirname?: string;
dirname: string;
/**
* The full absolute path and filename of the current module, with symlinks resolved.
* This is the same as the `url.fileURLToPath()` of the `import.meta.url`.
* **Caveat:** only local modules support this property. Modules not using the `file:` protocol will not provide it.
*/
filename?: string;
filename: string;
/**
* The absolute `file:` URL of the module.
*/
Expand Down
31 changes: 29 additions & 2 deletions types/node/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,12 @@ declare module "node:test" {
*/
restore(): void;
}
type Timer = "setInterval" | "clearInterval" | "setTimeout" | "clearTimeout";
type Timer = "setInterval" | "setTimeout" | "setImmediate" | "Date";

interface MockTimersOptions {
apis: Timer[];
now?: number | Date;
}
/**
* Mocking timers is a technique commonly used in software testing to simulate and
* control the behavior of timers, such as `setInterval` and `setTimeout`,
Expand Down Expand Up @@ -1066,7 +1071,29 @@ declare module "node:test" {
* and `globalThis` will be mocked. As well as the global `Date` object.
* @since v20.4.0
*/
enable(timers?: Timer[]): void;
enable(options?: MockTimersOptions): void;
/**
* Sets the current Unix timestamp that will be used as reference for any mocked`Date` objects.
*
* ```js
* import assert from 'node:assert';
* import { test } from 'node:test';
*
* test('runAll functions following the given order', (context) => {
* const now = Date.now();
* const setTime = 1000;
* // Date.now is not mocked
* assert.deepStrictEqual(Date.now(), now);
*
* context.mock.timers.enable({ apis: ['Date'] });
* context.mock.timers.setTime(setTime);
* // Date.now is now 1000
* assert.strictEqual(Date.now(), setTime);
* });
* ```
* @since v20.11.0
*/
setTime(time: number): void;
/**
* This function restores the default behavior of all mocks that were previously
* created by this `MockTimers` instance and disassociates the mocks
Expand Down
2 changes: 2 additions & 0 deletions types/node/test/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ const entry: Module.SourceMapping = smap.findEntry(1, 1);
// global
{
const importmeta: ImportMeta = {} as any; // Fake because we cannot really access the true `import.meta` with the current build target
importmeta.dirname; // $ExpectType string
importmeta.filename; // $ExpectType string
importmeta.url; // $ExpectType string
importmeta.resolve("local"); // $ExpectType string
importmeta.resolve("local", "/parent"); // $ExpectType string
Expand Down
18 changes: 17 additions & 1 deletion types/node/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,10 +662,26 @@ new lcov();

describe("Mock Timers Test Suite", () => {
it((t) => {
t.mock.timers.enable({ apis: ["setTimeout"] });
// new Date suite
t.mock.timers.enable({ apis: ["Date"] });
// immediates suite
t.mock.timers.enable({ apis: ["setImmediate"] });
t.mock.timers.enable({ apis: ["setTimeout"], now: 1000 });
t.mock.timers.enable({ apis: ["setTimeout"], now: new Date() });
t.mock.timers.enable();
// @ts-expect-error
t.mock.timers.enable(["setTimeout"]);
// @ts-expect-error
t.mock.timers.enable(["DOES_NOT_EXIST"]);
t.mock.timers.enable({ apis: ["NOT_THERE"] });
t.mock.timers.enable();
t.mock.timers.setTime(1000);
// @ts-expect-error
t.mock.timers.setTime("1000");
// @ts-expect-error
t.mock.timers.setTime(new Date());
// @ts-expect-error
t.mock.timers.setTime();
t.mock.timers.reset();
t.mock.timers.tick(1000);
});
Expand Down
4 changes: 2 additions & 2 deletions types/node/ts4.8/module.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,13 @@ declare module "module" {
* The directory name of the current module. This is the same as the `path.dirname()` of the `import.meta.filename`.
* **Caveat:** only present on `file:` modules.
*/
dirname?: string;
dirname: string;
/**
* The full absolute path and filename of the current module, with symlinks resolved.
* This is the same as the `url.fileURLToPath()` of the `import.meta.url`.
* **Caveat:** only local modules support this property. Modules not using the `file:` protocol will not provide it.
*/
filename?: string;
filename: string;
/**
* The absolute `file:` URL of the module.
*/
Expand Down
31 changes: 29 additions & 2 deletions types/node/ts4.8/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,12 @@ declare module "node:test" {
*/
restore(): void;
}
type Timer = "setInterval" | "clearInterval" | "setTimeout" | "clearTimeout";
type Timer = "setInterval" | "setTimeout" | "setImmediate" | "Date";

interface MockTimersOptions {
apis: Timer[];
now?: number | Date;
}
/**
* Mocking timers is a technique commonly used in software testing to simulate and
* control the behavior of timers, such as `setInterval` and `setTimeout`,
Expand Down Expand Up @@ -1066,7 +1071,29 @@ declare module "node:test" {
* and `globalThis` will be mocked. As well as the global `Date` object.
* @since v20.4.0
*/
enable(timers?: Timer[]): void;
enable(options?: MockTimersOptions): void;
/**
* Sets the current Unix timestamp that will be used as reference for any mocked`Date` objects.
*
* ```js
* import assert from 'node:assert';
* import { test } from 'node:test';
*
* test('runAll functions following the given order', (context) => {
* const now = Date.now();
* const setTime = 1000;
* // Date.now is not mocked
* assert.deepStrictEqual(Date.now(), now);
*
* context.mock.timers.enable({ apis: ['Date'] });
* context.mock.timers.setTime(setTime);
* // Date.now is now 1000
* assert.strictEqual(Date.now(), setTime);
* });
* ```
* @since v20.11.0
*/
setTime(time: number): void;
/**
* This function restores the default behavior of all mocks that were previously
* created by this `MockTimers` instance and disassociates the mocks
Expand Down
2 changes: 2 additions & 0 deletions types/node/ts4.8/test/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ const entry: Module.SourceMapping = smap.findEntry(1, 1);
// global
{
const importmeta: ImportMeta = {} as any; // Fake because we cannot really access the true `import.meta` with the current build target
importmeta.dirname; // $ExpectType string
importmeta.filename; // $ExpectType string
importmeta.url; // $ExpectType string
importmeta.resolve("local"); // $ExpectType string
importmeta.resolve("local", "/parent"); // $ExpectType string
Expand Down
22 changes: 20 additions & 2 deletions types/node/ts4.8/test/test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Transform, TransformCallback, TransformOptions } from "node:stream";
import { after, afterEach, before, beforeEach, describe, it, Mock, mock, only, run, skip, test, todo } from "node:test";
import { dot, junit, spec, tap, TestEvent } from "node:test/reporters";
import { dot, junit, lcov, spec, tap, TestEvent } from "node:test/reporters";

// run without options
// $ExpectType TestsStream
Expand Down Expand Up @@ -657,13 +657,31 @@ new spec();
junit();
// $ExpectType AsyncGenerator<string, void, unknown>
junit("" as any);
// $ExpectType Lcov
new lcov();

describe("Mock Timers Test Suite", () => {
it((t) => {
t.mock.timers.enable({ apis: ["setTimeout"] });
// new Date suite
t.mock.timers.enable({ apis: ["Date"] });
// immediates suite
t.mock.timers.enable({ apis: ["setImmediate"] });
t.mock.timers.enable({ apis: ["setTimeout"], now: 1000 });
t.mock.timers.enable({ apis: ["setTimeout"], now: new Date() });
t.mock.timers.enable();
// @ts-expect-error
t.mock.timers.enable(["setTimeout"]);
// @ts-expect-error
t.mock.timers.enable(["DOES_NOT_EXIST"]);
t.mock.timers.enable({ apis: ["NOT_THERE"] });
t.mock.timers.enable();
t.mock.timers.setTime(1000);
// @ts-expect-error
t.mock.timers.setTime("1000");
// @ts-expect-error
t.mock.timers.setTime(new Date());
// @ts-expect-error
t.mock.timers.setTime();
t.mock.timers.reset();
t.mock.timers.tick(1000);
});
Expand Down

0 comments on commit 5ccf4ab

Please sign in to comment.