Skip to content

fix(WebSocketHandler): add public test() method#2727

Merged
kettanaito merged 3 commits into
mainfrom
fix/ws-test
Apr 24, 2026
Merged

fix(WebSocketHandler): add public test() method#2727
kettanaito merged 3 commits into
mainfrom
fix/ws-test

Conversation

@kettanaito
Copy link
Copy Markdown
Member

@kettanaito kettanaito commented Apr 24, 2026

Motivation

There isn't an easy way to check if a given WebSocketHandler matches a URL.

Changes

  • Adds the WebSocketHandler.test method that functions similarly to the same method from RequestHandler.
  • Uses the same internal #match logic for consistent predicate in .run() and .test().

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 24, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 113183de-9ca7-4d57-a38d-97805dfc2d16

📥 Commits

Reviewing files that changed from the base of the PR and between 57b1bc0 and a5fd9bf.

📒 Files selected for processing (2)
  • src/core/handlers/WebSocketHandler.test.ts
  • src/core/handlers/WebSocketHandler.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/core/handlers/WebSocketHandler.ts

📝 Walkthrough

Walkthrough

WebSocketHandler now accepts string | URL inputs for parsing and predicate checks, adds a synchronous test() method that uses a private #match helper, and updates run() to rely on #match. New Jest tests verify matching behavior across multiple URL forms and resolution contexts.

Changes

Cohort / File(s) Summary
Handler implementation
src/core/handlers/WebSocketHandler.ts
parse and predicate signatures updated to accept string | URL. Added public test(url: string | URL, resolutionContext?: WebSocketResolutionContext & { strict?: boolean }): boolean. Introduced private #match helper; run() refactored to use #match and return null when no match. Resolution uses resolutionContext?.baseUrl for parsing but evaluates predicate against the original input.
Handler tests
src/core/handlers/WebSocketHandler.test.ts
Added Jest suite asserting matching behavior for exact ws:// strings, non-matching ws:// strings, relative paths resolved with baseUrl, URL objects, and corresponding http:// equivalents; covers both matching and non-matching cases across inputs.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰
I hopped through URLs, string and class,
I matched ws paths both future and past.
A tiny test() with a careful glance,
Now handlers leap — give code a dance! 🥕✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: adding a public test() method to WebSocketHandler, which aligns with the primary focus of the PR.
Description check ✅ Passed The description is directly related to the changeset, explaining the motivation for adding the test() method and detailing the changes made.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/ws-test

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/core/handlers/WebSocketHandler.test.ts (1)

150-150: Rename this test title to “non-matching”.

The current title says “matching” but asserts false, which is confusing.

Suggested wording fix
-  it('returns false for a relative matching string', () => {
+  it('returns false for a relative non-matching string', () => {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/core/handlers/WebSocketHandler.test.ts` at line 150, Rename the
misleading test title in the it(...) block that currently reads "returns false
for a relative matching string" to indicate non-matching (e.g., "returns false
for a relative non-matching string" or simply "non-matching") so the test name
matches the assertion; update the it(...) description in
src/core/handlers/WebSocketHandler.test.ts (the test block containing the
assertion that expects false) to the new wording.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/core/handlers/WebSocketHandler.test.ts`:
- Line 150: Rename the misleading test title in the it(...) block that currently
reads "returns false for a relative matching string" to indicate non-matching
(e.g., "returns false for a relative non-matching string" or simply
"non-matching") so the test name matches the assertion; update the it(...)
description in src/core/handlers/WebSocketHandler.test.ts (the test block
containing the assertion that expects false) to the new wording.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 6b9f038c-5296-4ae0-8c97-0ff21106ec6a

📥 Commits

Reviewing files that changed from the base of the PR and between d814fc8 and 57b1bc0.

📒 Files selected for processing (2)
  • src/core/handlers/WebSocketHandler.test.ts
  • src/core/handlers/WebSocketHandler.ts

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 24, 2026

Open in StackBlitz

npm i https://pkg.pr.new/msw@2727

commit: a5fd9bf

@kettanaito kettanaito merged commit 3da7048 into main Apr 24, 2026
22 checks passed
@kettanaito kettanaito deleted the fix/ws-test branch April 24, 2026 15:46
@kettanaito
Copy link
Copy Markdown
Member Author

Released: v2.13.6 🎉

This has been released in v2.13.6.

Get these changes by running the following command:

npm i msw@latest

Predictable release automation by Release.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant