Skip to content

Commit 36ff4b4

Browse files
committed
Keep time a-tickin'
1 parent 7ae1957 commit 36ff4b4

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

src/index.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ export async function sleepUntil(
4949
}
5050

5151
// Wait for condition to pass
52-
const start = Date.now();
53-
while (Date.now() - start < maxDuration) {
52+
const start = realNow();
53+
while (realNow() - start < maxDuration) {
5454
await sleep(pollInterval);
5555
if (condition()) {
5656
// Success
@@ -100,8 +100,17 @@ function realNow() {
100100
* offset.
101101
*/
102102
export function setupFakeTimers() {
103-
beforeEach(() => void jest.useFakeTimers());
104-
afterEach(() => void jest.useRealTimers());
103+
let interval: ReturnType<typeof setInterval> | null = null;
104+
beforeEach(() => {
105+
interval = setInterval(() => {
106+
jest.advanceTimersByTime(0);
107+
}, 10);
108+
jest.useFakeTimers();
109+
});
110+
afterEach(() => {
111+
void jest.useRealTimers();
112+
if (interval != null) clearInterval(interval);
113+
});
105114

106115
/**
107116
* Sets the fake time such that a call to `Date.now()` (or `new Date()`)

0 commit comments

Comments
 (0)