AndersDJohnson / msw-expect

Assert on mocked requests with MSW & Jest.

Home Page:https://andersdjohnson.github.io/msw-expect/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

getAllHeaders is not a function

textbook opened this issue · comments

This package is exactly what I was looking for, but unfortunately caused an error when I tried to use it:

FetchError: request to https://example.com/ failed, reason: _req$headers2.getAllHeaders is not a function

From msw@0.28.0, when headers-utils was upgraded to v3 (see mswjs/msw@93a4f16), the getAllHeaders method no longer exists - it was renamed to all (see mswjs/headers-polyfill@1601873).

This is only a problem when using msw directly, rather than via msw-expect which currently brings along its own version of msw. I wonder if that's the right approach, though; should msw be a peer dependency, so people can bring along whatever compatible version they're currently using?

The simple fix for compatibility with 0.28.0-0.30.1 (plus 0.31.0-beta.0, but I wouldn't rely on that yet!) is:

-    const headersMap = req.headers?.getAllHeaders();
+    const headersMap = req.headers?.all();

plus some updates to src/test/native.test.ts for other changes to MSW's interface.

Example implementation at https://github.com/textbook/msw-expect/commit/f98267a460587f9d884aa3a18e4c012f8d6b53df, but I wanted to check on the dependency issue before just opening a PR.

@textbook Glad to hear you're liking msw-expect! Sorry about this bug. I'll get to work on a fix. Thanks!

@textbook OK, I think this should be resolved in v2.0.1, but please test and let me know. Also note there were some changes necessary for tests written in TypeScript - namely, having to assert the typeof your mockHandler()'d handler when passing it into rest.*, e.g., rest.get - see updates to docs.

Nice, that seems to do the job thanks 👍

One thing to note is that 0.y.z semvers are not compatible with one another, because the API's assumed to be changing more frequently pre-1.0.0, so even with msw@^0.21.3 as a peer dependency I still get the following with the latest version of MSW (0.31.0, which also works fine with this package):

npm WARN msw-expect@2.0.1 requires a peer of msw@^0.21.3 but none is installed. You must install peer dependencies yourself.

@textbook Thanks for testing! Yeah, I was curious what would happen with peer deps at 0.x since it's supposed to be semantically meaningless. Any ideas for a workaround? I wonder why msw hasn't bumped to 1.x by now...

You could do e.g. >=0.21.3 <1 to express the equivalent of ^0.21.3 for pre-1.x versions - you can try expressions out on https://semver.npmjs.com/ to see what they'll match.

@textbook Thanks for the tip! Corrected in #8 and published in v2.0.2.