Feedback: Default behaviors #526
Replies: 1 comment
-
|
Hey @jitendra-bhandure. Thanks for sharing your feedback! There is one crucial thing you are missing (and that I have to talk more about in general) is treating your mocks as your test boundary. Let me elaborate. Say you want to layer some overrides on top of your default handlers. You've got The error in your use case comes from how loose your override is. There's no way for any server to understand that by SolutionThe actual solution to this is simple. Scope your override to the context of your test case. Consider this: const defaultHandlers = [
http.get('/user/messages', () => HttpResponse.json({ messages: [] })),
http.get('/user/:id', () => HttpResponse.json({ id: 'default-user' })),
]
test('handles dynamic user fetching', () => {
server.use(
// 1. Drop the wildcard. Use an exact user ID for this test case.
http.get('/user/abc-123', () => HttpResponse.json({ id: 'test-user' }))
)
// 2. Later, fetch that same user ID in your tested logic.
await fetch('http://localhost/user/abc-123')
})This way, you establish deterministic network behavior layering that doesn't conflict with any of your defined request handlers. In order of priority within this test case:
This is one of the most overlooked things about API mocking. Your mocks are your boundary. The goal was never to model your backend behaviors 1-1 in your mocks. The goal has always been to use the mocks to say "given this network behavior, here's how my tested logic must behave". Always describe the given. This works great for code bases of any scale and even for any API mocking tools out there. This is how API mocking is meant to be used for the best control over the network. I touch on this briefly in the Server perspective in the docs, but I agree this topic has to be elaborated more on so it's more clear to everyone using the library. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Feedback
Feature Request: Introduce In-Place Route Overriding to Prevent Path Hijacking in Large Handler Suites
🚀 The Problem
In large-scale applications with 1,000+ default handlers, routes are carefully pre-sorted so specific static paths come before dynamic wildcards (e.g.,
/user/messagessits above/user/:id).However, when using
server.use()to mock a specific runtime behavior inside a single test, MSW prepends the new handler to the absolute top of the stack. This causes dynamic overrides to accidentally hijack unrelated static endpoints.❌ Code Example (The Current Pain Point)
🧠 Why This Doesn't Scale
To fix this today in a codebase with thousands of routes, developers must either:
http.get('/user/:id(?!messages|profile|settings|...)')) which break every time a teammate adds a new static route.💡 Proposed Solution: Introduce
server.override()We propose a native way to inject a handler that respects the existing path specificity, or an explicit method that updates an existing matching route in-place rather than blind prepending.
Option A: In-Place Matching (
server.override)Instead of prepending to the top of the stack,
server.override()looks for the existing token pattern match and swaps out only its resolver.Option B: Priority/Position Strategy Flag
Allow
server.use()to accept an options configuration to control its placement behavior.🎯 Benefits
Beta Was this translation helpful? Give feedback.
All reactions