mswjs / msw

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

TypeError: Cannot assign to read only property 'Request' of object '[object Window]' #2276

Closed hays0503 closed 2 months ago

hays0503 commented 2 months ago

Prerequisites

Environment check

Node.js version

v20.16.0

Reproduction repository

https://github.com/hays0503/sck1

Reproduction steps

Run test "test:coverage": "jest --coverage" And get error Screenshot_1

FAIL src/entities/ComponentSelectCity/ui/test/ComponentSelectCity.test.tsx × Фильтрация городов при поиске

● Фильтрация городов при поиске

TypeError: Cannot assign to read only property 'Request' of object '[object Window]'

Current behavior

I noticed that the error started appearing with version 2.4.4. I rolled back to older versions and the problem was solved.

Expected behavior

Fixing the problem

ddolcimascolo commented 2 months ago

@kettanaito

It looks like 2.4.4 really has something weird, it's like the third issue breaking people tests. Maybe you'd want to deprecate it on npm until a new version can be released?

David

JSingmin commented 2 months ago

I've gotten this error too after running an npm audit fix, first using msw 2.1.3 and also trying 2.4.5

ghost91- commented 2 months ago

We have a similar issue:

[2024-09-11T12:59:24.766Z]     TypeError: Cannot assign to read only property 'Headers' of object '[object Object]'
[2024-09-11T12:59:24.766Z] 
[2024-09-11T12:59:24.766Z]       4 |
[2024-09-11T12:59:24.766Z]       5 | export function prepareMockServer(mockServer: SetupServer): void {
[2024-09-11T12:59:24.766Z]     > 6 |     mockServer.listen();
[2024-09-11T12:59:24.766Z]         |                ^
hays0503 commented 2 months ago

Temporarily solved the problem by rolling back to an older version of the library ("msw": "^2.4.3")

rachel-bernard commented 2 months ago

I have the exact same issue after upgrading to the lastest version of msw. Fixed by rolling back to v2.4.3, as other commenters have said.

AdrienFromToulouse commented 2 months ago

confirmed, since 2.4.4 our tests fail in the same manner. TypeError: Cannot assign to read only property 'Request' of object '[object Window]' on a beforeAll(() => server.listen());

Igosuki commented 2 months ago

I narrowed it down to this commit :

https://github.com/mswjs/interceptors/commit/4660558f27e610063ff204ccf4a7595082040a0b#diff-9be9bd59c25c2573eca1764259102bcd08087e15b997c3cede7e51ceaaa5df9fR106

Overriding the dependency fixes the bug : "@mswjs/interceptors": "0.32.1"

kettanaito commented 2 months ago

This has a similar root cause and the fix as https://github.com/mswjs/msw/issues/2281#issuecomment-2351462039.

kettanaito commented 2 months ago

@davesidious,

Maybe you'd want to deprecate it on npm until a new version can be released?

There is no concept of deprecating or unpublishing a package on NPM. Software has bugs, and we've done a ton of changes and improvements in that Interceptors update. Honestly, it's a surprisingly little number of bugs, and most of them are related.

You should pin MSW to a specific version that worked for you until the issues are resolved. That's how to handle it in open source.

ghost91- commented 2 months ago

There is no concept of deprecating or unpublishing a package on NPM.

That’s actually incorrect, see https://docs.npmjs.com/deprecating-and-undeprecating-packages-or-package-versions

That said, I agree that it’s not really necessary in this particular case, especially since there seems to be a fix now.

kettanaito commented 2 months ago

Released: v2.4.7 🎉

This has been released in v2.4.7!

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.