From 9ad4e618019298d82732d49d00aafb846fb6bac7 Mon Sep 17 00:00:00 2001 From: Dan Bjorge Date: Mon, 25 Apr 2022 19:58:54 -0400 Subject: [PATCH] Add workaround for Jest 28 + uuid incompatibility --- jest.config.base.js | 1 + src/tests/common/resolver.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/tests/common/resolver.js diff --git a/jest.config.base.js b/jest.config.base.js index de3e314e9e9..e995a84ed15 100644 --- a/jest.config.base.js +++ b/jest.config.base.js @@ -40,6 +40,7 @@ module.exports = { }, ], ], + resolver: `${__dirname}/src/tests/common/resolver.js`, setupFilesAfterEnv: [`${__dirname}/src/tests/common/flush-promises-after-each-test.ts`], snapshotSerializers: [`${__dirname}/src/tests/common/typemoq-snapshot-serializer.ts`], testEnvironment: 'node', diff --git a/src/tests/common/resolver.js b/src/tests/common/resolver.js new file mode 100644 index 00000000000..825e7bc2d59 --- /dev/null +++ b/src/tests/common/resolver.js @@ -0,0 +1,30 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +module.exports = (path, options) => { + // Call the defaultResolver, so we leverage its cache, error handling, etc. + return options.defaultResolver(path, { + ...options, + // Use packageFilter to process parsed `package.json` before the resolution (see https://www.npmjs.com/package/resolve#resolveid-opts-cb) + packageFilter: pkg => { + // This is a workaround for https://github.com/uuidjs/uuid/pull/616 + // + // jest-environment-jsdom 28+ tries to use browser exports instead of default exports, + // but uuid only offers an ESM browser export and not a CommonJS one. Jest does not yet + // support ESM modules natively, so this causes a Jest error related to trying to parse + // "export" syntax. + // + // This workaround prevents Jest from considering uuid's module-based exports at all; + // it falls back to uuid's CommonJS+node "main" property. + // + // Once we're able to migrate our Jest config to ESM and a browser crypto + // implementation is available for the browser+ESM version of uuid to use (eg, via + // https://github.com/jsdom/jsdom/pull/3352 or a similar polyfill), this can go away. + if (pkg.name === 'uuid') { + delete pkg['exports']; + delete pkg['module']; + } + return pkg; + }, + }); +};