Closed diego-aquino closed 1 week ago
I can confirm, this is also happening to me.
Previously this test was working:
expect(response.headers.get("Content-Type")).toBe("text/html");
But started to fail with version 2.4.4
and above:
- Expected:
"text/html"
+ Received:
null
Any potential movement here? This is holding off our upgrade as we have a lot of tests designed this way.
I just got bit by this bug. couldn't figure why my remix server was serving everything as plain/text for a few hours now when MSW mocks were turned on, only to find out my express server stopped sending Content-Type
headers at all.
worked around this bug by loading my mock setupServer script with npm command instead of inline in my express server so now doing something like in my package.json:
scripts: {
dev: 'tsx -r ./mocks/index.ts server.ts'
}
msw
to verify.@diego-aquino, I confirm that your reproduction repo has the tests passing on msw@latest
:
✓ tests/failing-on-2.4.9.test.ts (1)
✓ preserves init values in headers created before the server was started
Just make sure to remove the pervious msw
version because it looks like npm is resolving @mswjs/interceptors
from the 2.4.3, which is an extremely old version that doesn't contain the fix.
Prerequisites
Environment check
msw
versionNode.js version
v20.17.0
Reproduction repository
https://github.com/diego-aquino/msw-headers-init-repro-with-vitest
Reproduction steps
npm install
npm run test
Current behavior
The
README.md
of the reproduction repository contains a more detailed explanation. Here's a summary:I created two identical test suites, except for the imported MSW version:
passing-on-v2.4.3.test.ts
- This suite is usingmsw@2.4.3
and passes.failing-on-v2.4.9.test.ts
- This suite is usingmsw@2.4.9
and fails.Before starting the MSW server, each suite creates a
Headers
instance with initial values, appends a new header and checks if the initial values are preserved and the instance contains the correct values.After starting the server with
server.listen()
, the suites append yet another header to the instance and re-run the checks. Withmsw@2.4.3
, the existing values are preserved and the new header is appended. However, the current headers are lost withmsw@2.4.9
when used in aRequest
orfetch
call. The only kept header is the one appended after starting the server, as if the instance was empty before.The problem does not appear to occur when using the headers directly, because only the last expect reading the headers from a
Request
fails.Expected behavior
For headers created before
server.listen()
,Request
andfetch
should consider their previous values after changed withset
orappend
. This was the behavior withmsw@2.4.3
and appears to have changed inmsw@2.4.4
and later.Let me know if there is anything unclear I could clarify!