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

VaclavObornik / mongodash / 21110265422

18 Jan 2026 10:30AM UTC coverage: 91.594% (+0.04%) from 91.559%
21110265422

Pull #442

github

web-flow
Merge e9e0d352b into 9b7f83268
Pull Request #442: updates

1331 of 1556 branches covered (85.54%)

Branch coverage included in aggregate %.

13 of 14 new or added lines in 2 files covered. (92.86%)

2156 of 2251 relevant lines covered (95.78%)

363.04 hits per line

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

94.34
/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 = Date.now();
16✔
35
    let lastTick = Date.now();
16✔
36

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

39
    while (true) {
16✔
40
        const now = Date.now();
183✔
41

42
        // --- Time Jump Detection (Debug Support) ---
43
        const elapsedSinceLastTick = now - lastTick;
183✔
44
        // If elapsed time is significantly larger than poll interval (e.g. > 1s),
45
        // we assume the process was paused (e.g. at a breakpoint).
46
        if (elapsedSinceLastTick > 1000) {
183✔
47
            const jump = elapsedSinceLastTick - pollIntervalMs; // Approximate jump
1✔
48
            if (jump > 0) {
1!
49
                debug(`Time jump detected: ${jump}ms. Extending deadline.`);
1✔
50
                deadline += jump;
1✔
51
            }
52
        }
53
        lastTick = now;
183✔
54
        // -------------------------------------------
55

56
        if (now > deadline) {
183✔
57
            debug(`Timeout! Elapsed: ${now - start}ms`);
2✔
58
            throw new Error(`waitUntil timeout after ${timeoutMs}ms (adjusted for pauses)`);
2✔
59
        }
60

61
        let result: boolean;
62
        try {
181✔
63
            result = await condition();
181✔
64
        } catch (err) {
NEW
65
            debug(`Condition threw error:`, err);
×
66
            // error is ignored
67
            result = false; // Condition failing throws implies not met? Or should we propagate?
×
68
            // Usually waitUntil swallows errors unless critical. Let's assume false.
69
            // But if it's a logic error in condition, maybe we should throw.
70
            // For now, let's treat throw as false for robustness in shaky tests.
71
        }
72

73
        if (result) {
181✔
74
            if (stabilityDurationMs === 0) {
61✔
75
                debug(`Condition met immediately.`);
2✔
76
                return;
2✔
77
            }
78
            if (now - stableSince >= stabilityDurationMs) {
59✔
79
                debug(`Condition stable for ${now - stableSince}ms. Done.`);
12✔
80
                return;
12✔
81
            }
82
            // Condition is true but haven't been stable long enough
83
            // Continue loop
84
        } else {
85
            // Condition failed, reset stability timer
86
            if (stableSince !== now) {
120✔
87
                // Avoid spamming log every tick if it was already failing
88
                // Actually, stableSince is reset to 'now' every time it fails?
89
                // No, only when it WAS true and becomes false?
90
                // Original code: stableSince = now; on else.
91
                // So if it keeps failing, stableSince keeps moving forward.
92
            }
93
            stableSince = now;
120✔
94
        }
95

96
        await new Promise((r) => setTimeout(r, pollIntervalMs));
167✔
97
    }
98
}
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