uuid: ESM Problems with Jest@26 and Node.js 14.x

Hi, I’m using jest to test my project. Trying to test a file where I import uuid results with an error:

 ● Test suite failed to run

    SyntaxError: The requested module 'uuid' does not provide an export named 'v1'

          at async Promise.all (index 0)
      at jasmine2 (node_modules/jest-jasmine2/build/index.js:228:5)

I saw @dps910 comment and I guess it’s something similar. I’m using UUID@8, node@14 and Jest@26 (and it’s freshly-supported esm modules implementation).

Jest command: node --experimental-vm-modules .\node_modules\jest\bin\jest.js The file that I’m testing imports uuid like you suggested:

import { v1 as uuidv1 } from 'uuid';
uuidv1();

It fails only when I’m testing though.

_Originally posted by @Tzahile in https://github.com/uuidjs/uuid/issues/245#issuecomment-628650062_

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 3
  • Comments: 19 (8 by maintainers)

Commits related to this issue

Most upvoted comments

My workaround for this was to manually map the uuid module to it’s commonJS export:

// jest.config.js
moduleNameMapper: {
    // Force module uuid to resolve with the CJS entry point, because Jest does not support package.json.exports. See https://github.com/uuidjs/uuid/issues/451
    "uuid": require.resolve('uuid'),
},

EDIT: This might not work depending on your projects dependency structure. See https://github.com/microsoft/accessibility-insights-web/pull/5421#issuecomment-1109168149 for a much better explanation.

This works and is more scoped than overriding exports conditions, but has the downside that it is essentially a silent yarn resolution; it forces every dependency chain through uuid to use whatever version of uuid happens to be hoisted, even if some chains want older versions.


EDIT: Please see https://github.com/uuidjs/uuid/issues/451#issuecomment-1377066303 for an even more specific solution.

Turns out mock service worker (@mswjs) has its own implementation of uuidv4 in a file called uuid.ts.

This caused an unexpected side effect when implementing the suggested moduleNameMapper fix. It caused the following error in the @mswjs package:

TypeError: uuid_1.uuidv4 is not a function

To resolve this, simply use a regex with an imperative start and end like so:

moduleNameMapper: {
  "^uuid$": require.resolve('uuid'),
}

✅ This ensures only the actual uuid package is affected.

I have released uuid@9.0.0-beta.0 that should restore compatibility with Jest. Please try it out and let me know if it fixes the Jest interoperability (to be tested with jest@29.0.0-alpha.1).

Hi @ctavan, for my project we still need the workaround for jest tests to pass after updating to 9.0.0 with jest 29:

# jest.config.js
module.exports {
  moduleNameMapper: {
    // Workaround for Jest not having ESM support yet
    // See: https://github.com/uuidjs/uuid/issues/451
    uuid: require.resolve('uuid'),

Im still seeing it as well. Node 16, “uuid”: “^9.0.0”, “jest”: “^29.5.0”, “jest-environment-jsdom”: “^29.5.0”,

image

I’m still seeing this issue. uuid 9.0.0 and jest 29.4.0/29.5.0, and node 16.19.1

It looks like jest is not picking up the right version of the uuid module, opting for the one in esm-browser folder

 FAIL  services/user/test/api.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:

    /Users/jim/development/domo/foyer/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'

      40 |
      41 |     setupModelTest() {
    > 42 |         const { MongoMemoryServer } = require('mongodb-memory-server');
         |                                       ^
      43 |
      44 |         // const { MongoMemoryServer } = require('mongodb-memory-server');             // use this if mongo not on host
      45 |         // const { MongoMemoryServer } = require('mongodb-memory-server-core');

      at Runtime.createScriptFromCode (node_modules/jest-cli/node_modules/jest-runtime/build/index.js:1495:14)
      at Object.<anonymous> (node_modules/mongodb-memory-server-core/src/util/lockfile.ts:7:1)
      at Object.<anonymous> (node_modules/mongodb-memory-server-core/src/util/MongoBinary.ts:9:1)
      at Object.<anonymous> (node_modules/mongodb-memory-server-core/src/util/MongoInstance.ts:3:1)
      at Object.<anonymous> (node_modules/mongodb-memory-server-core/src/MongoMemoryServer.ts:15:1)
      at Object.<anonymous> (node_modules/mongodb-memory-server-core/src/index.ts:2:1)
      at Object.<anonymous> (node_modules/mongodb-memory-server/index.js:5:20)
      at Object.require [as setupModelTest] (test/test-utils.js:42:39)
      at Object.setupModelTest (services/user/test/api.test.js:6:6)

I’ve tried coaxing it to force it to transpile, but have had no luck.

@pl12133 i love you buddy

So that seems like an awesome workaround, but it doesn’t work if you’re using a jest.config.mjs instead as you can’t require things there. Any thoughts on what the syntax might be in that case?

@Tzahile as pointed out by @TrySound, ESM support in jest is not complete yet. I think you will have to wait for ESM support in jest to be finished before you can use it with this library.