2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-17 19:21:29 +03:00
Files
axios/tests
Mohammad Faiz 73a7c55282 fix: clean up error handling, fix a proto-pollution gap, and seal a few loose ends (#10922)
* Clean up error handling, fix a proto-pollution gap, and seal a few loose ends.

Been poking around the codebase and found a handful of things that needed tidying up:

- resolveConfig.js - config.params and config.paramsSerializer were being accessed directly off user input instead of going through the own() guard. If someone crafted a config with inherited params from the prototype, you'd get unexpected behavior. Swapped to own('params') / own('paramsSerializer') like the rest of the module does.

- http.js - there was a stray console.warn('emit error', err) in an abort-event catch block. Debug leftover, shouldn't reach production. Replaced with a quiet catch.

- AxiosHeaders.js - three places were throwing raw Error or TypeError instead of AxiosError. Swapped them over with ERR_BAD_OPTION_VALUE. Also added the missing AxiosError import (creates a circular dep with AxiosError.js which imports AxiosHeaders, but it works fine at runtime since the throws are inside method bodies, not at module eval time).

- toFormData.js - the circular reference detection was throwing a bare Error. Changed to AxiosError without a code, so it stays distinguishable from the depth-exceeded error that uses ERR_FORM_DATA_DEPTH_EXCEEDED. (There's a test that explicitly checks this distinction.)

- formDataToStream.js - two more raw throws (TypeError and Error) → AxiosError.

- buildURL.js - import self-path ../helpers/AxiosURLSearchParams.js when it lives in the same directory as the importer. Changed to ./AxiosURLSearchParams.js.

- index.d.cts - CanceledError was missing readonly name: 'CanceledError' that index.d.ts already has. Added it to keep the CJS declarations in sync.

Lint passes clean, all 770 unit tests green. Nothing breaking - all changes are either internal (no consumer-facing API change) or type-only.

* Update AxiosHeaders.js

* Update AxiosHeaders.js

* fix: revert breaking error-type changes per review feedback

Reverts AxiosError throws back to native Error/TypeError in AxiosHeaders,
formDataToStream, and toFormData to avoid breaking existing consumers
who catch by constructor name or check isAxiosError().

Adds regression tests for resolveConfig own('params')/own('paramsSerializer')
guard as requested in review.

Removes unused AxiosError imports from AxiosHeaders and formDataToStream.

* docs: add pre-release notes for config hardening

---------

Co-authored-by: Jason Saayman <jasonsaayman@gmail.com>
2026-05-26 21:09:03 +02:00
..

Test Contribution Guide

This guide explains how to contribute tests inside the tests directory. It is intentionally scoped to this directory only.

Tests Directory Layout

tests/
  browser/   # browser runtime tests
  setup/     # shared test setup utilities
  smoke/     # package-compat smoke suites (esm + cjs)
  unit/      # focused unit/behavior tests

Use the runtime-first layout already present in this directory:

  • Put browser-runtime behavior in tests/browser.
  • Put non-browser focused tests in tests/unit.
  • Put packaging/compatibility smoke checks in tests/smoke/esm/tests and tests/smoke/cjs/tests.
  • Reuse helpers from tests/setup instead of duplicating setup logic.

File Naming Conventions

Follow the existing file patterns:

  • Unit tests: *.test.js
  • Browser tests: *.browser.test.js
  • ESM smoke tests: *.smoke.test.js
  • CJS smoke tests: *.smoke.test.cjs

When adding a new test, match the nearest existing file name pattern in the same subdirectory.

Suite-Specific Authoring Patterns

Unit (tests/unit)

  • Keep tests focused on one behavior or API surface.
  • For adapter/network behavior, prefer local test servers using utilities from tests/setup/server.js.
  • Ensure server cleanup with try/finally so tests do not leak resources.
  • Keep fixtures close to the tests that use them (see tests/unit/adapters for examples).

Representative files:

  • tests/unit/adapters/http.test.js
  • tests/unit/adapters/fetch.test.js
  • tests/unit/regression.test.js

Browser (tests/browser)

  • Use local in-file MockXMLHttpRequest style mocks when testing request behavior.
  • Replace global XHR in beforeEach and restore it in afterEach.
  • Reset spies/mocks in cleanup hooks to keep tests isolated.
  • Keep assertions centered on observable request/response behavior.

Representative files:

  • tests/browser/requests.browser.test.js
  • tests/browser/adapter.browser.test.js
  • tests/browser/defaults.browser.test.js

Smoke (tests/smoke)

  • Keep ESM and CJS smoke coverage aligned for compatibility-sensitive behavior.
  • If you add a new smoke scenario in one format, add the equivalent in the other format.
  • Keep smoke tests small and focused on import/runtime behavior and critical request flows.

Representative files:

  • tests/smoke/esm/tests/fetch.smoke.test.js
  • tests/smoke/cjs/tests/fetch.smoke.test.cjs
  • tests/smoke/esm/tests/basic.smoke.test.js
  • tests/smoke/cjs/tests/basic.smoke.test.cjs

Shared Setup Utilities (tests/setup)

Use shared helpers before introducing new setup code:

  • tests/setup/server.js
    • Server lifecycle helpers: startHTTPServer, stopHTTPServer, stopAllTrackedHTTPServers
    • Timing helpers: setTimeoutAsync
    • Data/stream helpers used by adapter tests
  • tests/setup/browser.setup.js
    • Browser cleanup hook (afterEach) for clearing test DOM state

General expectation: if a helper can be reused by multiple tests in this directory, add or extend it in tests/setup instead of copying setup code between test files.

Fixtures and Test Data

  • Prefer colocated fixtures near the test files that need them.
  • Keep fixture names explicit and stable.
  • For matrix-like scenarios, prefer concise table-driven cases inside the test file when practical.

Examples of colocated fixtures:

  • tests/unit/adapters/cert.pem
  • tests/unit/adapters/key.pem
  • tests/unit/adapters/axios.png

Contributor Checklist

Before opening a PR for tests in this directory:

  • File is placed in the correct suite directory (unit, browser, or smoke).
  • File name matches the local pattern (*.test.js, *.browser.test.js, *.smoke.test.js, *.smoke.test.cjs).
  • Test setup/teardown is explicit and leaves no global/server state behind.
  • Shared setup logic uses tests/setup helpers where possible.
  • Smoke tests remain ESM/CJS consistent when behavior is format-sensitive.
  • Fixtures are colocated and minimal.
  • Assertions are deterministic and avoid unnecessary timing/network flakiness.