Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(testing): improve FakeTime testing #5123

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions testing/time_test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import {
assert,
assertAlmostEquals,
assertEquals,
assertInstanceOf,
assertNotEquals,
assertRejects,
assertStrictEquals,
assertThrows,
} from "@std/assert";
import { FakeTime, TimeError } from "./time.ts";
import { _internals } from "./_time.ts";
Expand Down Expand Up @@ -632,3 +634,88 @@ Deno.test("Date from FakeTime is structured cloneable", () => {
assert(date instanceof Date);
assert(cloned instanceof Date_);
});

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 called multiple times (later call is effective)", () => {
const now = Date.now();
try {
const _time1 = new FakeTime(1000);
const _time2 = new FakeTime(2000);
assertEquals(Date.now(), 2000);
} finally {
FakeTime.restore();
}
kt3k marked this conversation as resolved.
Show resolved Hide resolved
assert(Date.now() >= now);
});

Deno.test("FakeTime can be constructed with number, Date, or string", () => {
try {
const _time1 = new FakeTime(1000);
assertEquals(Date.now(), 1000);
const _time2 = new FakeTime(new Date(2000));
assertEquals(Date.now(), 2000);
const _time3 = new FakeTime("Thu Jan 01 1970 00:00:03 GMT+0000");
assertEquals(Date.now(), 3000);
} finally {
FakeTime.restore();
}
kt3k marked this conversation as resolved.
Show resolved Hide resolved
});

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");
});
iuioiua marked this conversation as resolved.
Show resolved Hide resolved

Deno.test("time.now = N throws when N < time.now", () => {
using time = new FakeTime(1000);
assertThrows(
() => {
time.now = 999;
},
Error,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this PR, but we should throw RangeError.

"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);
});