BREAKING(testing): disable multiple FakeTime creations (#5130)

This commit is contained in:
Yoshiya Hinosawa 2024-06-25 14:15:25 +09:00 committed by GitHub
parent a4d4e51cfa
commit 3204a708a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 1 deletions

View File

@ -313,7 +313,7 @@ export class FakeTime {
start?: number | string | Date | null,
options?: FakeTimeOptions,
) {
if (time) time.restore();
if (time) throw new TimeError("The time is already faked");
initializedAt = _internals.Date.now();
startedAt = start instanceof Date
? start.valueOf()

View File

@ -7,6 +7,7 @@ import {
assertNotEquals,
assertRejects,
assertStrictEquals,
assertThrows,
} from "@std/assert";
import { FakeTime, TimeError } from "./time.ts";
import { _internals } from "./_time.ts";
@ -638,3 +639,8 @@ Deno.test("Date from FakeTime is structured cloneable", () => {
assert(date instanceof Date);
assert(cloned instanceof Date_);
});
Deno.test("new FakeTime() throws if the time is already faked", () => {
using _time = new FakeTime();
assertThrows(() => new FakeTime());
});