Closed ChristianSiegert closed 1 year ago
Cool utility 😊 But what is the purpose of the test?
I use it to assert that functions set the expected date, e.g.
assertEquals(
await createUser(sql, { name: "John" }),
{ id: 1, name: "John", dateCreated: new Date(fakeTime.now)},
);
If real time is used, i.e. if the time is not controlled or frozen, reliably specifying the expected dateCreated
is impossible.
I found a solution in the meantime. Advance ticks frequently (advanceFrequency
) but do not change the fake time in a meaningful way (advanceRate
) (deno docs):
const fakeTime = new FakeTime(Date.now(), {
advanceFrequency: 0.01, // Frequency in milliseconds at which fake time is updated
advanceRate: 0.01, // Rate relative to real time at which fake time is updated
});
This way postgresjs receives ticks to do its processing but the time in my tests appears frozen, so comparing dates at millisecond resolution works even though the fake time was ticked forward automatically.
The Deno standard library provides class FakeTime to freeze/manipulate time so writing tests that involve time is easier. When using postgresjs in combination with
FakeTime
, tests fail with:Reproduction:
Run the test file above with
deno test --allow-env --allow-net --no-check --unstable test.ts
.Output:
Environment: