Fetch API Support Beta #1464
Replies: 38 comments 174 replies
-
Greetings, I started a fresh NextJs application, applied the new syntax to my handler, ran a simple test that checked for data after a GET request, and Jest blew up. If I switch back to the latest msw version and apply the old syntax, everything works as usual.
Handler:
Error:
|
Beta Was this translation helpful? Give feedback.
-
It's probably worth mentioning the new |
Beta Was this translation helpful? Give feedback.
-
From the migration guide:
This is possible with an array of entries instead of a plain object: new Headers([
["Set-Cookie", "sessionId=123"],
["Set-Cookie", "name=world"],
]); |
Beta Was this translation helpful? Give feedback.
-
With all the breaking changes towards the standards, may I also suggest renaming |
Beta Was this translation helpful? Give feedback.
-
@kettanaito this is what I found implementing some tests -> https://codesandbox.io/p/sandbox/msw-standard-api-9z1f0h?file=%2Frest.spec.ts Seems the Note: I expected the same behavior as |
Beta Was this translation helpful? Give feedback.
-
❤️ the new API. I tried it out in 3 projects and found myself just knowing what to do thanks to already knowing Request, Response, etc. rather than having to go look at MSW docs when I couldn't remember some context utility or whatever. Great job, looking forward to this being released! The only frustration was not being able to use Node 18, but that applies to the current version of MSW as well so nothing new to report there. |
Beta Was this translation helpful? Give feedback.
-
I am testing with vitest here: https://github.com/xmlking/svelte-starter-kit/tree/main/src/mocks env: node v18.11.0 , "cross-fetch": "3.1.5", "msw": "0.49.0" <--- Works looking for any workaround or fix |
Beta Was this translation helpful? Give feedback.
-
Love the upcoming changes! As already mentioned in #1436, I noticed a TypeScript bug when passing a predefined object to |
Beta Was this translation helpful? Give feedback.
-
I'll repost my question regarding the new Beta and generics here:
Would the proper way be like the above replies? Using Or maybe using something like |
Beta Was this translation helpful? Give feedback.
-
Looks like with the latest version:
|
Beta Was this translation helpful? Give feedback.
-
Hi folks! I've been using the Fetch API support to set up mocking for a mature codebase for the last week or so and it's been great. Use cases include a lightweight E2E testing suite that catches the most egregious errors, as well as a light weight development environment for our designers. It's a lot more intuitive than the previous iteration and it works really well. Two things that stood out to me and I wanted to point out to you:
|
Beta Was this translation helpful? Give feedback.
-
The Fetch API spec explicitly bans body on GET requests, despite this being quite common in the wild (and not explicitly banned by the HTTP spec). Stripe, Elasticsearch and many others all have API designs that require body in GET requests. node-fetch, axios also allows this. |
Beta Was this translation helpful? Give feedback.
-
Hey! Thank you for taking the time to help us with the next library release. We’re approaching this one as a release candidate because it contains quite a number of breaking changes.
Motivation
The main motivation behind this release is to rework the public API to respect and use the Fetch API standard. This means handling intercepted requests as
Request
instances and expecting the mocked responses to be declared asResponse
instances. While this may sound simple, it requires a significant change to how MSW is used by each of you.This is also the biggest API change we’ve had since the day I pushed the
req, res, ctx
API. With this release, that era is gone and so is that API. The next era begins and it looks fantastic.Before you begin
The purpose of this beta release is to gather your feedback on the changes. We kindly ask you to share such feedback in the comments under this discussion, following these rules:
Getting started
👉 Please follow the Migration guide to learn more about how to install this beta release and the migration instructions for every feature we’ve changed or deprecated.
Expect breaking changes. It will require some effort to migrate any existing MSW setup to the new API but it’s going to be worth it.
What is expected from the participants?
Frequently asked questions
Are these changes backward-compatible?
No. Backward compatibility is beneficial when you want to have the advantage of the change while remaining on the previous API. This change, however, is about the API, so maintaining backward compatibility is not feasible for it. If you wish to use the previous API you can do so by staying on the previous version. However, you will update to these changes eventually as this is the public API we’re going forward with.
Are these changes “production” ready?
No. Be mindful that you’re trying out a beta release of the library. Things may and likely will change as we iterate through your feedback.
That being said, the API changes proposed in this release will end up in the public release, eventually. This is the direction we’re taking, so migrating to it may ease any additional changes you need to do in your setup once this release becomes public.
When is this release going to be public?
As soon as we gather enough feedback to be certain people can migrate to the new version without issues. We will then release the changes under the
latest
tag, updating the documentation respectively.Beta Was this translation helpful? Give feedback.
All reactions