Просмотр исходного кода

ref(test): throw if a fixture ever gets overriden (#32551)

* ref(test): throw if a fixture ever gets overriden

* ref(test): drop comments

* ref(test): use the right fixture type
Jonas 3 лет назад
Родитель
Сommit
f4de34e315
2 измененных файлов с 32 добавлено и 11 удалено
  1. 31 10
      tests/js/sentry-test/loadFixtures.ts
  2. 1 1
      tests/js/spec/components/searchSyntax/parser.spec.tsx

+ 31 - 10
tests/js/sentry-test/loadFixtures.ts

@@ -4,6 +4,8 @@
 import fs from 'fs';
 import fs from 'fs';
 import path from 'path';
 import path from 'path';
 
 
+import TestStubFixtures from '../../fixtures/js-stubs/types';
+
 const FIXTURES_ROOT = path.join(__dirname, '../../fixtures');
 const FIXTURES_ROOT = path.join(__dirname, '../../fixtures');
 
 
 type Options = {
 type Options = {
@@ -16,34 +18,53 @@ type Options = {
 /**
 /**
  * Loads a directory of fixtures. Supports js and json fixtures.
  * Loads a directory of fixtures. Supports js and json fixtures.
  */
  */
-export function loadFixtures(dir: string, opts: Options = {}) {
+export function loadFixtures(dir: string, opts: Options = {}): TestStubFixtures {
   const from = path.join(FIXTURES_ROOT, dir);
   const from = path.join(FIXTURES_ROOT, dir);
   const files = fs.readdirSync(from);
   const files = fs.readdirSync(from);
 
 
-  const fixturesPairs = files.map(file => {
+  // @ts-ignore, this is a partial definition
+  const fixtures: TestStubFixtures = {};
+
+  for (const file of files) {
     const filePath = path.join(from, file);
     const filePath = path.join(from, file);
 
 
     if (/[jt]sx?$/.test(file)) {
     if (/[jt]sx?$/.test(file)) {
       const module = require(filePath);
       const module = require(filePath);
 
 
-      if (Object.keys(module).includes('default')) {
+      if (module.default) {
         throw new Error('Javascript fixtures cannot use default export');
         throw new Error('Javascript fixtures cannot use default export');
       }
       }
 
 
-      return [file, module] as const;
+      fixtures[file] = module;
+      continue;
     }
     }
-
     if (/json$/.test(file)) {
     if (/json$/.test(file)) {
-      return [file, JSON.parse(fs.readFileSync(filePath).toString())] as const;
+      fixtures[file] = JSON.parse(fs.readFileSync(filePath).toString());
+      continue;
     }
     }
 
 
     throw new Error(`Invalid fixture type found: ${file}`);
     throw new Error(`Invalid fixture type found: ${file}`);
-  });
-
-  const fixtures = Object.fromEntries(fixturesPairs);
+  }
 
 
   if (opts.flatten) {
   if (opts.flatten) {
-    return Object.values(fixtures).reduce((acc, val) => ({...acc, ...val}), {});
+    // @ts-ignore, this is a partial definition
+    const flattenedFixtures: TestStubFixtures = {};
+
+    for (const moduleKey in fixtures) {
+      for (const moduleExport in fixtures[moduleKey]) {
+        // Check if our flattenedFixtures already contains a key with the same export.
+        // If it does, we want to throw and make sure that we dont silently override the fixtures.
+        if (flattenedFixtures?.[moduleKey]?.[moduleExport]) {
+          throw new Error(
+            `Flatten will override module ${flattenedFixtures[moduleKey]} with ${fixtures[moduleKey][moduleExport]}`
+          );
+        }
+
+        flattenedFixtures[moduleExport] = fixtures[moduleKey][moduleExport];
+      }
+    }
+
+    return flattenedFixtures;
   }
   }
 
 
   return fixtures;
   return fixtures;

+ 1 - 1
tests/js/spec/components/searchSyntax/parser.spec.tsx

@@ -55,7 +55,7 @@ const normalizeResult = (tokens: TokenResult<Token>[]) =>
   });
   });
 
 
 describe('searchSyntax/parser', function () {
 describe('searchSyntax/parser', function () {
-  const testData: Record<string, TestCase[]> = loadFixtures('search-syntax');
+  const testData = loadFixtures('search-syntax') as unknown as Record<string, TestCase[]>;
 
 
   const registerTestCase = (testCase: TestCase) =>
   const registerTestCase = (testCase: TestCase) =>
     it(`handles ${testCase.query}`, () => {
     it(`handles ${testCase.query}`, () => {