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

ESM Problems with Jest@26 and Node.js 14.x #451

Closed
Tracked by #636
ctavan opened this issue May 14, 2020 · 19 comments · Fixed by #642
Closed
Tracked by #636

ESM Problems with Jest@26 and Node.js 14.x #451

ctavan opened this issue May 14, 2020 · 19 comments · Fixed by #642
Labels
waiting-for-upstream Waiting for upstream fixes

Comments

@ctavan
Copy link
Member

ctavan commented May 14, 2020

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 #245 (comment)

@TrySound
Copy link
Member

Looks like "exports" is not supported yet.
jestjs/jest#9430 (comment)

@ctavan
Copy link
Member Author

ctavan commented May 14, 2020

@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.

dirkdev98 added a commit to compasjs/compas that referenced this issue May 15, 2020
Had issues with ES exports and Jest see uuidjs/uuid#451
@SimenB
Copy link
Contributor

SimenB commented May 15, 2020

FWIW, tracking issue: jestjs/jest#9771

@pl12133
Copy link

pl12133 commented Apr 28, 2022

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 microsoft/accessibility-insights-web#5421 (comment) 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 #451 (comment) for an even more specific solution.

@SimenB
Copy link
Contributor

SimenB commented Apr 28, 2022

Jest 28 does support it, see #616 for latest

@pl12133
Copy link

pl12133 commented Apr 28, 2022

I can confirm #616 fixes this issue in my project using Jest 28 and jest-environment-jsdom. Thanks for working on it.

@grosch
Copy link

grosch commented Jun 23, 2022

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?

@SimenB
Copy link
Contributor

SimenB commented Jun 23, 2022

@damdafayton
Copy link

damdafayton commented Aug 2, 2022

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 microsoft/accessibility-insights-web#5421 (comment) 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.

Thanks so much. This solved my problem which I was suffering since 15 hours. Altough I have jest 28 and did everything on StackOverflow nothing helped except this.

This is my post on SO regarding my problem: https://stackoverflow.com/questions/73203367/this-annoying-jest-error-again-syntaxerror-unexpected-token-export/73203803#73203803

@ctavan
Copy link
Member Author

ctavan commented Aug 5, 2022

I have released [email protected] that should restore compatibility with Jest. Please try it out and let me know if it fixes the Jest interoperability (to be tested with [email protected]).

cy6erskunk added a commit to cy6erskunk/moviesmap that referenced this issue Nov 24, 2022
Also add uuid@v9 as devDep to mitigate the issue
uuidjs/uuid#451
@robbieaverill
Copy link

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'),

@mbrookson
Copy link

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.

@SimenB
Copy link
Contributor

SimenB commented Jan 10, 2023

Note that moduleNameMapper is not needed for Jest 29 and uuid v9. Anyone still having issues on latest should open a new issue with a reproduction.

@mbrookson
Copy link

@SimenB Yep, that's true and great that the root cause is fixed but until dependency packages are updated it's a waiting game. I'm using next-auth and some @aws-sdk packages which are still reliant on v8 unfortunately.

lucasgarfield added a commit to lucasgarfield/image-builder-frontend that referenced this issue Feb 7, 2023
This commit bumps Jest to 29.4.1. This requires adding two additional
development dependencies.

As of Jest 28 `jest-environment-jsdom` is no longer shipped with Jest by
default, and has therefore been added as a dependency.

`uuid` was also added as a dev dependency. Jest 29 introduced some
compatibility issues with `uuid` that were fixed in `uuid@9`:
uuidjs/uuid#451 (comment)

We import and use `uuid` in our tests, but as it was a dependencies
dependency we did not control which version was installed. Version 8
(what was installed) causes Jest tests to fail like so:
```
    SyntaxError: Unexpected token 'export'

    > 29 | import { v4 as uuidv4 } from 'uuid';
```
croissanne pushed a commit to osbuild/image-builder-frontend that referenced this issue Feb 7, 2023
This commit bumps Jest to 29.4.1. This requires adding two additional
development dependencies.

As of Jest 28 `jest-environment-jsdom` is no longer shipped with Jest by
default, and has therefore been added as a dependency.

`uuid` was also added as a dev dependency. Jest 29 introduced some
compatibility issues with `uuid` that were fixed in `uuid@9`:
uuidjs/uuid#451 (comment)

We import and use `uuid` in our tests, but as it was a dependencies
dependency we did not control which version was installed. Version 8
(what was installed) causes Jest tests to fail like so:
```
    SyntaxError: Unexpected token 'export'

    > 29 | import { v4 as uuidv4 } from 'uuid';
```
ryelo pushed a commit to RedHatInsights/image-builder-frontend-build that referenced this issue Feb 7, 2023
This commit bumps Jest to 29.4.1. This requires adding two additional
development dependencies.

As of Jest 28 `jest-environment-jsdom` is no longer shipped with Jest by
default, and has therefore been added as a dependency.

`uuid` was also added as a dev dependency. Jest 29 introduced some
compatibility issues with `uuid` that were fixed in `uuid@9`:
uuidjs/uuid#451 (comment)

We import and use `uuid` in our tests, but as it was a dependencies
dependency we did not control which version was installed. Version 8
(what was installed) causes Jest tests to fail like so:
```
    SyntaxError: Unexpected token 'export'

    > 29 | import { v4 as uuidv4 } from 'uuid';
```
ryelo pushed a commit to RedHatInsights/image-builder-frontend-build that referenced this issue Feb 7, 2023
This commit bumps Jest to 29.4.1. This requires adding two additional
development dependencies.

As of Jest 28 `jest-environment-jsdom` is no longer shipped with Jest by
default, and has therefore been added as a dependency.

`uuid` was also added as a dev dependency. Jest 29 introduced some
compatibility issues with `uuid` that were fixed in `uuid@9`:
uuidjs/uuid#451 (comment)

We import and use `uuid` in our tests, but as it was a dependencies
dependency we did not control which version was installed. Version 8
(what was installed) causes Jest tests to fail like so:
```
    SyntaxError: Unexpected token 'export'

    > 29 | import { v4 as uuidv4 } from 'uuid';
```
@ginxx009
Copy link

Hoping this can help everyone solve this problem

  • Jest Version : "@types/jest": "^29.4.0",
  • UUID Version : "uuid": "^9.0.0"

Even without the jest.config.js this will work because if you have multiple configs inside your project it will generate an error saying : Multiple Configuration Found problem

So in your __tests__ folder and App-test.tsx if you are using typescript

import 'react-native';
import React from 'react';
import App from '../src/App';
import {v4 as uuidv4} from 'uuid';

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

it('renders correctly', () => {
  renderer.create(<App />);
});

jest.mock('uuid', () => {
  return {
    v4: jest.fn(() => 1)
  }
})

@felipebutcher
Copy link

@pl12133 i love you buddy

@jmbldwn
Copy link

jmbldwn commented Apr 3, 2023

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.

@vkotu
Copy link

vkotu commented Apr 14, 2023

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

image

@SimenB
Copy link
Contributor

SimenB commented Apr 14, 2023

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

Successfully merging a pull request may close this issue.