Skip to content

An open source alternative to Tableau. Embeddable visual analytic

fix(test): replace nanoid jest mapping with local CJS shim (#516)

Elwynn Chen0ead9e8c8779
diff --git a/packages/graphic-walker/jest.config.js b/packages/graphic-walker/jest.config.js
index 0855789b..c96227d3 100644
--- a/packages/graphic-walker/jest.config.js
+++ b/packages/graphic-walker/jest.config.js
@@ -4,7 +4,7 @@ module.exports = {
testEnvironment: 'node',
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
- '^nanoid$': '<rootDir>/../../node_modules/nanoid/index.cjs',
+ '^nanoid$': '<rootDir>/tests/shims/nanoid.cjs',
},
testPathIgnorePatterns: ['<rootDir>/dist/'],
};
diff --git a/packages/graphic-walker/tests/shims/nanoid.cjs b/packages/graphic-walker/tests/shims/nanoid.cjs
new file mode 100644
index 00000000..c9c242b6
--- /dev/null
+++ b/packages/graphic-walker/tests/shims/nanoid.cjs
@@ -0,0 +1,31 @@
+/**
+ * CommonJS stand-in for `nanoid` used only by Jest (see moduleNameMapper).
+ * nanoid >= 4 ships pure ESM, which Jest's CJS runtime cannot require, and
+ * mapping into node_modules broke whenever hoisting changed the installed
+ * version. Mirrors the upstream secure implementation.
+ */
+const crypto = require('crypto');
+
+const urlAlphabet = 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict';
+
+const nanoid = (size = 21) => {
+ const bytes = crypto.randomBytes(size);
+ let id = '';
+ while (size--) {
+ id += urlAlphabet[bytes[size] & 63];
+ }
+ return id;
+};
+
+const customAlphabet = (alphabet, defaultSize = 21) => {
+ return (size = defaultSize) => {
+ const bytes = crypto.randomBytes(size);
+ let id = '';
+ while (size--) {
+ id += alphabet[bytes[size] % alphabet.length];
+ }
+ return id;
+ };
+};
+
+module.exports = { nanoid, customAlphabet, urlAlphabet };