Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Remove `@peculiar/webcrypto` polyfill from the Jest setup (#69466)

Nemanja Glumac3532b35b000a
diff --git a/frontend/test/jest-setup.js b/frontend/test/jest-setup.js
index e5b9009a9d3..1e240226477 100644
--- a/frontend/test/jest-setup.js
+++ b/frontend/test/jest-setup.js
@@ -1,6 +1,6 @@
+import { webcrypto } from "crypto";
import { TextDecoder, TextEncoder } from "util";
-import { Crypto, CryptoKey } from "@peculiar/webcrypto";
import { ReadableStream } from "web-streams-polyfill";
import "cross-fetch/polyfill";
import "raf/polyfill";
@@ -40,10 +40,12 @@ class JSDOMTextEncoder extends TextEncoder {
// global TextEncoder and Crypto are not available in jsdom + Jest, see
// https://stackoverflow.com/questions/70808405/how-to-set-global-textdecoder-in-jest-for-jsdom-if-nodes-util-textdecoder-is-ty
-// (hacky fix)
-delete globalThis.crypto;
-globalThis.crypto = new Crypto();
-globalThis.CryptoKey = CryptoKey;
+// Use Node.js native webcrypto instead of @peculiar/webcrypto polyfill
+Object.defineProperty(globalThis, "crypto", {
+ value: webcrypto,
+ writable: true,
+ configurable: true,
+});
global.TextEncoder = JSDOMTextEncoder;
global.TextDecoder = TextDecoder;
diff --git a/jest.config.js b/jest.config.js
index 304e1251021..8cc693cc3c4 100644
--- a/jest.config.js
+++ b/jest.config.js
@@ -4,6 +4,9 @@ const esmPackages = require("./jest.esm-packages.js");
const baseConfig = {
moduleNameMapper: {
+ // Force jose to use Node.js runtime instead of browser runtime in jsdom environment.
+ // The browser runtime expects CryptoKey to be globally available, which jsdom doesn't provide.
+ "^jose$": "<rootDir>/node_modules/jose/dist/node/cjs/index.js",
"^build-configs/(.*)$": "<rootDir>/frontend/build/$1",
"\\.(css|less)$": "<rootDir>/frontend/test/__mocks__/styleMock.js",
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":