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

fix: set customExportConditions to [''] #22

Merged
merged 1 commit into from
Nov 13, 2024
Merged

Conversation

kettanaito
Copy link
Member

Move the customExportConditions from userland to this package to ensure msw/node is resolved correctly in Jest.

@kettanaito kettanaito changed the title fix: set customExportConditions to [] fix: set customExportConditions to [''] Nov 13, 2024
@kettanaito kettanaito merged commit e136f35 into main Nov 13, 2024
1 check passed
@kettanaito kettanaito deleted the fix/export-conditions branch November 13, 2024 09:42
@kettanaito
Copy link
Member Author

Released: v0.0.9 🎉

This has been released in v0.0.9!

Make sure to always update to the latest version (npm i jest-fixed-jsdom@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

@jiri-prokop-pb
Copy link
Contributor

Hey, just FYI. This change is breaking our usage, unfortunately.

We'd prefer to keep it the previous way.

The problem is that with more complicated setups, you can't enforce export conditions to be [''] because it can break other things one uses in Jest.

For reference, right now we have the following patch for jest-runtime:

diff --git a/build/index.d.ts b/build/index.d.ts
index 5bdf9fdf30fd0f28a1760ac41404238b61b20b94..4936148bb1fc4ad390f183ebd2d5a0db75179873 100644
--- a/build/index.d.ts
+++ b/build/index.d.ts
@@ -84,6 +84,8 @@ declare class Runtime {
   private jestGlobals?;
   private readonly esmConditions;
   private readonly cjsConditions;
+  private readonly esmConditions_msw;
+  private readonly cjsConditions_msw;
   private isTornDown;
   constructor(
     config: Config.ProjectConfig,
diff --git a/build/index.js b/build/index.js
index 01cb4206525dab0e564e07366bb0de1cc68ae2d1..9d6c0a0da432c4e71bf7cb4b778d7349247062a0 100644
--- a/build/index.js
+++ b/build/index.js
@@ -266,6 +266,8 @@ class Runtime {
   jestGlobals;
   esmConditions;
   cjsConditions;
+  esmConditions_msw;
+  cjsConditions_msw;
   isTornDown = false;
   constructor(
     config,
@@ -335,6 +337,8 @@ class Runtime {
     this.cjsConditions = Array.from(
       new Set(['require', 'default', ...envExportConditions])
     );
+    this.esmConditions_msw = [...new Set(['import', 'default', ''])];
+    this.cjsConditions_msw = [...new Set(['require', 'default', ''])];
     if (config.automock) {
       config.setupFiles.forEach(filePath => {
         if (filePath.includes(NODE_MODULES)) {
@@ -1274,14 +1278,22 @@ class Runtime {
   _resolveCjsModule(from, to) {
     return to
       ? this._resolver.resolveModule(from, to, {
-          conditions: this.cjsConditions
+          conditions:
+            ['msw', 'msw/node'].includes(to) ||
+            to.startsWith('@mswjs/interceptors')
+              ? this.cjsConditions_msw
+              : this.cjsConditions
         })
       : from;
   }
   _resolveModule(from, to) {
     return to
       ? this._resolver.resolveModuleAsync(from, to, {
-          conditions: this.esmConditions
+          conditions:
+            ['msw', 'msw/node'].includes(to) ||
+            to.startsWith('@mswjs/interceptors')
+              ? this.esmConditions_msw
+              : this.esmConditions
         })
       : from;
   }

Without this many of our tests were failing with the solution suggested in MSW's doc.

Some time ago I started creating PR for Jest to allow specify export conditions in a more granular way but I wasn't able to finish it yet as I have other more important tasks. The patch above was just a hacky way to do it but I struggled to create something nice and generic.

So, maybe consider this 🙏

(Note: we are in transition to vitest right now but it may take us couple of weeks to finalize it in our monorepo)

@kettanaito
Copy link
Member Author

@jiri-prokop-pb, if this is breaking for you, provide a customExportConditions property override in your Jest config and list any other conditions you need after 'node'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants