Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial implementation of
freezeTime
and freezeTimeAwait
- Loading branch information
Showing
8 changed files
with
141 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import dayjs from "dayjs"; | ||
import { freezeTime, freezeTimeAwait } from ".."; | ||
|
||
function nowString() { | ||
const d = new Date(); | ||
return d.toISOString(); | ||
} | ||
|
||
function dayjsNow() { | ||
return dayjs(new Date()); | ||
} | ||
|
||
const now = dayjsNow().subtract(1, "week"); | ||
|
||
[ | ||
{ time: now, title: "works with Dayjs" }, | ||
{ time: now.toDate(), title: "works with Date" } | ||
].forEach(({ time, title }) => { | ||
describe(title, () => { | ||
describe("freezeTime()", () => { | ||
it("executes the given expect inside of the function", () => { | ||
freezeTime(time, () => { | ||
expect(nowString()).toEqual(time.toISOString()); | ||
}); | ||
expect.assertions(1); | ||
}); | ||
}); | ||
|
||
describe("freezeTimeAwait()", () => { | ||
async function doShit(): Promise<string> { | ||
return Promise.resolve(dayjsNow().toISOString()); | ||
} | ||
|
||
it("executes the given expect inside of the function", async () => { | ||
await freezeTimeAwait(time, async () => { | ||
const res = await doShit(); | ||
expect(res).toEqual(time.toISOString()); | ||
}); | ||
expect.assertions(1); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,58 @@ | ||
export * from "./lib/my-lib"; | ||
import { Dayjs, isDayjs } from "dayjs"; | ||
import timekeeper from "timekeeper"; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
function getDate(time: Dayjs | Date) { | ||
return isDayjs(time) ? time.toDate() : time; | ||
} | ||
|
||
/** | ||
* Executes the given function, freezing the time first to the given `time`, and resetting the function execution (uses `timekeeper`) | ||
* | ||
* ```typescript | ||
* // assume now is "2019-03-21T12:21:13.000Z" | ||
* | ||
* function renderTime() { | ||
* const d = new Date() | ||
* console.log(d.toISOString()) | ||
* } | ||
* | ||
* renderTime() // => logs "2019-03-21T12:21:13.000Z" | ||
* | ||
* const time = new Date("2018-01-02T13:14:15.123Z") | ||
* const res = freezeTime(time, () => { renderTime(); return 'blah' }) // => logs "2018-01-02T13:14:15.123Z" | ||
* console.log(res) // => logs 'blah' (freezeTime() returns the result of the passed function) | ||
* | ||
* renderTime() // => logs "2019-03-21T12:21:13.010Z" (time is unfrozen, let's say that a 10ms have passed) | ||
* ``` | ||
* | ||
* @param time the time that will be the new "now" | ||
* @param fn function to execute, on which the time will be frozen as `time` | ||
*/ | ||
export function freezeTime<T>(time: Dayjs | Date, fn: () => T): T { | ||
timekeeper.freeze(getDate(time)); | ||
const result = fn(); | ||
timekeeper.reset(); | ||
return result; | ||
} | ||
|
||
/** | ||
* same as `freezeTime()` but it expects an async function. It will await for the function's return and return it before resetting the time | ||
* | ||
* TODO: this needs concurrency testing | ||
* | ||
* @param time the time that will be the new "now" | ||
* @param fn async function to execute, on which the time will be frozen as `time` | ||
* @see freezeTime | ||
*/ | ||
export async function freezeTimeAwait<T>( | ||
time: Dayjs | Date, | ||
fn: () => Promise<T> | ||
): Promise<T> { | ||
timekeeper.freeze(getDate(time)); | ||
const result = await fn(); | ||
timekeeper.reset(); | ||
return result; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters