Skip to content

test(cloudflare-workers): add comprehensive tests for websocket and conninfo adapters#4748

Open
rokasta12 wants to merge 1 commit intohonojs:mainfrom
rokasta12:feat/cloudflare-workers-tests
Open

test(cloudflare-workers): add comprehensive tests for websocket and conninfo adapters#4748
rokasta12 wants to merge 1 commit intohonojs:mainfrom
rokasta12:feat/cloudflare-workers-tests

Conversation

@rokasta12
Copy link
Contributor

Summary

  • Add 12 new tests to websocket.test.ts (14 total, up from 2), achieving 100% statement/branch/function/line coverage of websocket.ts
  • Add 1 new test to conninfo.test.ts for absent cf-connecting-ip header, matching the pattern in the Cloudflare Pages adapter

WebSocket Tests Added

Test Source lines covered
101 response with webSocket set to client L48-52 (Response construction)
server.accept() called during upgrade L46-47
onClose handler triggered L36-37 (conditional addEventListener)
onError handler triggered L42-43 (conditional addEventListener)
No event listeners when no handlers provided L36, L39, L42 (branch: false)
send() delegated to server.send() L31 (WSContext send)
close() delegated to server.close() L22 (WSContext close)
protocol exposed through WSContext L23-25 (getter)
readyState exposed through WSContext L27-29 (getter)
url converted to URL when present L30 (truthy branch)
url set to null when falsy L30 (falsy branch)
raw set to server WebSocket L26

Test Infrastructure

  • Extracted createMockWebSocketPair() helper that creates isolated mocks per test (EventTarget-based server with accept/close/send as vi.fn())
  • Added CloseEvent polyfill for Node.js/vitest environment
  • Mocked Response constructor for the 101 status test (Node.js restricts status to 200-599, but Cloudflare Workers allows 101)

Conninfo Test Added

  • Verifies getConnInfo() returns undefined address when cf-connecting-ip header is absent (matches the existing pattern in cloudflare-pages/conninfo.test.ts)

Checklist

  • Tests pass: npx vitest --run src/adapter/cloudflare-workers/websocket.test.ts (14/14)
  • Tests pass: npx vitest --run src/adapter/cloudflare-workers/conninfo.test.ts (2/2)
  • TypeScript: npx tsc --noEmit — zero errors
  • Formatting: bun run format:fix — no changes needed
  • Linting: bun run lint:fix — zero errors (only pre-existing warnings)

…onninfo adapters

Add 12 new tests to websocket.test.ts covering the complete upgradeWebSocket
implementation: 101 response with webSocket client, server.accept() call,
onClose/onError event handlers, conditional addEventListener (no-handler case),
WSContext delegation (send, close), and WSContext properties (protocol,
readyState, url, raw). Achieves 100% coverage of websocket.ts.

Add 1 new test to conninfo.test.ts for absent cf-connecting-ip header,
matching the pattern used in the Cloudflare Pages adapter tests.
@codecov
Copy link

codecov bot commented Feb 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.50%. Comparing base (2de30d7) to head (129578e).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4748      +/-   ##
==========================================
+ Coverage   91.48%   91.50%   +0.01%     
==========================================
  Files         177      177              
  Lines       11556    11556              
  Branches     3357     3364       +7     
==========================================
+ Hits        10572    10574       +2     
+ Misses        983      981       -2     
  Partials        1        1              

☔ View full report in Codecov by Sentry.
📢 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.

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