BREAKING(testing): replace TimeError exception in favor of built-in error classes in some cases (#5550)

* BREAKING(testing): remove `TimeError` in favor of built-in error classes

* work

* refactor: use `Error` instead of `TypeError`

* work

* fix

* work

* work

* fix

* fix
This commit is contained in:
Asher Gomez 2024-07-29 14:48:44 +10:00 committed by GitHub
parent 2b33b55881
commit 7eed8d0e04
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 8 deletions

View File

@ -44,7 +44,8 @@ import { _internals } from "./_time.ts";
export type { DelayOptions };
/**
* An error related to faking time.
* Represents an error when trying to execute an invalid operation on fake time,
* given the state fake time is in.
*
* @example Usage
* ```ts
@ -52,7 +53,9 @@ export type { DelayOptions };
* import { assertThrows } from "@std/assert";
*
* assertThrows(() => {
* new FakeTime(NaN);
* const time = new FakeTime();
* time.restore();
* time.restore();
* }, TimeError);
* ```
*/
@ -281,8 +284,11 @@ export class FakeTime {
* Construct a FakeTime object. This overrides the real Date object and timer functions with fake ones that can be
* controlled through the fake time instance.
*
* @param start The time to simulate. The default is the current time..
* @param start The time to simulate. The default is the current time.
* @param options The options
*
* @throws {TimeError} If time is already faked
* @throws {TypeError} If the start is invalid
*/
constructor(
start?: number | string | Date | null,
@ -297,7 +303,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 TypeError("Invalid start time");
now = startedAt;
timerId = timerIdGen();
@ -356,6 +362,8 @@ export class FakeTime {
/**
* Restores real time.
*
* @throws {TimeError} If time is already restored
*
* @example Usage
* ```ts
* import { FakeTime } from "@std/testing/time";
@ -380,6 +388,8 @@ export class FakeTime {
/**
* Restores real time temporarily until callback returns and resolves.
*
* @throws {TimeError} If time is not faked
*
* @example Usage
* ```ts
* import { FakeTime } from "@std/testing/time";
@ -449,6 +459,8 @@ export class FakeTime {
* Set the current time. It will call any functions waiting to be called between the current and new fake time.
* If the timer callback throws, time will stop advancing forward beyond that timer.
*
* @throws {RangeError} If the time goes backwards
*
* @example Usage
* ```ts
* import { FakeTime } from "@std/testing/time";
@ -466,7 +478,7 @@ export class FakeTime {
* @param value The current time (in milliseconds)
*/
set now(value: number) {
if (value < now) throw new Error("time cannot go backwards");
if (value < now) throw new RangeError("Time cannot go backwards");
let dueNode: DueNode | null = dueTree.min();
while (dueNode && dueNode.due <= value) {
const timer: Timer | undefined = dueNode.timers.shift();

View File

@ -700,7 +700,7 @@ 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), TypeError, "Invalid start time");
});
Deno.test("FakeTime.restore() throws when the time is already restored", () => {
@ -721,8 +721,8 @@ Deno.test("time.now = N throws when N < time.now", () => {
() => {
time.now = 999;
},
Error,
"time cannot go backwards",
RangeError,
"Time cannot go backwards",
);
});