test(testing): improve FakeTime testing (#5123)

This commit is contained in:
Yoshiya Hinosawa 2024-06-25 15:51:24 +09:00 committed by GitHub
parent 67eaa4ace2
commit 40bf2a57b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,6 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import {
assert,
assertAlmostEquals,
assertEquals,
assertInstanceOf,
assertMatch,
@ -644,3 +645,80 @@ Deno.test("new FakeTime() throws if the time is already faked", () => {
using _time = new FakeTime();
assertThrows(() => new FakeTime());
});
Deno.test("Faked timer functions throws when called after FakeTime is restored", () => {
let fakeSetTimeout: typeof setTimeout;
let fakeClearTimeout: typeof clearTimeout;
let fakeSetInterval: typeof setInterval;
let fakeClearInterval: typeof clearInterval;
{
using _time: FakeTime = new FakeTime();
fakeSetTimeout = setTimeout;
fakeClearTimeout = clearTimeout;
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");
});
Deno.test("Faked Date.now returns real time after FakeTime is restored", () => {
let fakeDateNow: typeof Date.now;
{
using _time: FakeTime = new FakeTime();
fakeDateNow = Date.now;
}
assertAlmostEquals(Date.now(), fakeDateNow());
});
Deno.test("FakeTime can be constructed with number, Date, or string", () => {
{
using _time = new FakeTime(1000);
assertEquals(Date.now(), 1000);
}
{
using _time = new FakeTime(new Date(2000));
assertEquals(Date.now(), 2000);
}
{
using _time = new FakeTime("Thu Jan 01 1970 00:00:03 GMT+0000");
assertEquals(Date.now(), 3000);
}
});
Deno.test("FakeTime throws when NaN is provided", () => {
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");
});
Deno.test("time.restore() throws when the time is already restored", () => {
const time = new FakeTime();
time.restore();
assertThrows(() => time.restore(), TimeError, "time already restored");
});
Deno.test("time.now = N throws when N < time.now", () => {
using time = new FakeTime(1000);
assertThrows(
() => {
time.now = 999;
},
Error,
"time cannot go backwards",
);
});
Deno.test("time.start returns the started time of the fake time", () => {
using time = new FakeTime(1000);
time.now = 2000;
assertEquals(time.start, 1000);
});