← Back to Pull requests

Stabilize flaky waitForNextUpdate hook unit tests #10054

Closed
unknown closed this pull request without merging on Oct 07, 2026 16:31 UTC
View on GitHub
unknown unknown commented on Oct 01, 2026 22:10 UTC
Author

Fixes: https://issues.redhat.com/browse/RHOAIENG-97653

Problem

Hook unit tests that call waitForNextUpdate() intermittently assert stale state under full-workspace / contended runs. The hook under test has already rendered the next value (including error paths), but assertions on RTL’s result.current still see the previous (often initial) value — e.g. error: undefined when the wrapper already produced an error.

Root timing gap: the helper treated a render-phase counter bump as “update ready,” while @testing-library/react only assigns result.current in a post-commit useEffect. Under load, waitForNextUpdate can resolve in that window, so callers race RTL’s publish. An audit tied this to dozens of waitForNextUpdate call sites across the dashboard and feature packages (see RHOAIENG-97653).

A naive fix that also required result.current === currentResult (or “not the value at wait start”) is too strict or too loose for hooks that return a new array/object identity every render (e.g. FetchState-style tuples), which is why those alternatives were abandoned.

User / product impact

No user-visible product behavior change. Impact is on developers and CI:

  • Hook unit tests that use waitForNextUpdate should stop flaking on stale result.current.

  • The same renderHook fix is duplicated into each in-scope package/distribution testUtils/hooks.ts copy (not re-exported from jest-config).

  • getUpdateCount() remains render-phase (unchanged semantics for existing hookToHaveUpdateCount assertions).

  • No API/BFF contract, RBAC, or migration impact. Upstream copies under packages//upstream/ are intentionally untouched (separate sync).
  • Solution

    Canonical fix in @odh-dashboard/jest-config renderHook:

    1. Keep updateCount for render-phase / matcher use.
    2. Add publishedCount, advanced only when the helper has written the rendered value onto RTL’s result ref (post-commit).
    3. In a useEffect, stage the rendered value and flushPublish() once the RTL result ref is available — write result.current before bumping publishedCount, so waitForNextUpdate never resolves on a lagging ref. (Our effect runs before RTL’s; RTL then writes the same value again.)
    4. waitForNextUpdate waits on publishedCount > expected via RTL waitFor (not on updateCount or result identity).
    5. Pending publish state is held in a const deferred object (resultRef + boxed { value } unpublished) so legitimate null/undefined results are not confused with “nothing pending,” and prefer-const / es2021 Error constructor constraints are satisfied (no { cause } options object).

    Package copies: apply the exact same renderHook logic (including the React import) to each in-scope frontend/src/__tests__/unit/testUtils/hooks.ts — agent-ops, automl, autorag, data-connect-hub, data-registry, eval-hub, gen-ai, maas, mlflow, and distributions/core-bff. Do not re-export from @odh-dashboard/jest-config; keep local copies so packages stay self-contained. Upstream copies under packages//upstream/ are left unchanged.

    Tests: extend hooks.spec.ts for promise/object/batched/successive updates, FetchState-like identity churn, and fake-timer publish via act (avoid overlapping wait + timer advance under fake timers).

    Evidence

    Local:

    pnpm --filter @odh-dashboard/jest-config run type-check   # pass
    pnpm --filter @odh-dashboard/jest-config run lint         # pass
    pnpm --filter @odh-dashboard/jest-config exec jest src/__tests__/hooks.spec.ts --no-coverage
      # 23 passed
    
    # Spot-check consumer previously sensitive to wait sync:
    frontend: usePipelineRecurringRuns.spec.ts — 5 passed
    

    Coverage in hooks.spec.ts includes: publish-before-resolve for promise and object updates; batched setState final value; successive async updates; new-array-each-render (FetchState-like); fake-timer act flush / partial delay / interval / multi-update / timeout.

    [RHOAIENG-97653]: https://redhat.atlassian.net/browse/RHOAIENG-97653?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

    Diff: 522 lines (+391 / -131) across 6 files Reviewer: andyatmiami Review duration: 8301m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 522 lines diff across 6 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.374
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #10054"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.8% (MEDIUM) · Higher = larger blast radius
    47.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.9% · Safe to merge
    18.9%