mswjs / msw

Industry standard API mocking for JavaScript.
https://mswjs.io
MIT License
15.99k stars 521 forks source link

TypeError: Cannot set property headers of #<_Request> which has only a getter #2290

Closed stevensacks closed 2 months ago

stevensacks commented 2 months ago

Prerequisites

Environment check

Node.js version

20.17.0

Reproduction repository

https://github.com/gaia-react/remix

Reproduction steps

  1. Run npx create-remix@latest --template gaia-react/remix
  2. npm install msw@2.4.8 (it's currently using 2.4.2 which is the last working version)
  3. npm run dev to launch Remix server
  4. Click on "Auth Example"
  5. Type in the password passw0rd (zero for o) and click the Login button
  6. TypeError occurs - MSW handler does not get called

Current behavior

I'm using remix-auth to make the call to the login endpoint, but it doesn't make it to the defined MSW handler.

If you need the console log of the error to show up in the terminal process, add it here after line 21.

TypeError: Cannot set property headers of #<_Request> which has only a getter
      at Object.construct (file:///Users/stevensacks/Development/gaia/framework/remix/node_modules/@mswjs/interceptors/lib/node/chunk-RWGRRMVU.mjs:217:27)
      at Authenticator.authenticate (/Users/stevensacks/Development/gaia/framework/remix/node_modules/remix-auth/build/authenticator.js:68:41)

Expected behavior

Expect the API call to go through to the MSW handler, as it does in version 2.4.2.

hugotiburtino commented 2 months ago

Same here. See error in CI

To reproduce, just clone the repo, check the branch dependabot/npm_and_yarn/msw-2.4.7 out, install with yarn and run yarn test

kettanaito commented 2 months ago

@stevensacks, can you please point me to what exact request handler you expect to be called?

kettanaito commented 2 months ago

I believe I tracked down the issue. Opened a fix at https://github.com/mswjs/interceptors/pull/640. Will propagate to MSW soon.

I'd love to know more details on the expected result. The error is not thrown anymore in your code, but nothing happens on the page. I can see the 302 response arriving as error in your auth logic. That seems to be related to your application (maybe that's intentional?).

stevensacks commented 2 months ago

That is odd. Login was working before with the msw handler (inside the test folder) as written. I’ll look into it asap.

stevensacks commented 2 months ago

Seems to be a bug in remix-auth. It's not storing the session correctly.

Login succeeds Redirects to /profile Profile loader authenticator.isAuthenticated fails Redirects to /login

From the user side, it appears to never leave the login page.

kettanaito commented 2 months ago

We did release some fixes around redirects in MSW, but those are suppose to follow redirects correctly (that was never done before). They are also relevant to fetch only. In your case, you are using ClientRequest in Node.js, so you can disregard this message.

I believe the underlying issue is fixed. We didn't cover one Request constructor scenario when recording raw headers. I will propagate the fix to MSW, and you will have no errors in your auth logic.

stevensacks commented 2 months ago

The reason it wasn't redirecting to /profile after login was a mistake in my code. remix-auth's authenticator throws a redirect on success, but my try catch around it was handling it as if it was an error instead of rethrowing it. It's working now in 2.4.8.

kettanaito commented 2 months ago

Awesome! 🎉 Good job on figuring that out. Meanwhile, the fix is about ready. Will publish today.

kettanaito commented 2 months ago

Released: v2.4.9 🎉

This has been released in v2.4.9!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

hugotiburtino commented 1 month ago

I'm still having the error TypeError: Cannot set property headers of [object Response] which has only a getter See https://github.com/serlo/cloudflare-worker/pull/882

To reproduce, just clone the repo, check the branch dependabot/npm_and_yarn/msw-2.4.9 out, install with yarn and runyarn test