Skip to content

test: add unit tests for retryWithBackoff#7446

Merged
Ashutoshx7 merged 1 commit into
sugarlabs:masterfrom
sapnilbiswas:test/retry-with-backoff-coverage
Jul 10, 2026
Merged

test: add unit tests for retryWithBackoff#7446
Ashutoshx7 merged 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
Member

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

@Ashutoshx7

Copy link
Copy Markdown
Member

checking in again for any updates @sapnilbiswas

@sapnilbiswas

Copy link
Copy Markdown
Contributor Author

@Ashutoshx7 I apologize for being this late in responding, I was actually going through some end-sem exams and was unable to keep on working on it. I will be back to contributing in a week.

@Ashutoshx7

Copy link
Copy Markdown
Member

@Ashutoshx7 I apologize for being this late in responding, I was actually going through some end-sem exams and was unable to keep on working on it. I will be back to contributing in a week.

Ya sure
Its fine
I should have waited a bit more
Take ur time
And best of luck for your exams

@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

This PR has merge conflicts with master.

Please rebase your branch:

# Add upstream remote (one-time setup)
git remote add upstream https://github.com/sugarlabs/musicblocks.git

# Fetch latest master and rebase
git fetch upstream
git rebase upstream/master

# Resolve any conflicts, then:
git push --force-with-lease origin YOUR_BRANCH

Tip: Enable "Allow edits from maintainers" on this PR so we can auto-rebase for you next time. This only grants access to your PR branch. Your fork's other branches are not affected.

@sapnilbiswas sapnilbiswas force-pushed the test/retry-with-backoff-coverage branch from 93f728c to 4af6b22 Compare July 10, 2026 11:29
@codecov

codecov Bot commented Jul 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.38%. Comparing base (84e8052) to head (b43de39).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #7446   +/-   ##
=======================================
  Coverage   58.38%   58.38%           
=======================================
  Files         174      174           
  Lines       57894    57894           
=======================================
+ Hits        33800    33802    +2     
+ Misses      24094    24092    -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sapnilbiswas sapnilbiswas force-pushed the test/retry-with-backoff-coverage branch from 4af6b22 to b43de39 Compare July 10, 2026 11:42
@Ashutoshx7 Ashutoshx7 merged commit 1801131 into sugarlabs:master Jul 10, 2026
13 checks passed
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