question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Cannot use uuid with Jest

See original GitHub issue

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: https://github.com/vercel/next.js/issues/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

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
herdsothomcommented, Jul 8, 2022

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): https://github.com/uuidjs/uuid/issues/451#issuecomment-1112328417

3reactions
johnhuntercommented, Jul 7, 2022

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)/)`,
  ],
Read more comments on GitHub >

github_iconTop Results From Across the Web

ESM Problems with Jest@26 and Node.js 14.x #451 - GitHub
Trying to test a file where I import uuid results with an error: ○ Test suite failed to run ... [BUG] Cannot use...
Read more >
Can't use uuid module when running tests with Jest
The problem is version incompatibility between Jest and uuid . uuid version 8 supports native ES modules while Jest version 26 doesn't.
Read more >
uuid - NPM Package Overview - Socket.dev
Start using Socket to analyze uuid and its 0 dependencies to secure your app ... Bug Fixes. add Jest/jsdom compatibility (#642) (16f9c46) ...
Read more >
Module not found: Can't resolve 'uuid/v4' - Super User
Install uuid library via node package manager i.e. npm npm install uuid. Then import uuidv4 from uuid library
Read more >
A missing Jest mock feature - Medium
My problem here was quite simple. I have implemented a uuid() function that creates a unique id for my objects. Each time it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found