fix: Refactor for bailing out wait time - #2162
Conversation
Greptile SummaryRefactors matcher polling to retain the complete comparison context and terminate immediately when further retries cannot change the outcome.
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains; the previously reported abort path now returns directly, preserves the comparison context, and is covered for both assertion polarities with a single condition invocation.
|
| Filename | Overview |
|---|---|
| src/util/waitUntil.ts | Returns structured condition context and now exits directly on abort without allowing the retry handler to intercept it. |
| src/util/executeCommand.ts | Standardizes comparison result fields and marks terminal empty-subject or cardinality-mismatch outcomes for early bailout. |
| src/utils.ts | Adapts shared comparison and boolean-matcher helpers to the structured result contract. |
| test/util/waitUntil.test.ts | Covers immediate abort behavior for both assertion polarities and verifies that the condition executes only once. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Matcher invokes waitUntil] --> B[Evaluate condition]
B --> C[Structured strategy result]
C --> D{Assertion polarity satisfied?}
D -->|Yes| E[Return subject, actual, and success]
D -->|No| F{Abort requested?}
F -->|Yes| G[Return immediately without retry]
F -->|No| H[Sleep and retry]
H --> B
Reviews (9): Last reviewed commit: "code review" | Re-trigger Greptile
|
@greptileai, double-check deeply if modification can brings side effect or regressions |
|
@greptileai, to align both |
|
Looking at the current type definitions from the diff: export type CompareResult<T> = { result: boolean; value: T }
export type StrategyResult<T, E = ...> = { subject: E; success: boolean; actual: T | undefined; abort?: boolean; }
export type StrategyMaybeArrayResult<T, E = ...> = { subject: E; success: boolean; actual: MaybeArray<T> | undefined; abort?: boolean; }The friction points are:
My recommendation: Unify export type CompareResult<T> = { success: boolean; actual: T }This lets Collapse export type StrategyResult<T, E = WebdriverIO.Element | WebdriverIO.ElementArray | WebdriverIO.Element[] | WebdriverIO.Browser | unknown> = {
subject: E;
success: boolean;
actual: T | undefined;
abort?: boolean;
}Callers that need array actuals use The end result: |
|
@greptileai, do you see any further changes or simplifications we should make to CompareResult & StrategyResult to streamline the code? |
|
A few concrete simplifications stand out: 1. Both share export type StrategyResult<T, E = unknown> = CompareResult<T | undefined> & {
subject: E;
abort?: boolean;
}This removes the duplication and makes the relationship between the two types explicit. 2. The default type for // current — the union is vacuous because `unknown` subsumes everything
E = WebdriverIO.Element | WebdriverIO.ElementArray | WebdriverIO.Element[] | WebdriverIO.Browser | unknownSince export type SubjectType = WebdriverIO.Element | WebdriverIO.ElementArray | WebdriverIO.Element[] | WebdriverIO.Browser
export type StrategyResult<T, E = SubjectType | unknown> = ...3. Avoid the mutation in In result.success = !isNot // mutates the caller's object
return resultPrefer a spread to make the return value a new object: return { ...result, success: !isNot }4.
The highest-value change here is #1 — it removes structural duplication and means any future field added to |
|
@greptileai, do you see any further changes or simplifications we should make to CompareResult & StrategyResult to streamline the code? You can skip |
|
@greptileai, do you see any further changes or simplifications? |
As pointed out by Greptile in Bug B here, implementing a bail-out mechanism to abort earlier in cases where we know we will fail and do not need to exhaust all the wait time.
By refactoring
waitUntilto receive the full context, we can enhance its behaviour while still returning the appropriate fetched actual and subject (like an awaited element).This also streamlines the code instead of using a local variable to capture modified actual & elements (subject)