test: add unit tests for parseProtocol, speedometer, throttle, and composeSignals helpers by mahmoodhamdi · Pull Request #10577 · axios/axios · GitHub
Skip to content

test: add unit tests for parseProtocol, speedometer, throttle, and composeSignals helpers#10577

Open
mahmoodhamdi wants to merge 1 commit intoaxios:v1.xfrom
mahmoodhamdi:test/add-helper-unit-tests
Open

test: add unit tests for parseProtocol, speedometer, throttle, and composeSignals helpers#10577
mahmoodhamdi wants to merge 1 commit intoaxios:v1.xfrom
mahmoodhamdi:test/add-helper-unit-tests

Conversation

@mahmoodhamdi
Copy link
Copy Markdown

@mahmoodhamdi mahmoodhamdi commented Mar 29, 2026

Summary

Adds missing unit test coverage for four helper utilities that had no dedicated tests:

  • parseProtocol — protocol parsing from URLs (http, https, ftp, data URIs, custom protocols, max length boundary)
  • speedometer — data transfer rate calculation using circular buffer (default params, custom min, rate computation)
  • throttle — function throttling decorator (immediate invocation, threshold enforcement, flush, multi-arg passthrough)
  • composeSignals — AbortSignal composition with timeout (timeout abort, external signal forwarding, falsy signal filtering, unsubscribe)

These helpers are used by the core library and fetch adapter but previously had no dedicated unit tests. This brings the tested helper count from 9/32 to 13/32.

Motivation

While working with the fetch adapter in a project, I noticed these core helpers lacked test coverage. composeSignals and throttle in particular are critical for request timeout and progress event handling — having tests for them helps catch regressions during future changes.

Test Details

Helper Tests Added Key Scenarios
parseProtocol 11 http/https/ftp, +/- chars, data URIs, file protocol, no protocol, empty string, 25-char boundary
speedometer 7 return type, pre-min undefined, post-min calculation, default params, custom min, zero min, rate accuracy
throttle 8 return shape, immediate call, throttling, deferred latest args, threshold expiry, flush, no-op flush, multi-args
composeSignals 10 no signals, null signals, timeout signal, timeout abort, external signal, falsy filtering, unsubscribe

Total: 36 new tests, all passing.

Testing

npx vitest run tests/unit/helpers/
# 13 test files, 74 tests — all passing

Lint is clean (npm run lint passes).


Summary by cubic

Adds unit tests for parseProtocol, speedometer, throttle, and composeSignals to cover critical paths and edge cases. Improves reliability around timeouts, throttling, and protocol parsing with no production code changes.

  • Description

    • parseProtocol: http/https/ftp, + and -, data/file URIs, no protocol, 25-char limit.
    • speedometer: default vs custom min, circular buffer wrap, numeric rate after min elapsed.
    • throttle: immediate call, threshold enforcement, latest args after wait, flush, multi-arg passthrough.
    • composeSignals: timeout abort with ETIMEDOUT reason, multiple signals, falsy filtering, unsubscribe support.
  • Testing

    • 4 new test files under tests/unit/helpers/; 36 tests added, all passing.
    • Run: npx vitest run tests/unit/helpers/.
    • No changes to production code or existing tests; no additional tests needed.

Written for commit 635af95. Summary will update on new commits.

…mposeSignals helpers

Add missing unit test coverage for four helper utilities:

- parseProtocol: protocol parsing from URLs including edge cases
  (data URIs, custom protocols, max length boundary)
- speedometer: data transfer rate calculation with circular buffer
- throttle: function throttling with flush support
- composeSignals: AbortSignal composition with timeout handling

These helpers are used by the core library and fetch adapter but
had no dedicated unit tests.
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

2 issues found across 4 files

Confidence score: 4/5

  • Test-related issues only; no direct product code impact, so overall merge risk appears low
  • tests/unit/helpers/composeSignals.test.js leaves timers/subscriptions pending, which could slow or destabilize the test suite over time
  • Pay close attention to tests/unit/helpers/composeSignals.test.js and tests/unit/helpers/speedometer.test.js - improve timer cleanup and strengthen assertions to catch regressions.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="tests/unit/helpers/composeSignals.test.js">

<violation number="1" location="tests/unit/helpers/composeSignals.test.js:18">
P2: Timeout-based composeSignals tests leave real timers pending because created signals are not unsubscribed, which can slow or destabilize test execution.</violation>
</file>

<file name="tests/unit/helpers/speedometer.test.js">

<violation number="1" location="tests/unit/helpers/speedometer.test.js:36">
P2: Speedometer rate tests use weak `> 0` assertions, so formula/unit regressions can pass undetected.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

});

it('should return an AbortSignal when timeout is provided', () => {
const signal = composeSignals([], 5000);
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot Mar 29, 2026

Choose a reason for hiding this comment

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

P2: Timeout-based composeSignals tests leave real timers pending because created signals are not unsubscribed, which can slow or destabilize test execution.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At tests/unit/helpers/composeSignals.test.js, line 18:

<comment>Timeout-based composeSignals tests leave real timers pending because created signals are not unsubscribed, which can slow or destabilize test execution.</comment>

<file context>
@@ -0,0 +1,80 @@
+  });
+
+  it('should return an AbortSignal when timeout is provided', () => {
+    const signal = composeSignals([], 5000);
+    expect(signal).toBeDefined();
+    expect(signal.aborted).toBe(false);
</file context>
Fix with Cubic

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot Mar 29, 2026

Choose a reason for hiding this comment

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

P2: Speedometer rate tests use weak > 0 assertions, so formula/unit regressions can pass undetected.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At tests/unit/helpers/speedometer.test.js, line 36:

<comment>Speedometer rate tests use weak `> 0` assertions, so formula/unit regressions can pass undetected.</comment>

<file context>
@@ -0,0 +1,93 @@
+
+    expect(result).toBeDefined();
+    expect(typeof result).toBe('number');
+    expect(result).toBeGreaterThan(0);
+  });
+
</file context>
Fix with Cubic

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant