• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

VaclavObornik / mongodash / 21110490286

18 Jan 2026 10:49AM UTC coverage: 91.468% (-0.1%) from 91.594%
21110490286

Pull #443

github

web-flow
Merge faa1140bb into 023eea10f
Pull Request #443: updates

1330 of 1556 branches covered (85.48%)

Branch coverage included in aggregate %.

11 of 15 new or added lines in 1 file covered. (73.33%)

1 existing line in 1 file now uncovered.

2154 of 2253 relevant lines covered (95.61%)

361.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.09
/src/testing/waitUntil.ts
1
import * as _debug from 'debug';
244✔
2
const debug = _debug('mongodash:testing');
244✔
3

4
export interface WaitUntilOptions {
5
    /**
6
     * Maximum time to wait for the condition to become true.
7
     * Default: 10000ms
8
     */
9
    timeoutMs?: number;
10
    /**
11
     * Interval between checks.
12
     * Default: 50ms
13
     */
14
    pollIntervalMs?: number;
15
    /**
16
     * How long the condition must remain true to be considered stable.
17
     * Default: 0 (no stability check)
18
     */
19
    stabilityDurationMs?: number;
20
}
21

22
/**
23
 * Waits until the provided condition function returns true.
24
 *
25
 * Includes "Time Jump Detection" to handle debugging sessions:
26
 * If a significant time gap is detected between checks (likely due to a breakpoint),
27
 * the timeout deadline is extended by that gap to prevent false timeouts.
28
 */
29
export async function waitUntil(condition: () => boolean | Promise<boolean>, options: WaitUntilOptions = {}): Promise<void> {
244✔
30
    const { timeoutMs = 10000, pollIntervalMs = 50, stabilityDurationMs = 0 } = options;
16✔
31

32
    const start = Date.now();
16✔
33
    let deadline = start + timeoutMs;
16✔
34
    let stableSince: number | null = null;
16✔
35

36
    debug(`Started. Timeout: ${timeoutMs}ms, Poll: ${pollIntervalMs}ms, Stability: ${stabilityDurationMs}ms`);
16✔
37

38
    while (true) {
16✔
39
        const now = Date.now();
188✔
40

41
        if (now > deadline) {
188✔
42
            debug(`Timeout! Elapsed: ${now - start}ms`);
2✔
43
            throw new Error(`waitUntil timeout after ${timeoutMs}ms (adjusted for pauses)`);
2✔
44
        }
45

46
        let result: boolean;
47
        try {
186✔
48
            result = await condition();
186✔
49
        } catch (err) {
50
            debug(`Condition threw error:`, err);
×
NEW
51
            result = false;
×
52
        }
53

54
        if (result) {
186✔
55
            if (stabilityDurationMs === 0) {
71✔
56
                debug(`Condition met immediately.`);
2✔
57
                return;
2✔
58
            }
59
            if (stableSince === null) {
69✔
60
                stableSince = now;
22✔
61
            } else if (now - stableSince >= stabilityDurationMs) {
47✔
62
                debug(`Condition stable for ${now - stableSince}ms. Done.`);
12✔
63
                return;
12✔
64
            }
65
        } else {
66
            if (stableSince !== null) {
115✔
67
                debug(`Condition failed, resetting stability timer.`);
10✔
68
            }
69
            stableSince = null;
115✔
70
        }
71

72
        // --- Time Jump Detection (Debug Support) ---
73
        // Only measure time jump DURING the sleep, to avoid counting slow condition checks as "debugger pauses".
74
        const sleepStart = Date.now();
172✔
75
        await new Promise((r) => setTimeout(r, pollIntervalMs));
172✔
76
        const sleepEnd = Date.now();
172✔
77
        const actualSleep = sleepEnd - sleepStart;
172✔
78

79
        // If actual sleep is significantly larger than requested (e.g. > 1s extra),
80
        // we assume the process was paused (e.g. at a breakpoint) or system was suspended.
81
        if (actualSleep > pollIntervalMs + 1000) {
172!
NEW
82
            const jump = actualSleep - pollIntervalMs;
×
NEW
83
            debug(`Time jump detected: ${jump}ms. Extending deadline.`);
×
NEW
84
            deadline += jump;
×
85
        }
86
        // -------------------------------------------
87
    }
88
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc