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

Documentation needed for ESM modules in a CSJ world #39

Closed
joelnet opened this issue Aug 1, 2023 · 1 comment
Closed

Documentation needed for ESM modules in a CSJ world #39

joelnet opened this issue Aug 1, 2023 · 1 comment

Comments

@joelnet
Copy link

joelnet commented Aug 1, 2023

Problem
Jest not compatible with ESM modules.

Environment
Jest: 26.6.3
babel-jest: 26.6.3

jest.config.js

const esModules = ["p-timeout"].join("|");

module.exports = {
  transformIgnorePatterns: [`/node_modules/(?!${esModules})`],
};

.babelrc

{
  "presets": ["next/babel"],
}

Error

  Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    /Users/jthoms/dev/@paciolan/evenue-next-server/src/node_modules/p-timeout/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export class TimeoutError extends Error {
                                                                                             ^^^^^^

    SyntaxError: Unexpected token 'export'

      11 | import { promiseHandler } from "./lib/promises";
      12 | import { logger } from "./logger";
    > 13 | import pTimeout from "p-timeout";
         | ^
      14 |
      15 | const remoteComponentCacheAge = ms(process.env.REMOTE_COMPONENT_CACHE_AGE || "24 hours"); // prettier-ignore
      16 | const zookeeperUrl = process.env.ZKP_URL || "localhost:2181";

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
      at Object.<anonymous>

I have tried solutions from:

My next options:

  • Using an old version of this library (v4)
  • Rolling my own lib

I am unsure how to get this module working with Jest and am seeking some help.

@sindresorhus
Copy link
Owner

I am unsure how to get this module working with Jest and am seeking some help.

This would be a question to ask on the Jest issue tracker. This package is valid ESM.

@sindresorhus sindresorhus closed this as not planned Won't fix, can't repro, duplicate, stale Aug 1, 2023
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

No branches or pull requests

2 participants