From 5ddd7d6c05957b30078dff689036e8ddaceef44c Mon Sep 17 00:00:00 2001 From: Rogier Schouten Date: Wed, 22 Nov 2017 11:22:29 +0100 Subject: [PATCH 1/5] update lolex typings. --- types/lolex/index.d.ts | 82 +++++++++++++++++++++++++------------- types/lolex/lolex-tests.ts | 29 +++++--------- 2 files changed, 64 insertions(+), 47 deletions(-) diff --git a/types/lolex/index.d.ts b/types/lolex/index.d.ts index c0dec4cd56..b033862062 100644 --- a/types/lolex/index.d.ts +++ b/types/lolex/index.d.ts @@ -1,6 +1,8 @@ -// Type definitions for lolex 1.5 +// Type definitions for lolex 2.1 // Project: https://github.com/sinonjs/lolex -// Definitions by: Wim Looman , Josh Goldberg +// Definitions by: Wim Looman +// Josh Goldberg +// Rogier Schouten // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /** @@ -34,7 +36,7 @@ type BrowserClock = LolexClock; type NodeClock = LolexClock & { /** * Mimicks process.hrtime(). - * + * * @param prevTime Previous system time to calculate time elapsed. * @returns High resolution real time as [seconds, nanoseconds]. */ @@ -49,7 +51,7 @@ type Clock = BrowserClock | NodeClock; /** * Names of clock methods that may be faked by install. */ -type FakeMethod = "setTimeout" | "clearTimeout" | "setImmediate" | "clearImmediate" | "setInterval" | "clearInterval" | "Date"; +type FakeMethod = "setTimeout" | "clearTimeout" | "setImmediate" | "clearImmediate" | "setInterval" | "clearInterval" | "Date" | "nextTick"; /** * Controls the flow of time. @@ -67,7 +69,7 @@ export interface LolexClock { /** * Schedules a callback to be fired once timeout milliseconds have ticked by. - * + * * @param callback Callback to be fired. * @param timeout How many ticks to wait to run the callback. * @param args Any extra arguments to pass to the callback. @@ -77,14 +79,14 @@ export interface LolexClock { /** * Clears a timer, as long as it was created using setTimeout. - * + * * @param id Timer ID or object. */ clearTimeout(id: TTimerId): void; /** * Schedules a callback to be fired every time timeout milliseconds have ticked by. - * + * * @param callback Callback to be fired. * @param timeout How many ticks to wait between callbacks. * @param args Any extra arguments to pass to the callback. @@ -94,14 +96,14 @@ export interface LolexClock { /** * Clears a timer, as long as it was created using setInterval. - * + * * @param id Timer ID or object. */ clearInterval(id: TTimerId): void; /** * Schedules the callback to be fired once 0 milliseconds have ticked by. - * + * * @param callback Callback to be fired. * @remarks You'll still have to call clock.tick() for the callback to fire. * @remarks If called during a tick the callback won't fire until 1 millisecond has ticked by. @@ -110,11 +112,16 @@ export interface LolexClock { /** * Clears a timer, as long as it was created using setImmediate. - * + * * @param id Timer ID or object. */ clearImmediate(id: TTimerId): void; + /** + * Simulates process.nextTick(); + */ + nextTick(callback: () => any): void; + /** * Advances the clock to the the moment of the first scheduled timer, firing it. */ @@ -122,14 +129,14 @@ export interface LolexClock { /** * Advance the clock, firing callbacks if necessary. - * + * * @param time How many ticks to advance by. */ tick(time: number | string): void; /** * Runs all pending timers until there are none remaining. - * + * * @remarks If new timers are added while it is executing they will be run as well. */ runAll(): void; @@ -142,7 +149,7 @@ export interface LolexClock { /** * Simulates a user changing the system clock. - * + * * @param now New system time. * @remarks This affects the current time but it does not in itself cause timers to fire. */ @@ -157,7 +164,7 @@ export interface LolexClock { /** * Creates a clock. - * + * * @param now Current time for the clock. * @param loopLimit Maximum number of timers that will be run when calling runAll() * before assuming that we have an infinite loop and throwing an error @@ -167,23 +174,42 @@ export interface LolexClock { */ export declare function createClock(now?: number | Date, loopLimit?: number): TClock; -/** - * Creates a clock and installs it globally. - * - * @param now Current time for the clock, as with lolex.createClock(). - * @param toFake Names of methods that should be faked. - * @type TClock Type of clock to create. - * @usage lolex.install(["setTimeout", "clearTimeout"]); - */ -export declare function install(now?: number | Date, toFake?: FakeMethod[]): TClock; + +export interface LolexInstallOpts { + /** + * installs lolex onto the specified target context (default: global) + */ + target?: any; + /** + * installs lolex with the specified unix epoch (default: 0) + */ + now?: number; + /** + * an array with explicit function names to hijack. When not set, lolex will automatically fake all methods except nextTick + * e.g., lolex.install({ toFake: ["setTimeout", "nextTick"]}) will fake only setTimeout and nextTick + */ + toFake?: FakeMethod[]; + /** + * the maximum number of timers that will be run when calling runAll() (default: 1000) + */ + loopLimit?: number; + /** + * tells lolex to increment mocked time automatically based on the real system time shift (e.g. the mocked time will be incremented by + * 20ms for every 20ms change in the real system time) (default: false) + */ + shouldAdvanceTime?: boolean; + /** + * relevant only when using with shouldAdvanceTime: true. increment mocked time by advanceTimeDelta ms every advanceTimeDelta ms change + * in the real system time (default: 20) + */ + advanceTimeDelta?: number; +} /** - * Creates a clock and installs it onto the context object. - * - * @param context Context to install the clock onto. + * Creates a clock and installs it globally. + * * @param now Current time for the clock, as with lolex.createClock(). * @param toFake Names of methods that should be faked. * @type TClock Type of clock to create. - * @usage lolex.install(context, ["setTimeout", "clearTimeout"]); */ -export declare function install(context?: any, now?: number | Date, toFake?: FakeMethod[]): TClock; +export declare function install(opts: LolexInstallOpts): TClock; diff --git a/types/lolex/lolex-tests.ts b/types/lolex/lolex-tests.ts index 876e4280c5..8039140dc5 100644 --- a/types/lolex/lolex-tests.ts +++ b/types/lolex/lolex-tests.ts @@ -16,25 +16,14 @@ lolex.createClock(new Date()); lolex.createClock(7, 9001); lolex.createClock(new Date(), 9001); -lolex.install(7); -lolex.install(new Date()); -lolex.install(7, ["setTimeout"]); -lolex.install(new Date(), ["setTimeout"]); - -lolex.install(7); -lolex.install(new Date()); -lolex.install(7, ["setTimeout"]); -lolex.install(new Date(), ["setTimeout"]); - -lolex.install({}, 7); -lolex.install({}, new Date()); -lolex.install({}, 7, ["setTimeout"]); -lolex.install({}, new Date(), ["setTimeout"]); - -lolex.install({}, 7); -lolex.install({}, new Date()); -lolex.install({}, 7, ["setTimeout"]); -lolex.install({}, new Date(), ["setTimeout"]); +lolex.install({ + advanceTimeDelta: 20, + loopLimit: 10, + now: 0, + shouldAdvanceTime: true, + target: {}, + toFake: ["setTimeout"] +}); const browserNow: number = browserClock.now; const browserDate: Date = new browserClock.Date(); @@ -80,5 +69,7 @@ nodeClock.setSystemTime(); nodeClock.setSystemTime(7); nodeClock.setSystemTime(new Date()); +nodeClock.nextTick(() => undefined); + browserClock.uninstall(); nodeClock.uninstall(); From 85205b1b8f76dca3fe0827a5242e48f627ff6f96 Mon Sep 17 00:00:00 2001 From: Rogier Schouten Date: Wed, 22 Nov 2017 11:25:57 +0100 Subject: [PATCH 2/5] bugfix in tsconfig.json - non-existing compiler option. --- types/lolex/tsconfig.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/types/lolex/tsconfig.json b/types/lolex/tsconfig.json index e9ab242410..d3832ef8ed 100644 --- a/types/lolex/tsconfig.json +++ b/types/lolex/tsconfig.json @@ -8,7 +8,6 @@ "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": false, - "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ "../" @@ -21,4 +20,4 @@ "index.d.ts", "lolex-tests.ts" ] -} \ No newline at end of file +} From c6798928cb5f078d1ff655e244c448973e7f4523 Mon Sep 17 00:00:00 2001 From: Rogier Schouten Date: Wed, 22 Nov 2017 16:17:30 +0100 Subject: [PATCH 3/5] Also add 'hrtime' method. --- types/lolex/index.d.ts | 2 +- types/lolex/lolex-tests.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/types/lolex/index.d.ts b/types/lolex/index.d.ts index b033862062..c0688a5b09 100644 --- a/types/lolex/index.d.ts +++ b/types/lolex/index.d.ts @@ -51,7 +51,7 @@ type Clock = BrowserClock | NodeClock; /** * Names of clock methods that may be faked by install. */ -type FakeMethod = "setTimeout" | "clearTimeout" | "setImmediate" | "clearImmediate" | "setInterval" | "clearInterval" | "Date" | "nextTick"; +type FakeMethod = "setTimeout" | "clearTimeout" | "setImmediate" | "clearImmediate" | "setInterval" | "clearInterval" | "Date" | "nextTick" | "hrtime"; /** * Controls the flow of time. diff --git a/types/lolex/lolex-tests.ts b/types/lolex/lolex-tests.ts index 8039140dc5..95b6a2be78 100644 --- a/types/lolex/lolex-tests.ts +++ b/types/lolex/lolex-tests.ts @@ -22,7 +22,7 @@ lolex.install({ now: 0, shouldAdvanceTime: true, target: {}, - toFake: ["setTimeout"] + toFake: ["setTimeout", "nextTick", "hrtime"] }); const browserNow: number = browserClock.now; From 2923550d2611e4553c1d03e26a06242e95e658df Mon Sep 17 00:00:00 2001 From: Rogier Schouten Date: Wed, 22 Nov 2017 16:18:29 +0100 Subject: [PATCH 4/5] Revert "bugfix in tsconfig.json - non-existing compiler option." This reverts commit 85205b1b8f76dca3fe0827a5242e48f627ff6f96. --- types/lolex/tsconfig.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/types/lolex/tsconfig.json b/types/lolex/tsconfig.json index d3832ef8ed..e9ab242410 100644 --- a/types/lolex/tsconfig.json +++ b/types/lolex/tsconfig.json @@ -8,6 +8,7 @@ "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": false, + "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ "../" @@ -20,4 +21,4 @@ "index.d.ts", "lolex-tests.ts" ] -} +} \ No newline at end of file From 6ad9ba8e3c1e881462901da876507641f6bc0615 Mon Sep 17 00:00:00 2001 From: Rogier Schouten Date: Thu, 23 Nov 2017 09:48:33 +0100 Subject: [PATCH 5/5] review comment fixes --- types/lolex/index.d.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/types/lolex/index.d.ts b/types/lolex/index.d.ts index c0688a5b09..510cb911ec 100644 --- a/types/lolex/index.d.ts +++ b/types/lolex/index.d.ts @@ -120,7 +120,7 @@ export interface LolexClock { /** * Simulates process.nextTick(); */ - nextTick(callback: () => any): void; + nextTick(callback: () => void): void; /** * Advances the clock to the the moment of the first scheduled timer, firing it. @@ -177,29 +177,34 @@ export declare function createClock(now?: number | Date, l export interface LolexInstallOpts { /** - * installs lolex onto the specified target context (default: global) + * Installs lolex onto the specified target context (default: global) */ target?: any; + /** - * installs lolex with the specified unix epoch (default: 0) + * Installs lolex with the specified unix epoch (default: 0) */ now?: number; + /** - * an array with explicit function names to hijack. When not set, lolex will automatically fake all methods except nextTick + * An array with explicit function names to hijack. When not set, lolex will automatically fake all methods except nextTick * e.g., lolex.install({ toFake: ["setTimeout", "nextTick"]}) will fake only setTimeout and nextTick */ toFake?: FakeMethod[]; + /** - * the maximum number of timers that will be run when calling runAll() (default: 1000) + * The maximum number of timers that will be run when calling runAll() (default: 1000) */ loopLimit?: number; + /** - * tells lolex to increment mocked time automatically based on the real system time shift (e.g. the mocked time will be incremented by + * Tells lolex to increment mocked time automatically based on the real system time shift (e.g. the mocked time will be incremented by * 20ms for every 20ms change in the real system time) (default: false) */ shouldAdvanceTime?: boolean; + /** - * relevant only when using with shouldAdvanceTime: true. increment mocked time by advanceTimeDelta ms every advanceTimeDelta ms change + * Relevant only when using with shouldAdvanceTime: true. increment mocked time by advanceTimeDelta ms every advanceTimeDelta ms change * in the real system time (default: 20) */ advanceTimeDelta?: number;