Skip to content

Commit

Permalink
test(testing): improve FakeTime testing (#5123)
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k authored Jun 25, 2024
1 parent 67eaa4a commit 40bf2a5
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions testing/time_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import {
assert,
assertAlmostEquals,
assertEquals,
assertInstanceOf,
assertMatch,
Expand Down Expand Up @@ -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);
});

0 comments on commit 40bf2a5

Please sign in to comment.