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

[BUG] Cannot use uuid with Jest #640

Closed
2 tasks done
herdsothom opened this issue Jul 6, 2022 · 12 comments
Closed
2 tasks done

[BUG] Cannot use uuid with Jest #640

herdsothom opened this issue Jul 6, 2022 · 12 comments
Labels

Comments

@herdsothom
Copy link

Before you begin...

  • I have searched the existing issues
  • I am not using version 13.x of node (if so, please upgrade)

Description of the problem

When I import the aws-sdk/client-dynamodb (which uses uuid), I can no longer test with the latest version of Jest.

 jest
 FAIL  __tests__/example.test.js
  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • 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/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /sandbox/node_modules/uuid/dist/esm-browser/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { default as v1 } from './v1.js';
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14)
      at Object.<anonymous> (node_modules/@aws-sdk/client-dynamodb/dist-cjs/protocols/Aws_json1_0.js:7:16)

Test Suites: 1 failed, 1 total

Recipe for reproducing

Please see this sandbox:

https://codesandbox.io/s/boring-resonance-g84z3y?file=/__tests__/example.test.js

I basically just go from this starting point example: https://github.com/vercel/next.js/tree/canary/examples/with-jest
And then import dynamodb somewhere.

Additional information

I also raised an issue in Next.js but got redirected here: vercel/next.js#38368

Environment

npx: installed 1 in 0.997s

  System:
    OS: Linux 5.4 Debian GNU/Linux 10 (buster) 10 (buster)
    CPU: (16) x64 Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
    Memory: 9.27 GB / 62.72 GB
    Container: Yes
    Shell: 5.0.3 - /bin/bash
  Binaries:
    Node: 14.18.1 - ~/.nvm/versions/node/v14.18.1/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v14.18.1/bin/yarn
    npm: 6.14.15 - ~/.nvm/versions/node/v14.18.1/bin/npm
  npmPackages:
    @aws-sdk/client-dynamodb: ^3.121.0 => 3.121.0
    @testing-library/jest-dom: 5.16.4 => 5.16.4
    @testing-library/react: 13.2.0 => 13.2.0
    @testing-library/user-event: 14.2.0 => 14.2.0
    @types/react: 18.0.9 => 18.0.9
    jest: 28.1.0 => 28.1.0
    jest-environment-jsdom: 28.1.0 => 28.1.0
    next: latest => 12.2.0
    react: ^18.1.0 => 18.2.0
    react-dom: ^18.1.0 => 18.2.0
    typescript: 4.6.4 => 4.6.4
@herdsothom herdsothom added the bug label Jul 6, 2022
@johnhunter
Copy link

I have the same issue. Started with Jest@28. I'm using uuid in a private library where Jest works fine. However, that private library, when used as a dependency in a project reproduces the error reported. Other library ESM dependencies work as expected.

@johnhunter
Copy link

This may be a problem with Jest (they changed Babel configuration in v28). It seems that Jest does not transform the uuid dependency.

The following workaround in jest.config got tests passing for me:

  transformIgnorePatterns: [
    `node_modules/(?!(uuid)/)`,
  ],

@herdsothom
Copy link
Author

transformIgnorePatterns: [
node_modules/(?!(uuid)/),
],

Thanks for the message John.
I added that line in the config in the sandbox environment I linked but the test still failed, did you try it there or are you meaning in your own project?

Also might I just further ask - why is this necessary? I thought by default all of node_modules is ignored anyway?

Thanks for the help!

@johnhunter
Copy link

This was in my own project. The thinking was to get Jest to transform that node_module - so it ends up as a commonjs module which Jest was expecting. Sorry that didn't work for you.

@herdsothom
Copy link
Author

This was in my own project. The thinking was to get Jest to transform that node_module - so it ends up as a commonjs module which Jest was expecting. Sorry that didn't work for you.

Ah makes sense, thanks for clarifying. still pretty new to JS ecosystem!

@chrisahardie
Copy link

Jest is pulling in the "default" ESModules export, which only has experimental support in Jest you need to opt into.

Is it possible to add a require to the exports in package.json so Jest will pull in the correct file per this comment?

...
"exports": {
    ".": {
      "require": "./dist/index.js",
      "node": {
        "module": "./dist/esm-node/index.js",
        ...

@herdsothom
Copy link
Author

Jest is pulling in the "default" ESModules export, which only has experimental support in Jest you need to opt into.

Is it possible to add a require to the exports in package.json so Jest will pull in the correct file per this comment?

...
"exports": {
    ".": {
      "require": "./dist/index.js",
      "node": {
        "module": "./dist/esm-node/index.js",
        ...

wow! this fixed it.
So i went into my node_modules folder, into the uuid package, and added that "require" line into the package.json.

Only issue with this approach is this is going to get deleted whenever I delete/reinstall node_modules, and probably when I update packages.
Cheers for the help. wish i understood better why thats required.

@herdsothom
Copy link
Author

For anyone else looking for a fix for this, this PR solves it: https://github.com/uuidjs/uuid/pull/616/files
So you can copy those changes into your package.json for UUID until it gets merged.

Also a better fix, that worked for me, that doesnt require changing something in node_modules (so will persist through reinstalling your node_modules): #451 (comment)

@kikoanis
Copy link

For anyone else looking for a fix for this, this PR solves it: https://github.com/uuidjs/uuid/pull/616/files So you can copy those changes into your package.json for UUID until it gets merged.

Also a better fix, that worked for me, that doesnt require changing something in node_modules (so will persist through reinstalling your node_modules): #451 (comment)

Thanks
or your could use https://www.npmjs.com/package/patch-package to patch it till the PR is merged in

@MarcPorciuncula
Copy link

Would it be possible to release a v8.3.3 with this fix? It's hard to get deps to bump to v9 because of breaking changes

@ctavan
Copy link
Member

ctavan commented Oct 18, 2022

Unfortunately supporting jest out of the box was technically a breaking change for this library as we added a CommonJS browser build that users receive who previously received an ESM build.

As far as I know the change to jest that broke interoperability with uuid also got introduced with a major version bump of jest, so I think this is working as intended.

@paulchiu
Copy link

For me a combination of the suggestions by @chrisahardie and @kikoanis was the fix:

  1. Install patch-package
  2. Create patch for uuid
  3. Integrate the use of patch-package into test/CI commands

Tip: If you are manually creating your patch, by default patch-package ignores package.json so you need to run the command npx patch-package --exclude // uuid instead.

Additional tip: If your CI/CD builds Docker images for test runs, make sure the ./patches folder is copied into the image.

Patch `./patches/uuid+8.3.2.patch`
diff --git a/node_modules/uuid/package.json b/node_modules/uuid/package.json
index f0ab371..0561718 100644
--- a/node_modules/uuid/package.json
+++ b/node_modules/uuid/package.json
@@ -20,6 +20,7 @@
   "main": "./dist/index.js",
   "exports": {
     ".": {
+      "require": "./dist/index.js",
       "node": {
         "module": "./dist/esm-node/index.js",
         "require": "./dist/index.js",

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

No branches or pull requests

7 participants