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

provide a way to avoid having to specify __esModule:true in jest.mock() #9482

Open
dalimian opened this issue Aug 22, 2024 · 3 comments
Open

Comments

@dalimian
Copy link

Describe the feature

after migrating jest from babel to swc we found that now our unit tests have to specify an __esModule: true when mocking a module that has a default export.

example:

jest.mock('path/to/some-module.js', () => ({
 default: jest.fn(),
 __esModule: true,
}));

worth a mention that it seems this is only needed when mocking a module that is dynamically imported with import() (statically imported modules are fine)

we previously avoided this with babel config via specifying a noInterop: true on a plugin called babel-plugin-dynamic-import-node

here is the full Babel config

{
  "presets": [
    ["@babel/preset-env", {
      "exclude": ["proposal-dynamic-import"]
    }],
    "@babel/preset-react"
  ],
  "plugins": [
    ["babel-plugin-dynamic-import-node", { "noInterop": true }],
    "@babel/plugin-transform-runtime",
    ["babel-plugin-module-resolver", {
      "root": ["."]
      }
    }]
  ]
}

can you please provide an equivalent for swc config

our swc config is

{
  "$schema": "https://swc.rs/schema.json",
  "sourceMaps": "inline",
  "jsc": {
    "target": "es5",
    "externalHelpers": true,
    "loose": true,
    "baseUrl": "./",
    "parser": {
      "syntax": "ecmascript",
      "jsx": true,
      "dynamicImport": true
    },
    "transform": {
      "react": {
        "pragma": "React.createElement",
        "pragmaFrag": "React.Fragment",
        "throwIfNamespace": true,
        "development": false,
        "useBuiltins": false
      },
      "optimizer": {
        "globals": {
          "vars": {
            "NODE_ENV": "true"
          }
        }
      }
    },
    "experimental": {
      "plugins": [
        [
          "swc_mut_cjs_exports",
          {}
        ]
      ]
    }
  },
  "module": {
    "type": "commonjs"
  }
}

Babel plugin or link to the feature description

No response

Additional context

No response

@kdy1
Copy link
Member

kdy1 commented Aug 22, 2024

You may try noInterop: true for swc

@kdy1
Copy link
Member

kdy1 commented Aug 22, 2024

And please provide a minimal reproduction

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 22, 2024
@dalimian
Copy link
Author

dalimian commented Aug 25, 2024

thanks @kdy1 , here is github and codesandbox links that minimally reproduce the issue, whichever you prefer

I did try noInterop: true, it did fix this issue but introduced another issue where nothing from node_modules could be imported, links above reproduce that as well

@kdy1 kdy1 reopened this Aug 25, 2024
@kdy1 kdy1 added this to the Planned (Low priority) milestone Aug 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants