Browse Source

tests(fixtures): Make loadFixtures more generic (#26667)

Evan Purkhiser 3 years ago
parent
commit
c559d60da7
5 changed files with 56 additions and 26 deletions
  1. 1 0
      package.json
  2. 0 23
      tests/js/sentry-test/loadFixtures.js
  3. 49 0
      tests/js/sentry-test/loadFixtures.ts
  4. 1 3
      tests/js/setup.js
  5. 5 0
      yarn.lock

+ 1 - 0
package.json

@@ -147,6 +147,7 @@
     "@storybook/builder-webpack5": "^6.2.9",
     "@storybook/react": "^6.2.9",
     "@storybook/theming": "^6.2.9",
+    "@types/js-yaml": "^4.0.1",
     "@visual-snapshot/jest": "^2.0.2",
     "@wojtekmaj/enzyme-adapter-react-17": "0.6.1",
     "babel-eslint": "^10.1.0",

+ 0 - 23
tests/js/sentry-test/loadFixtures.js

@@ -1,23 +0,0 @@
-/* eslint import/no-nodejs-modules:0 */
-import fs from 'fs';
-
-export function loadFixtures(dir) {
-  // Dynamically load fixtures
-  const modules = fs.readdirSync(dir).map(filename => require(`${dir}/${filename}`));
-
-  modules.forEach(exports => {
-    if (Object.keys(exports).includes('default')) {
-      throw new Error('Javascript fixtures cannot use default export');
-    }
-  });
-
-  const fixtures = modules.reduce(
-    (acc, exports) => ({
-      ...acc,
-      ...exports,
-    }),
-    {}
-  );
-
-  return fixtures;
-}

+ 49 - 0
tests/js/sentry-test/loadFixtures.ts

@@ -0,0 +1,49 @@
+/* global __dirname */
+/* eslint import/no-nodejs-modules:0 */
+import fs from 'fs';
+import path from 'path';
+
+const FIXTURES_ROOT = path.join(__dirname, '../../fixtures');
+
+type Options = {
+  /**
+   * Flatten all fixtures to together into a single object
+   */
+  flatten?: boolean;
+};
+
+/**
+ * Loads a directory of fixtures. Supports js and json fixtures.
+ */
+export function loadFixtures(dir: string, opts: Options = {}) {
+  const from = path.join(FIXTURES_ROOT, dir);
+  const files = fs.readdirSync(from);
+
+  const fixturesPairs = files.map(file => {
+    const filePath = path.join(from, file);
+
+    if (/[jt]sx?$/.test(file)) {
+      const module = require(filePath);
+
+      if (Object.keys(module).includes('default')) {
+        throw new Error('Javascript fixtures cannot use default export');
+      }
+
+      return [file, module] as const;
+    }
+
+    if (/json$/.test(file)) {
+      return [file, JSON.parse(fs.readFileSync(filePath).toString())] as const;
+    }
+
+    throw new Error(`Invalid fixture type found: ${file}`);
+  });
+
+  const fixtures = Object.fromEntries(fixturesPairs);
+
+  if (opts.flatten) {
+    return Object.values(fixtures).reduce((acc, val) => ({...acc, ...val}), {});
+  }
+
+  return fixtures;
+}

+ 1 - 3
tests/js/setup.js

@@ -1,4 +1,3 @@
-/* global __dirname */
 import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
 import Enzyme from 'enzyme'; // eslint-disable-line no-restricted-imports
 import MockDate from 'mockdate';
@@ -45,8 +44,7 @@ MockDate.set(constantDate);
  * Load all files in `tests/js/fixtures/*` as a module.
  * These will then be added to the `TestStubs` global below
  */
-const fixturesPath = `${__dirname}/../fixtures/js-stubs`;
-const fixtures = loadFixtures(fixturesPath);
+const fixtures = loadFixtures('js-stubs', {flatten: true});
 
 /**
  * Global testing configuration

+ 5 - 0
yarn.lock

@@ -2997,6 +2997,11 @@
   resolved "https://registry.yarnpkg.com/@types/js-cookie/-/js-cookie-2.2.6.tgz#f1a1cb35aff47bc5cfb05cb0c441ca91e914c26f"
   integrity sha512-+oY0FDTO2GYKEV0YPvSshGq9t7YozVkgvXLty7zogQNuCxBhT9/3INX9Q7H1aRZ4SUDRXAKlJuA4EA5nTt7SNw==
 
+"@types/js-yaml@^4.0.1":
+  version "4.0.1"
+  resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-4.0.1.tgz#5544730b65a480b18ace6b6ce914e519cec2d43b"
+  integrity sha512-xdOvNmXmrZqqPy3kuCQ+fz6wA0xU5pji9cd1nDrflWaAWtYLLGk5ykW0H6yg5TVyehHP1pfmuuSaZkhP+kspVA==
+
 "@types/json-schema@*", "@types/json-schema@^7.0.3", "@types/json-schema@^7.0.4", "@types/json-schema@^7.0.5", "@types/json-schema@^7.0.6":
   version "7.0.7"
   resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.7.tgz#98a993516c859eb0d5c4c8f098317a9ea68db9ad"