panva / jose

JWA, JWS, JWE, JWT, JWK, JWKS for Node.js, Browser, Cloudflare Workers, Deno, Bun, and other Web-interoperable runtimes.
MIT License
5.4k stars 309 forks source link

`msw` is unable to intercept JWKS request in node environment due to namespace (`* as`) import #669

Closed cubeghost closed 4 months ago

cubeghost commented 4 months ago

What happened?

Request mocking library msw is unable to intercept requests when the http or https libraries are imported using namespace style imports, for example import * as http from 'node:http' instead of import http from 'node:http'. https://github.com/mswjs/msw/issues/2049

I could use nock, but msw is what my team uses elsewhere and it's what's recommended for use with vitest.

In my replicated example, I can confirm that changing the import style in runtime/node/fetch_jwks.ts allows the request to be mocked and the test to pass.

Version

5.2.4

Runtime

Node.js

Runtime Details

Node 18 & 20, vitest 1.5.3, msw 2.2.14

Code to reproduce

https://github.com/cubeghost/msw-jose-import-issue/blob/main/index.test.ts

Required

panva commented 4 months ago

https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node does not have a default export for node built-ins, node's actual runtime does, but its community maintained types don't.

https://github.com/mswjs/msw/issues/2049#issuecomment-2002640920 is a very hot take, node's actual runtime doesn't care, DT says to import all named exports and doesn't support the default, nock works with both, msw doesn't.

Ultimately, if import { get } from 'node:http' is a valid way of getting the get export from a node builtin than so is import * as http from 'node:http'; http.get and the way it's written at the moment is within the node's community driven DT types.