mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
test(testing): improve FakeTime
testing (#5123)
This commit is contained in:
parent
67eaa4ace2
commit
40bf2a57b4
@ -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);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user