Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest : Cannot find module 'msw/node' from ... #1809

Closed
4 tasks done
PanchoutNathan opened this issue Oct 30, 2023 · 11 comments
Closed
4 tasks done

Jest : Cannot find module 'msw/node' from ... #1809

PanchoutNathan opened this issue Oct 30, 2023 · 11 comments
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node

Comments

@PanchoutNathan
Copy link

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 18 or higher

Node.js version

18.18

Reproduction repository

openfun/joanie#445

Reproduction steps

yarn test numbers

Current behavior

● Test suite failed to run

Cannot find module 'msw/node' from 'mocks/server.ts'

Require stack:
  mocks/server.ts
  jest.setup.js



  at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/resolver.js:427:11)
  at Object.<anonymous> (mocks/server.ts:11:15)
  at Object.<anonymous> (jest.setup.js:8:17)

Expected behavior

There were no issues before upgrading to 2.0.1. The tests should run correctly.

@PanchoutNathan PanchoutNathan added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node labels Oct 30, 2023
@mattcosta7
Copy link
Contributor

@PanchoutNathan from that repo it looks like you may not have seen this

https://mswjs.io/docs/migrations/1.x-to-2.x/#cannot-find-module-mswnode-jsdom

in the docs for the migration from 1.x to 2.x.

Let us know if this doesn't help resolve your issue here! I'm going to close, assuming this should fix the issue, but please reopen if it does not

@jd-carroll
Copy link

@mattcosta7 Setting testEnvironmentOptions still shouldn't be necessary as that has a global impact (for all modules) not just msw.

The real issue is how the "exports" of msw are resolved.

In the msw package.json, there are the following lines:

msw/package.json

Lines 16 to 17 in ea28bd9

