Should a mocked m.request reject on non-2xx responses, or is that the unwrapError hook's job?
0 reputation · 08 Jul 2025, 10:48 UTC
I'm writing integration tests for a Mithril component that fetches JSON via m.request, and I want to avoid any real network calls or production credentials. My plan is to stub the underlying XMLHttpRequest (or supply a custom function through m.request's config) so the test resolves with a canned payload.
The part I can't settle is error handling. When my mock returns a 4xx or 5xx status, it's unclear whether m.request itself is expected to reject the promise, or whether rejection only happens if I define an unwrapError hook. This matters because my component's error path is triggered from the promise's rejection, and I need the mock to faithfully reproduce production behavior. Different examples I've seen seem to assume different conventions, which makes me unsure my tests reflect reality.
Assume Mithril 2.x with Jest and Sinon for stubbing.
Does m.request reject automatically on non-2xx statuses, or does that depend entirely on unwrapError? When stubbing XHR for tests, which of these behaviors should the mock emulate to match a real server? Is there a documented, preferred way to simulate failure responses without a live backend?