Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't work when used with "express.json" middleware #18

Closed
nnnnat opened this issue Aug 4, 2022 · 2 comments · Fixed by #19
Closed

Doesn't work when used with "express.json" middleware #18

nnnnat opened this issue Aug 4, 2022 · 2 comments · Fixed by #19

Comments

@nnnnat
Copy link

nnnnat commented Aug 4, 2022

Hey y'all big fan of MSW!

I've been using MSW & http-middleware together to create a mock GraphQL API for a few months & I've recently run into an issue after updating to MSW v0.44.2 & http-middleware v0.5.0. I have a very similar setup to the custom server example in this repo & after updating all GQL operations now seem to pass through & return a 404 "Mock not found".

I've put together a small example that reproduces the issue I'm seeing.

@kettanaito
Copy link
Member

kettanaito commented Aug 8, 2022

Hey, @nnnnat. Thanks for raising this.

I've taken a quick look and it seems that the body we encode in the middleware results in the encoded "[object Object]" string. When MSW attempts to parse that body, it treats it as a string, so it doesn't pass as a valid GraphQL API request.

I believe the issue is somewhere around here:

body: encodeBuffer(req.body),

If I look at your server, I can see that you use express.json() middleware. It will preserve the JSON request body as an object, and that causes the issue:

https://github.com/nnnnat/msw-middleware-example/blob/22c1a8d1c6f1b600ae14562559ad5c25a9ffe61f/main.js#L8

On the middleware's side, we need to check if the req.body is an object and stringify it in that case, so that encodeBuffer always accepts a plain string.

@kettanaito kettanaito changed the title createMiddleware no longer working after updating to v0.5.0 Doesn't work when used with "express.json" middleware Aug 8, 2022
@kettanaito
Copy link
Member

Released: v0.5.1 🎉

This has been released in v0.5.1!

Make sure to always update to the latest version (npm i @mswjs/http-middleware@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

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 a pull request may close this issue.

2 participants