Skip to content

Commit 4ae86fa

Browse files
authored
Keep time a-tickin' (#3)
* Keep time a-tickin' * Lint
1 parent 7ae1957 commit 4ae86fa

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

src/index.ts

Lines changed: 15 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,19 @@ 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) {
113+
clearInterval(interval);
114+
}
115+
});
105116

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

0 commit comments

Comments
 (0)