Skip to content

test: add unit tests for retryWithBackoff#7446

Open
sapnilbiswas wants to merge 1 commit into
sugarlabs:masterfrom
sapnilbiswas:test/retry-with-backoff-coverage
Open

test: add unit tests for retryWithBackoff#7446
sapnilbiswas wants to merge 1 commit into
sugarlabs:masterfrom
sapnilbiswas:test/retry-with-backoff-coverage

Conversation

@sapnilbiswas

Copy link
Copy Markdown
Contributor

Summary

This PR adds a comprehensive Jest test suite for the js/utils/retryWithBackoff.js module, which handles race conditions (like waiting for container bounds) using an exponential backoff strategy. This previously untested file now has 100% test coverage (statements, branches, functions, and lines).

What changed

  • Added: js/utils/__tests__/retryWithBackoff.test.js — New Jest test file for testing the backoff and retry behavior.

Test coverage

  • Immediate Resolution: Verifies that if check() returns truthy on the first try, it resolves immediately without triggering any delay timeouts.
  • Exponential Backoff Scaling: Ensures the delay correctly scales exponentially between attempts (initialDelay * 2^attempt).
  • Retry Exhaustion: Checks that exceeding maxRetries properly halts execution and throws an Error with the provided (or default) errorMessage.
  • Side-effects Callback (onRetry): Asserts that the optional onRetry callback is invoked with the correct attempt index on each failure.
  • Default Arguments & Fallbacks: Ensures fallback to the default Promise-wrapped setTimeout when a custom delayFn is omitted, and tests the default maxRetries (20) and initialDelay (50) values.

How it is tested

  • Mocking: Used jest.fn() to mock the check(), onSuccess(), onRetry(), and delayFn() functions to strictly verify invocation counts and arguments.
  • Simulating State Changes: Utilized .mockReturnValueOnce() to simulate check() initially failing (returning falsy values) before eventually succeeding on later attempts to test the retry loop.
  • Fake Timers: For testing the default delayFn (which uses standard setTimeout), jest.useFakeTimers() and jest.advanceTimersByTime() were used to advance the clock instantly, ensuring the tests remain fast and deterministic without actual asynchronous waiting.
  • Error Handling: Used Jest's .rejects.toThrow() to validate that the correct error boundaries are hit when the maximum number of retries is exceeded.

PR Category

  • Tests

@sapnilbiswas sapnilbiswas marked this pull request as ready for review May 28, 2026 18:29
@github-actions github-actions Bot added tests Adds or updates test coverage size/M Medium: 50-249 lines changed area/javascript Changes to JS source files area/tests Changes to test files labels May 28, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🧪 Jest Test Results

✅ All Jest tests passed! This PR is ready to merge.

Coverage: Statements: 47.58% | Branches: 38.82% | Functions: 52.5% | Lines: 47.98%
Master Coverage: Statements: 48.09% | Branches: 39.61% | Functions: 52.88% | Lines: 48.49%

@sapnilbiswas

Copy link
Copy Markdown
Contributor Author

@walterbender PLease have a look when you get time

@mahesh-09-12 mahesh-09-12 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally - the added tests cover the retry, backoff, and failure scenarios well.

@vyagh vyagh requested review from Ashutoshx7 and omsuneri June 11, 2026 13:00
@Ashutoshx7

Copy link
Copy Markdown
Collaborator

why a separate file when we already have one on master ?

need changes

@walterbender

Copy link
Copy Markdown
Member

Please see the comment by Ahsutosh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/javascript Changes to JS source files area/tests Changes to test files size/M Medium: 50-249 lines changed tests Adds or updates test coverage

Projects

Development

Successfully merging this pull request may close these issues.

4 participants