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)
Links to this issue
Commits related to this issue
- stdlib: fork uuid v4 from uuidjs/uuid Had issues with ES exports and Jest see https://github.com/uuidjs/uuid/issues/451 — committed to compasjs/compas by dirkdev98 4 years ago
- Migrate config to Jest 29 Also add uuid@v9 as devDep to mitigate the issue https://github.com/uuidjs/uuid/issues/451 — committed to cy6erskunk/moviesmap by cy6erskunk 2 years ago
- Dev Deps: Bump Jest to 29.4.1 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... — committed to lucasgarfield/image-builder-frontend by lucasgarfield a year ago
- Dev Deps: Bump Jest to 29.4.1 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... — committed to osbuild/image-builder-frontend by lucasgarfield a year ago
- Dev Deps: Bump Jest to 29.4.1 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... — committed to RedHatInsights/image-builder-frontend-build by invalid-email-address a year ago
- Dev Deps: Bump Jest to 29.4.1 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... — committed to RedHatInsights/image-builder-frontend-build by invalid-email-address a year ago
- Fixed tests relating to uuid package. Based on advice here: https://github.com/uuidjs/uuid/issues/451#issuecomment-1377066303 — committed to data-forge-notebook/data-forge-notebook by ashleydavis 10 months ago
My workaround for this was to manually map the
uuidmodule to it’s commonJS export: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.
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 ofuuidv4in a file calleduuid.ts.This caused an unexpected side effect when implementing the suggested
moduleNameMapperfix. It caused the following error in the@mswjspackage:To resolve this, simply use a regex with an imperative start and end like so:
✅ This ensures only the actual
uuidpackage is affected.I have released
uuid@9.0.0-beta.0that should restore compatibility with Jest. Please try it out and let me know if it fixes the Jest interoperability (to be tested withjest@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:
Im still seeing it as well. Node 16, “uuid”: “^9.0.0”, “jest”: “^29.5.0”, “jest-environment-jsdom”: “^29.5.0”,
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
I’ve tried coaxing it to force it to transpile, but have had no luck.
FWIW, tracking issue: https://github.com/facebook/jest/issues/9771
@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
requirethings 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.