"./browser": {
"node": null,

msw/package.json

Lines 23 to 24 in ea28bd9

"./node": {
"browser": null,

msw/package.json

Lines 30 to 31 in ea28bd9

"./native": {
"browser": null,

Why is msw specifing an environment that doesn't exist? The nulls should be removed from the export mapping.

The issue is that resolvers such as export.resolver (which is what is used in jest) will look up exported environments like this:

https://github.com/lukeed/resolve.exports/blob/ac545e5b1b0845c0cae9d5e728531db85becc3fa/src/utils.ts#L133-L137

I know this is a little hard to read, but it reads along the lines of this:

  • If there is an export (m) for the target value
  • If the export is a string, use that...
  • If the export is an array, loop over each entry...
  • Otherwise assume the export is an object, and iterate over each key in the object
  • If a key in the object exists in the conditions provided, return the value from that object attempting to resolve any additional conditions

So there are two ways to resolve this issue:

  1. Remove the nulls from the export mappings in package.json
  2. Move the entries with null to the end of the object instead of the first (after default), this will allow any other conditions to be resolved first

I'm not aware of any reason why having an environment condition with a null entry would be required in the exports. If there is, please share cause I am trying to expand my knowledge of ESM ❤️

@jd-carroll
Copy link

@PanchoutNathan - I'd re-open this as a bug

@jd-carroll

This comment was marked as outdated.

@mattcosta7
Copy link
Contributor

I'm not aware of any reason why having an environment condition with a null entry would be required in the exports. If there is, please share cause I am trying to expand my knowledge of ESM ❤️

Quick response just explaining the reasoning here

setupServer is not actually valid in a browser
setupWorker is not actually valid in node

Since these require globals from specific environments the null values block them and lead to build instead of runtime errors.

Jests jsdom environment resolves using browser exports, which makes this more complicated, and ideally the suggestion is to configure jest to use the node import instead of the browser one for msw. In jest/jsdom msw still needs to operate as if it's running in node and not as if it's running in a browser. It's a bit complicated.

A custom resolver can be configured to do this, and patch package can remove the null or configure it differently.

@jd-carroll
Copy link

I understand that this is turning into a bit of a Holy War and I've see the comments in issues like #1787 & #1786.

However... I still feel that there is a bug here. I have spent an extensive amount of time debugging issues related to msw in my environment (vast majority not msw). I've also created an "extension" to msw to add some functionality I needed. (read I've been deep in the weeds with msw for the last week)

All that to say, I've resolved this import issue (at least for my use cases) locally -> without touching any of the jest settings.

In my initial assessment I was wrong about one thing: When the export resolver looks up a subpath and tries to match conditions it is going in object key order of the export object not the conditions as I previously thought.

This matters because the problem is unidirectional, meaning the problem is dealing with "browser like" in node and never "node like" in the browser.

Therefore, if you change the order of the conditions in the export objects the issue is resolved.

I'm including an updated patch below, please give it a try because if I am wrong I would love to understand why! ❤️

@jd-carroll
Copy link

Note the order of conditions in each export subpath. Importantly, browser is always last (unless there is a node: null)

Also, I updated the stub package.json's (i.e native/package.json). This is very likely not necessary, but included it for completeness.

msw-npm-2.0.11-653c526018.patch

diff --git a/native/package.json b/native/package.json
index a44c5d0e4327961c0b600402058ca54d18dec450..3652faa63c205cd1c5c6014e771968217e9bd49a 100644
--- a/native/package.json
+++ b/native/package.json
@@ -1,6 +1,6 @@
 {
-  "browser": null,
   "main": "../lib/native/index.js",
   "module": "../lib/native/index.mjs",
-  "types": "../lib/native/index.d.ts"
+  "types": "../lib/native/index.d.ts",
+  "browser": null
 }
diff --git a/node/package.json b/node/package.json
index 395e0757599fd89e96eda01e7c6588d9390d9bd8..57cd4eaf3a720c7078ac45b6f61fd4fdbb85878e 100644
--- a/node/package.json
+++ b/node/package.json
@@ -1,6 +1,6 @@
 {
-  "browser": null,
   "main": "../lib/node/index.js",
   "module": "../lib/node/index.mjs",
-  "types": "../lib/node/index.d.ts"
+  "types": "../lib/node/index.d.ts",
+  "browser": null
 }
diff --git a/package.json b/package.json
index caa140dc3caa6844459847a832c366b613b718a3..935a090ac986b40895fb49f497724615f5c7e9b8 100644
--- a/package.json
+++ b/package.json
@@ -14,24 +14,24 @@
       "default": "./lib/core/index.js"
     },
     "./browser": {
-      "node": null,
       "types": "./lib/browser/index.d.ts",
       "require": "./lib/browser/index.js",
       "import": "./lib/browser/index.mjs",
+      "node": null,
       "default": "./lib/browser/index.js"
     },
     "./node": {
-      "browser": null,
       "types": "./lib/node/index.d.ts",
       "require": "./lib/node/index.js",
       "import": "./lib/node/index.mjs",
+      "browser": null,
       "default": "./lib/node/index.mjs"
     },
     "./native": {
-      "browser": null,
       "types": "./lib/native/index.d.ts",
       "require": "./lib/native/index.js",
       "import": "./lib/native/index.mjs",
+      "browser": null,
       "default": "./lib/native/index.js"
     },
     "./mockServiceWorker.js": "./lib/mockServiceWorker.js",

@mswjs-interceptors-npm-0.25.13-02b879c411.patch

diff --git a/ClientRequest/package.json b/ClientRequest/package.json
index 4ee863900b8d67f320b4675f3d9666ee10d61262..bfe2e5cf4ffd502e96511b9be9e79dc48a2cc576 100644
--- a/ClientRequest/package.json
+++ b/ClientRequest/package.json
@@ -1,6 +1,6 @@
 {
   "main": "../lib/node/interceptors/ClientRequest/index.js",
   "module": "../lib/node/interceptors/ClientRequest/index.mjs",
-  "browser": null,
-  "types": "../lib/node/interceptors/ClientRequest/index.d.ts"
+  "types": "../lib/node/interceptors/ClientRequest/index.d.ts",
+  "browser": null
 }
diff --git a/RemoteHttpInterceptor/package.json b/RemoteHttpInterceptor/package.json
index bc5d53aaffbbd17864028ec47062a66a0191a3c3..099d1be93b580a850ed57bdd70fe88ecf5f6713b 100644
--- a/RemoteHttpInterceptor/package.json
+++ b/RemoteHttpInterceptor/package.json
@@ -1,6 +1,6 @@
 {
   "main": "../lib/node/RemoteHttpInterceptor.js",
   "module": "../lib/node/RemoteHttpInterceptor.mjs",
-  "browser": null,
-  "types": "../lib/node/RemoteHttpInterceptor.d.ts"
+  "types": "../lib/node/RemoteHttpInterceptor.d.ts",
+  "browser": null
 }
diff --git a/XMLHttpRequest/package.json b/XMLHttpRequest/package.json
index 8830bae0e2f893df8e80629f10503719451f23e2..0cf8814cbc925e7e4a4c4f8cf2c8cc888a42c527 100644
--- a/XMLHttpRequest/package.json
+++ b/XMLHttpRequest/package.json
@@ -1,6 +1,6 @@
 {
   "main": "../lib/node/interceptors/XMLHttpRequest/index.js",
   "module": "../lib/node/interceptors/XMLHttpRequest/index.mjs",
-  "browser": "../lib/browser/interceptors/XMLHttpRequest/index.js",
-  "types": "../lib/node/interceptors/XMLHttpRequest/index.d.ts"
+  "types": "../lib/node/interceptors/XMLHttpRequest/index.d.ts",
+  "browser": "../lib/browser/interceptors/XMLHttpRequest/index.js"
 }
diff --git a/fetch/package.json b/fetch/package.json
index 5e5dac857354660542b403210ae36e059a5478df..e489fef86622cfd6bbb031b456a860d324d596e7 100644
--- a/fetch/package.json
+++ b/fetch/package.json
@@ -1,6 +1,6 @@
 {
   "main": "../lib/node/interceptors/fetch/index.js",
   "module": "../lib/node/interceptors/fetch/index.mjs",
-  "browser": "../lib/browser/interceptors/fetch/index.js",
-  "types": "../lib/node/interceptors/fetch/index.d.ts"
+  "types": "../lib/node/interceptors/fetch/index.d.ts",
+  "browser": "../lib/browser/interceptors/fetch/index.js"
 }
diff --git a/package.json b/package.json
index 7a5e276423d5d59820b7c49f7eeff8ef6779d2cb..249203ad762b3aea470d6e86d65713721e609778 100644
--- a/package.json
+++ b/package.json
@@ -8,59 +8,59 @@
   "exports": {
     ".": {
       "types": "./lib/node/index.d.ts",
+      "require": "./lib/node/index.js",
+      "import": "./lib/node/index.mjs",
       "browser": {
         "types": "./lib/browser/index.d.ts",
         "require": "./lib/browser/index.js",
         "import": "./lib/browser/index.mjs",
         "default": "./lib/browser/index.js"
       },
-      "require": "./lib/node/index.js",
-      "import": "./lib/node/index.mjs",
       "default": "./lib/node/index.js"
     },
     "./ClientRequest": {
-      "browser": null,
       "types": "./lib/node/interceptors/ClientRequest/index.d.ts",
       "require": "./lib/node/interceptors/ClientRequest/index.js",
       "import": "./lib/node/interceptors/ClientRequest/index.mjs",
+      "browser": null,
       "default": "./lib/node/interceptors/ClientRequest/index.js"
     },
     "./XMLHttpRequest": {
+      "types": "./lib/node/interceptors/XMLHttpRequest/index.d.ts",
+      "require": "./lib/node/interceptors/XMLHttpRequest/index.js",
+      "import": "./lib/node/interceptors/XMLHttpRequest/index.mjs",
       "browser": {
         "types": "./lib/browser/interceptors/XMLHttpRequest/index.d.ts",
         "require": "./lib/browser/interceptors/XMLHttpRequest/index.js",
         "import": "./lib/browser/interceptors/XMLHttpRequest/index.mjs",
         "default": "./lib/browser/interceptors/XMLHttpRequest/index.js"
       },
-      "types": "./lib/node/interceptors/XMLHttpRequest/index.d.ts",
-      "require": "./lib/node/interceptors/XMLHttpRequest/index.js",
-      "import": "./lib/node/interceptors/XMLHttpRequest/index.mjs",
       "default": "./lib/node/interceptors/XMLHttpRequest/index.js"
     },
     "./fetch": {
+      "types": "./lib/node/interceptors/fetch/index.d.ts",
+      "require": "./lib/node/interceptors/fetch/index.js",
+      "import": "./lib/node/interceptors/fetch/index.mjs",
       "browser": {
         "types": "./lib/browser/interceptors/fetch/index.d.ts",
         "require": "./lib/browser/interceptors/fetch/index.js",
         "import": "./lib/browser/interceptors/fetch/index.mjs",
         "default": "./lib/browser/interceptors/fetch/index.js"
       },
-      "types": "./lib/node/interceptors/fetch/index.d.ts",
-      "require": "./lib/node/interceptors/fetch/index.js",
-      "import": "./lib/node/interceptors/fetch/index.mjs",
       "default": "./lib/node/interceptors/fetch/index.js"
     },
     "./RemoteHttpInterceptor": {
-      "browser": null,
       "types": "./lib/node/RemoteHttpInterceptor.d.ts",
       "require": "./lib/node/RemoteHttpInterceptor.js",
       "import": "./lib/node/RemoteHttpInterceptor.mjs",
+      "browser": null,
       "default": "./lib/node/RemoteHttpInterceptor.js"
     },
     "./presets/node": {
-      "browser": null,
       "types": "./lib/node/presets/node.d.ts",
       "require": "./lib/node/presets/node.js",
       "import": "./lib/node/presets/node.mjs",
+      "browser": null,
       "default": "./lib/node/presets/node.js"
     },
     "./presets/browser": {

Note: This change is required in both msw and @mswjs/interceptors

@mattcosta7
Copy link
Contributor

mattcosta7 commented Dec 30, 2023

Note the order of conditions in each export subpath. Importantly, browser is always last (unless there is a node: null)

Also, I updated the stub package.json's (i.e native/package.json). This is very likely not necessary, but included it for completeness.

msw-npm-2.0.11-653c526018.patch

diff --git a/native/package.json b/native/package.json
index a44c5d0e4327961c0b600402058ca54d18dec450..3652faa63c205cd1c5c6014e771968217e9bd49a 100644
--- a/native/package.json
+++ b/native/package.json
@@ -1,6 +1,6 @@
 {
-  "browser": null,
   "main": "../lib/native/index.js",
   "module": "../lib/native/index.mjs",
-  "types": "../lib/native/index.d.ts"
+  "types": "../lib/native/index.d.ts",
+  "browser": null
 }
diff --git a/node/package.json b/node/package.json
index 395e0757599fd89e96eda01e7c6588d9390d9bd8..57cd4eaf3a720c7078ac45b6f61fd4fdbb85878e 100644
--- a/node/package.json
+++ b/node/package.json
@@ -1,6 +1,6 @@
 {
-  "browser": null,
   "main": "../lib/node/index.js",
   "module": "../lib/node/index.mjs",
-  "types": "../lib/node/index.d.ts"
+  "types": "../lib/node/index.d.ts",
+  "browser": null
 }
diff --git a/package.json b/package.json
index caa140dc3caa6844459847a832c366b613b718a3..935a090ac986b40895fb49f497724615f5c7e9b8 100644
--- a/package.json
+++ b/package.json
@@ -14,24 +14,24 @@
       "default": "./lib/core/index.js"
     },
     "./browser": {
-      "node": null,
       "types": "./lib/browser/index.d.ts",
       "require": "./lib/browser/index.js",
       "import": "./lib/browser/index.mjs",
+      "node": null,
       "default": "./lib/browser/index.js"
     },
     "./node": {
-      "browser": null,
       "types": "./lib/node/index.d.ts",
       "require": "./lib/node/index.js",
       "import": "./lib/node/index.mjs",
+      "browser": null,
       "default": "./lib/node/index.mjs"
     },
     "./native": {
-      "browser": null,
       "types": "./lib/native/index.d.ts",
       "require": "./lib/native/index.js",
       "import": "./lib/native/index.mjs",
+      "browser": null,
       "default": "./lib/native/index.js"
     },
     "./mockServiceWorker.js": "./lib/mockServiceWorker.js",

@mswjs-interceptors-npm-0.25.13-02b879c411.patch

diff --git a/ClientRequest/package.json b/ClientRequest/package.json
index 4ee863900b8d67f320b4675f3d9666ee10d61262..bfe2e5cf4ffd502e96511b9be9e79dc48a2cc576 100644
--- a/ClientRequest/package.json
+++ b/ClientRequest/package.json
@@ -1,6 +1,6 @@
 {
   "main": "../lib/node/interceptors/ClientRequest/index.js",
   "module": "../lib/node/interceptors/ClientRequest/index.mjs",
-  "browser": null,
-  "types": "../lib/node/interceptors/ClientRequest/index.d.ts"
+  "types": "../lib/node/interceptors/ClientRequest/index.d.ts",
+  "browser": null
 }
diff --git a/RemoteHttpInterceptor/package.json b/RemoteHttpInterceptor/package.json
index bc5d53aaffbbd17864028ec47062a66a0191a3c3..099d1be93b580a850ed57bdd70fe88ecf5f6713b 100644
--- a/RemoteHttpInterceptor/package.json
+++ b/RemoteHttpInterceptor/package.json
@@ -1,6 +1,6 @@
 {
   "main": "../lib/node/RemoteHttpInterceptor.js",
   "module": "../lib/node/RemoteHttpInterceptor.mjs",
-  "browser": null,
-  "types": "../lib/node/RemoteHttpInterceptor.d.ts"
+  "types": "../lib/node/RemoteHttpInterceptor.d.ts",
+  "browser": null
 }
diff --git a/XMLHttpRequest/package.json b/XMLHttpRequest/package.json
index 8830bae0e2f893df8e80629f10503719451f23e2..0cf8814cbc925e7e4a4c4f8cf2c8cc888a42c527 100644
--- a/XMLHttpRequest/package.json
+++ b/XMLHttpRequest/package.json
@@ -1,6 +1,6 @@
 {
   "main": "../lib/node/interceptors/XMLHttpRequest/index.js",
   "module": "../lib/node/interceptors/XMLHttpRequest/index.mjs",
-  "browser": "../lib/browser/interceptors/XMLHttpRequest/index.js",
-  "types": "../lib/node/interceptors/XMLHttpRequest/index.d.ts"
+  "types": "../lib/node/interceptors/XMLHttpRequest/index.d.ts",
+  "browser": "../lib/browser/interceptors/XMLHttpRequest/index.js"
 }
diff --git a/fetch/package.json b/fetch/package.json
index 5e5dac857354660542b403210ae36e059a5478df..e489fef86622cfd6bbb031b456a860d324d596e7 100644
--- a/fetch/package.json
+++ b/fetch/package.json
@@ -1,6 +1,6 @@
 {
   "main": "../lib/node/interceptors/fetch/index.js",
   "module": "../lib/node/interceptors/fetch/index.mjs",
-  "browser": "../lib/browser/interceptors/fetch/index.js",
-  "types": "../lib/node/interceptors/fetch/index.d.ts"
+  "types": "../lib/node/interceptors/fetch/index.d.ts",
+  "browser": "../lib/browser/interceptors/fetch/index.js"
 }
diff --git a/package.json b/package.json
index 7a5e276423d5d59820b7c49f7eeff8ef6779d2cb..249203ad762b3aea470d6e86d65713721e609778 100644
--- a/package.json
+++ b/package.json
@@ -8,59 +8,59 @@
   "exports": {
     ".": {
       "types": "./lib/node/index.d.ts",
+      "require": "./lib/node/index.js",
+      "import": "./lib/node/index.mjs",
       "browser": {
         "types": "./lib/browser/index.d.ts",
         "require": "./lib/browser/index.js",
         "import": "./lib/browser/index.mjs",
         "default": "./lib/browser/index.js"
       },
-      "require": "./lib/node/index.js",
-      "import": "./lib/node/index.mjs",
       "default": "./lib/node/index.js"
     },
     "./ClientRequest": {
-      "browser": null,
       "types": "./lib/node/interceptors/ClientRequest/index.d.ts",
       "require": "./lib/node/interceptors/ClientRequest/index.js",
       "import": "./lib/node/interceptors/ClientRequest/index.mjs",
+      "browser": null,
       "default": "./lib/node/interceptors/ClientRequest/index.js"
     },
     "./XMLHttpRequest": {
+      "types": "./lib/node/interceptors/XMLHttpRequest/index.d.ts",
+      "require": "./lib/node/interceptors/XMLHttpRequest/index.js",
+      "import": "./lib/node/interceptors/XMLHttpRequest/index.mjs",
       "browser": {
         "types": "./lib/browser/interceptors/XMLHttpRequest/index.d.ts",
         "require": "./lib/browser/interceptors/XMLHttpRequest/index.js",
         "import": "./lib/browser/interceptors/XMLHttpRequest/index.mjs",
         "default": "./lib/browser/interceptors/XMLHttpRequest/index.js"
       },
-      "types": "./lib/node/interceptors/XMLHttpRequest/index.d.ts",
-      "require": "./lib/node/interceptors/XMLHttpRequest/index.js",
-      "import": "./lib/node/interceptors/XMLHttpRequest/index.mjs",
       "default": "./lib/node/interceptors/XMLHttpRequest/index.js"
     },
     "./fetch": {
+      "types": "./lib/node/interceptors/fetch/index.d.ts",
+      "require": "./lib/node/interceptors/fetch/index.js",
+      "import": "./lib/node/interceptors/fetch/index.mjs",
       "browser": {
         "types": "./lib/browser/interceptors/fetch/index.d.ts",
         "require": "./lib/browser/interceptors/fetch/index.js",
         "import": "./lib/browser/interceptors/fetch/index.mjs",
         "default": "./lib/browser/interceptors/fetch/index.js"
       },
-      "types": "./lib/node/interceptors/fetch/index.d.ts",
-      "require": "./lib/node/interceptors/fetch/index.js",
-      "import": "./lib/node/interceptors/fetch/index.mjs",
       "default": "./lib/node/interceptors/fetch/index.js"
     },
     "./RemoteHttpInterceptor": {
-      "browser": null,
       "types": "./lib/node/RemoteHttpInterceptor.d.ts",
       "require": "./lib/node/RemoteHttpInterceptor.js",
       "import": "./lib/node/RemoteHttpInterceptor.mjs",
+      "browser": null,
       "default": "./lib/node/RemoteHttpInterceptor.js"
     },
     "./presets/node": {
-      "browser": null,
       "types": "./lib/node/presets/node.d.ts",
       "require": "./lib/node/presets/node.js",
       "import": "./lib/node/presets/node.mjs",
+      "browser": null,
       "default": "./lib/node/presets/node.js"
     },
     "./presets/browser": {

Note: This change is required in both msw and @mswjs/interceptors

I'm away for a bit still, but with these changes the browser and node conditions will never be satisfied

Import and/or require will resolve in either environment, so browser and node would never be used

Export conditions resolve in order, and the first matching condition is used, not the best matching one

@jd-carroll
Copy link

Damn, you are correct. Ok, last attempt here before I throw in the towel... -> Why not use consistent nested subpath definitions instead of having top level require/import.

Meaning, instead of:

    ".": {
      "types": "./lib/node/index.d.ts",
      "browser": {
        "types": "./lib/browser/index.d.ts",
        "require": "./lib/browser/index.js",
        "import": "./lib/browser/index.mjs",
        "default": "./lib/browser/index.js"
      },
      "require": "./lib/node/index.js",
      "import": "./lib/node/index.mjs",
      "default": "./lib/node/index.js"
    },

Do:

  "exports": {
    ".": {
      "node": {
        "types": "./lib/node/index.d.ts",
        "require": "./lib/node/index.js",
        "import": "./lib/node/index.mjs",
        "default": "./lib/node/index.js"
      },
      "browser": {
        "types": "./lib/browser/index.d.ts",
        "require": "./lib/browser/index.js",
        "import": "./lib/browser/index.mjs",
        "default": "./lib/browser/index.js"
      },
      "default": "./lib/node/index.js"
    },
    "./ClientRequest": {
      "node": {
        "types": "./lib/node/interceptors/ClientRequest/index.d.ts",
        "require": "./lib/node/interceptors/ClientRequest/index.js",
        "import": "./lib/node/interceptors/ClientRequest/index.mjs",
        "default": "./lib/node/interceptors/ClientRequest/index.js"
      },
      "browser": null,
      "default": "./lib/node/interceptors/ClientRequest/index.js"
    },

This way, priority is always given to node and it is still returning null for the browser in the necessary places... ? (My assumption is that true browser environments will never have any condition other than browser.)

Also are you aware of any situations where the environment conditions contain neither node nor browser leading to the default entry being used?

Also apologies for my persistence on this, msw has been a huge help to us and ultimately just trying to make it easier for folks to use ❤️

PS- Happy new year to those on the Gregorian calendar!

@jd-carroll
Copy link

Alright, throwing in the towel...

While that idea in theory would work, because its JSDOM, the resolvable conditions are default, require, and browser. No node among them...

Shucks, appreciate you humoring me on this

@clyncha
Copy link

clyncha commented Feb 15, 2024

I updated my jest.config.js file as suggested for this error https://mswjs.io/docs/migrations/1.x-to-2.x#cannot-find-module-mswnode-jsdom in my code but I still get Cannot find module 'mocks/node' error. Did something change?

My config file

/* eslint-disable @typescript-eslint/no-var-requires */
const nextJest = require('next/jest');

const createJestConfig = nextJest({
  // Provide the path to your Next.js app to load next.config.js and .env files in your test environment
  dir: './',
});

// Add any custom config to be passed to Jest
/** @type {import('jest').Config} */
const customJestConfig = {
  // Add code coverage
  collectCoverage: true,
  collectCoverageFrom: ['**/src/*.{js,jsx,ts,tsx}', '!**/node_modules/**'],
  coverageReporters: ['lcov'],

  // Add more setup options before each test is run
  setupFiles: ['./jest.polyfills.js'],
  setupFilesAfterEnv: ['<rootDir>/config/jest/setupTests.js'],
  testEnvironmentOptions: {
    customExportConditions: [],
  },
  testEnvironment: 'jest-environment-jsdom',

  moduleNameMapper: {
    '^components/(.*)$': ['<rootDir>/src/components/$1'],
    '^config/(.*)$': ['<rootDir>/config/$1'],
    '^lib/(.*)$': ['<rootDir>/src/lib/$1'],
    '^src/(.*)$': ['<rootDir>/src/$1'],
  },

  transform: {
    '\\.(gql|graphql)$': '<rootDir>/src/lib/jest-transform-graphql-shim.js',
  },
};

// workaround for ESM issues until this is fixed: https://github.com/vercel/next.js/issues/35634
// note: query-string package couldn't use same approach as uuid above
const jestConfig = async () => {
  // createJestConfig called this way to ensure that next/jest can load the Next.js config which is async
  const nextJestConfig = await createJestConfig(customJestConfig)();
  // /node_modules/ is the first pattern
  nextJestConfig.transformIgnorePatterns[0] = '/node_modules/(?!query-string)/';
  return nextJestConfig;
};

module.exports = jestConfig;

@github-actions github-actions bot locked and limited conversation to collaborators Oct 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants