|
@@ -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;
|