Selaa lähdekoodia

fixed typedoc issue, attempting to fix jest issue with a polyfill

telamonian 5 vuotta sitten
vanhempi
commit
4dd3e1d1d0

+ 41 - 1
packages/ui-components/src/icon/icon.ts

@@ -1,5 +1,45 @@
 import { nameFromPath } from '../utils';
 
+/**
+ * `require.context` polyfill for jest tests.
+ * Modified from https://stackoverflow.com/a/42191018/425458.
+ */
+// This condition actually should detect if it's an Node environment
+if (typeof require.context === 'undefined') {
+  const fs = require('fs');
+  const path = require('path');
+
+  require.context = (base: string, deep?: boolean, filter?: RegExp): any => {
+    const files: { [key: string]: boolean } = Object.create(null);
+
+    function readDirectory(directory: string) {
+      fs.readdirSync(directory).forEach((file: string) => {
+        const fullPath = path.resolve(directory, file);
+
+        if (fs.statSync(fullPath).isDirectory()) {
+          if (deep) readDirectory(fullPath);
+
+          return;
+        }
+
+        if (!filter.test(fullPath)) return;
+
+        files[fullPath] = true;
+      });
+    }
+
+    readDirectory(path.resolve(__dirname, base));
+
+    function Module(file: string) {
+      return require(file);
+    }
+
+    Module.keys = () => Object.keys(files);
+
+    return Module;
+  };
+}
+
 export namespace Icon {
   /**
    * Import all svgs from a directory. The input argument should be
@@ -16,7 +56,7 @@ export namespace Icon {
     }, []);
   }
 
-  export const defaultIcons = importSvgs(
+  export const defaultIcons: ReadonlyArray<IModel> = importSvgs(
     require.context('raw-loader!../../style/icons', true, /\.svg$/)
   );
 

+ 1 - 1
packages/ui-components/tdoptions.json

@@ -17,5 +17,5 @@
   },
   "esModuleInterop": true,
   "jsx": "react",
-  "types": ["webpack-env"]
+  "types": ["webpack-env", "node"]
 }

+ 1 - 1
packages/ui-components/tsconfig.json

@@ -2,7 +2,7 @@
   "extends": "../../tsconfigbase",
   "compilerOptions": {
     "outDir": "lib",
-    "types": ["webpack-env"],
+    "types": ["webpack-env", "node"],
     "rootDir": "src"
   },
   "include": ["src/**/*"]