File size: 539 Bytes
d9494a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
type ExpectEventuallyOptions = {
  timeoutMs?: number;
  intervalMs?: number;
};

export const expectEventually = async (
  assertion: () => Promise<void> | void,
  { timeoutMs = 10_000, intervalMs = 100 }: ExpectEventuallyOptions = {},
): Promise<void> => {
  const startedAt = Date.now();

  for (;;) {
    try {
      await assertion();

      return;
    } catch (error) {
      if (Date.now() - startedAt > timeoutMs) {
        throw error;
      }

      await new Promise((resolve) => setTimeout(resolve, intervalMs));
    }
  }
};