refactor(testing): improve FakeTime error messaging (#5533)

* refactor(time): improve `FakeTime` error messaging

* fix

* fix
This commit is contained in:
Asher Gomez 2024-07-24 16:48:23 +10:00 committed by GitHub
parent ff2f1f1946
commit 3f318935c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 17 deletions

View File

@ -131,12 +131,12 @@ function fakeSetTimeout(
// deno-lint-ignore no-explicit-any
...args: any[]
): number {
if (!time) throw new TimeError("no fake time");
if (!time) throw new TimeError("Time is not faked");
return setTimer(callback, delay, args, false);
}
function fakeClearTimeout(id?: unknown) {
if (!time) throw new TimeError("no fake time");
if (!time) throw new TimeError("Time is not faked");
if (typeof id === "number" && dueNodes.has(id)) {
dueNodes.delete(id);
}
@ -149,12 +149,12 @@ function fakeSetInterval(
// deno-lint-ignore no-explicit-any
...args: any[]
): number {
if (!time) throw new TimeError("no fake time");
if (!time) throw new TimeError("Time is not faked");
return setTimer(callback, delay, args, true);
}
function fakeClearInterval(id?: unknown) {
if (!time) throw new TimeError("no fake time");
if (!time) throw new TimeError("Time is not faked");
if (typeof id === "number" && dueNodes.has(id)) {
dueNodes.delete(id);
}
@ -288,7 +288,7 @@ export class FakeTime {
start?: number | string | Date | null,
options?: FakeTimeOptions,
) {
if (time) throw new TimeError("The time is already faked");
if (time) throw new TimeError("Time is already faked");
initializedAt = _internals.Date.now();
startedAt = start instanceof Date
? start.valueOf()
@ -297,7 +297,7 @@ export class FakeTime {
: typeof start === "string"
? (new Date(start)).valueOf()
: initializedAt;
if (Number.isNaN(startedAt)) throw new TimeError("invalid start");
if (Number.isNaN(startedAt)) throw new TimeError("Invalid start");
now = startedAt;
timerId = timerIdGen();
@ -373,7 +373,7 @@ export class FakeTime {
* ```
*/
static restore() {
if (!time) throw new TimeError("time already restored");
if (!time) throw new TimeError("Time is already restored");
time.restore();
}
@ -407,7 +407,7 @@ export class FakeTime {
// deno-lint-ignore no-explicit-any
...args: any[]
): Promise<T> {
if (!time) return Promise.reject(new TimeError("no fake time"));
if (!time) return Promise.reject(new TimeError("Time is not faked"));
restoreGlobals();
try {
const result = callback.apply(null, args);
@ -791,7 +791,7 @@ export class FakeTime {
* ```
*/
restore() {
if (!time) throw new TimeError("time already restored");
if (!time) throw new TimeError("Time is already restored");
time = undefined;
restoreGlobals();
if (advanceIntervalId) clearInterval(advanceIntervalId);

View File

@ -339,7 +339,7 @@ Deno.test("FakeTime.restoreFor() returns promise that rejected to TimeError if F
await assertRejects(
() => FakeTime.restoreFor(() => {}),
TimeError,
"no fake time",
"Time is not faked",
);
});
@ -659,10 +659,18 @@ Deno.test("Faked timer functions throws when called after FakeTime is restored",
fakeSetInterval = setInterval;
fakeClearInterval = clearInterval;
}
assertThrows(() => fakeSetTimeout(() => {}, 0), TimeError, "no fake time");
assertThrows(() => fakeClearTimeout(0), TimeError, "no fake time");
assertThrows(() => fakeSetInterval(() => {}, 0), TimeError, "no fake time");
assertThrows(() => fakeClearInterval(0), TimeError, "no fake time");
assertThrows(
() => fakeSetTimeout(() => {}, 0),
TimeError,
"Time is not faked",
);
assertThrows(() => fakeClearTimeout(0), TimeError, "Time is not faked");
assertThrows(
() => fakeSetInterval(() => {}, 0),
TimeError,
"Time is not faked",
);
assertThrows(() => fakeClearInterval(0), TimeError, "Time is not faked");
});
Deno.test("Faked Date.now returns real time after FakeTime is restored", () => {
@ -692,19 +700,19 @@ Deno.test("FakeTime can be constructed with number, Date, or string", () => {
});
Deno.test("FakeTime throws when NaN is provided", () => {
assertThrows(() => new FakeTime(NaN), TimeError, "invalid start");
assertThrows(() => new FakeTime(NaN), TimeError, "Invalid start");
});
Deno.test("FakeTime.restore() throws when the time is already restored", () => {
const _time = new FakeTime();
FakeTime.restore();
assertThrows(() => FakeTime.restore(), TimeError, "time already restored");
assertThrows(() => FakeTime.restore(), TimeError, "Time is already restored");
});
Deno.test("time.restore() throws when the time is already restored", () => {
const time = new FakeTime();
time.restore();
assertThrows(() => time.restore(), TimeError, "time already restored");
assertThrows(() => time.restore(), TimeError, "Time is already restored");
});
Deno.test("time.now = N throws when N < time.now", () => {