Skip to content

Mock global.fetch in tests instead of calling the live USPS API#2

Merged
freshlogic merged 1 commit into
mainfrom
mock-fetch-tests
May 25, 2026
Merged

Mock global.fetch in tests instead of calling the live USPS API#2
freshlogic merged 1 commit into
mainfrom
mock-fetch-tests

Conversation

@freshlogic
Copy link
Copy Markdown
Member

Summary

The tracking tests call the live USPS sandbox (apis-tem.usps.com), which intermittently returns 503 and has kept CI red for weeks. This replaces fetch with a node:test mock that routes the oauth and tracking endpoints to canned responses — no live calls, no new dependencies.

Details

  • Small mockUspsFetch() router stubs /oauth2/v3/token and /tracking/v3r2/tracking
  • Runs serially (global fetch mocking and concurrency: true don't mix) and clears the memory-cache token singleton between tests
  • The invalid-URL test still uses real fetch to assert its parse error
  • Caching test now asserts fetch was called exactly once on the second getAccessToken()

Test plan

  • npm test → 10 pass / 0 fail, ~40ms (was minutes of live calls)
  • npx eslint test/index.js clean

🤖 Generated with Claude Code

The tracking tests hit the live USPS sandbox (apis-tem.usps.com), which
intermittently returns 503 and had kept CI red for weeks. Replace fetch
with a node:test mock that routes the oauth and tracking endpoints to
canned responses — no live calls, no new dependencies.

Runs serially (global fetch mocking and concurrency don't mix) and
clears the memory-cache token singleton between tests. The invalid-URL
test still uses real fetch to assert its parse error.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@freshlogic freshlogic merged commit f32996b into main May 25, 2026
2 checks passed
@freshlogic freshlogic deleted the mock-fetch-tests branch May 25, 2026 18:16
@coveralls
Copy link
Copy Markdown

Coverage Report for CI Build 26414019541

Coverage remained the same at 100.0%

Details

  • Coverage remained the same as the base build.
  • Patch coverage: No coverable lines changed in this PR.
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 82
Covered Lines: 82
Line Coverage: 100.0%
Relevant Branches: 13
Covered Branches: 13
Branch Coverage: 100.0%
Branches in Coverage %: Yes
Coverage Strength: 5.88 hits per line

💛 - Coveralls

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.

2 participants