Skip to content

Commit

Permalink
feat: move component instrumentation to loader
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Apr 19, 2021
1 parent 4fa8cd3 commit 9152498
Show file tree
Hide file tree
Showing 92 changed files with 19,128 additions and 20,188 deletions.
43 changes: 32 additions & 11 deletions core/instrument/src/babel/extract-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,23 @@ import { analyze_components } from './analyze-component';
import { packageInfo } from '../misc/package-info';
import { readSourceFile } from '../misc/source-options';
import { LoadingDocStore, InstrumentOptions } from '../types';
import { getComponentProps } from '../misc/props-info';
import { getFileIinfo } from '../misc/file-info';
import { extractTests } from '../misc/jest-tests';

export interface ComponentParseData {
interface ComponentParseData {
component?: Component;
componentPackage?: PackageInfo;
}
const globalCache: {
[filePath: string]: ComponentParseData;
} = {};
export const extractComponent = async (
componentName: string,
filePath: string,
source?: string,
options?: InstrumentOptions,
): Promise<ComponentParseData> => {
const cacheKey = `${filePath}-${componentName}`;
if (globalCache[cacheKey]) {
return globalCache[cacheKey];
}
const follow = followImports(componentName, filePath, source, options);
const { components, resolver: resolveOptions } = options || {};
const { components, resolver: resolveOptions, propsLoaders = [], jest } =
options || {};

let component: Component;
let componentPackage: PackageInfo | undefined;
Expand Down Expand Up @@ -109,6 +106,31 @@ export const extractComponent = async (

component.request = follow.filePath;
component.fileName = path.basename(follow.filePath);
const propsFile = component.propsInfoFile || component.request;
if (propsFile) {
const propsInfo = await getComponentProps(
propsLoaders,
propsFile,
component.name,
component.source,
);
if (propsInfo) {
component.info = propsInfo;
}
}
const { fileInfo = true } = components || {};
if (fileInfo && component.request) {
component.fileInfo = await getFileIinfo(
component.request,
component.source,
);
}
if (jest !== false) {
const testResults = await extractTests([component.request], jest);
if (testResults) {
component.jest = testResults;
}
}
const saveSource = readSourceFile(
components?.sourceFiles,
follow.source,
Expand Down Expand Up @@ -140,8 +162,7 @@ export const extractComponent = async (
};
}

globalCache[cacheKey] = { component, componentPackage };
return globalCache[cacheKey];
return { component, componentPackage };
};

export const extractStoreComponent = async (
Expand Down
3 changes: 0 additions & 3 deletions core/instrument/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ import {
} from './types';

export * from './types';
export { getComponentProps } from './misc/props-info';
export { getFileIinfo } from './misc/file-info';
export { extractComponentTests } from './misc/jest-tests';

export { prettify };

Expand Down
131 changes: 35 additions & 96 deletions core/instrument/src/misc/jest-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
findJestConfig,
getRelatedTests,
} from '@component-controls/jest-extract';
import { Components, JestTests } from '@component-controls/core';
import { JestTests } from '@component-controls/core';

/**
* Separates the files into projects and runs jest tests
Expand All @@ -14,46 +14,30 @@ import { Components, JestTests } from '@component-controls/core';
* @returns return key/value pairs with test results and coverage associated with each file
*/
export const extractTests = async (
files: Record<string, string>,
files: string[],
options?: JestConfig,
): Promise<Record<string, JestTests>> => {
const tests = Object.keys(files).reduce(
(acc: Record<string, { key: string; files: string[] }[]>, key) => {
const component = files[key];
const projectFolder = findJestConfig(component);
if (!acc[projectFolder]) {
acc[projectFolder] = [];
}
acc[projectFolder].push({ key, files: [component] });
return acc;
},

{},
);
const projects = Object.keys(tests);
const results: Record<string, JestTests> = {};
for (const project of projects) {
const files = tests[project].reduce(
(
acc: {
testFiles: Record<string, string>;
coverageFiles: Record<string, string>;
},
component,
) => {
results[component.key] = { results: [], coverage: {} };
component.files.forEach(filePath =>
getRelatedTests(filePath).forEach(
f =>
(acc.testFiles[`.${path.sep}${path.relative(project, f)}`] =
component.key),
),
);
component.files.forEach(filePath => {
acc.coverageFiles[`.${path.sep}${path.relative(project, filePath)}`] =
component.key;
});
/* const dateModified = fs.statSync(projectFolder).mtime;
): Promise<JestTests | undefined> => {
if (!files.length) {
return undefined;
}
const projectFolder = findJestConfig(files[0]);
const tests = files.reduce(
(
acc: {
testFiles: string[];
coverageFiles: string[];
},
component,
) => {
acc.testFiles.push(
...getRelatedTests(component).map(
f => `.${path.sep}${path.relative(projectFolder, f)}`,
),
);
acc.coverageFiles.push(
`.${path.sep}${path.relative(projectFolder, component)}`,
);
/* const dateModified = fs.statSync(projectFolder).mtime;
const fileHash = createHash('md5')
.update(dateModified.toString())
Expand All @@ -72,61 +56,16 @@ export const extractTests = async (
coverage: cachedTest.coverage,
});
} */
return acc;
},
{ testFiles: {}, coverageFiles: {} },
);
if (Object.keys(files.testFiles).length) {
const testResults = await runProjectTests(
Object.keys(files.testFiles),
project,
{
collectCoverageOnlyFrom: Object.keys(files.coverageFiles),
...options,
},
);
if (testResults.coverage) {
Object.keys(testResults.coverage).forEach(covFile => {
const componentKey = files.coverageFiles[`.${path.sep}${covFile}`];
if (componentKey) {
results[componentKey].coverage[covFile] =
testResults.coverage[covFile];
}
});
}
if (testResults.results) {
testResults.results.forEach(r => {
const componentKey = files.testFiles[`.${path.sep}${r.testFilePath}`];
if (componentKey) {
results[componentKey].results.push(r);
}
});
}
}
}
return results;
};

/**
* runs the tests associated with the components and assign to field "jest"
* @param components key/Component list
* @param options jest runCLI options
*/
export const extractComponentTests = async (
components: Components,
options?: JestConfig,
): Promise<void> => {
const mapped = Object.keys(components).reduce(
(acc: Record<string, string>, key) => ({
...acc,
[key]: components[key].request as string,
}),
{},
return acc;
},
{ testFiles: [], coverageFiles: [] },
);
const extracted = await extractTests(mapped, options);
Object.keys(extracted).forEach(key => {
if (components[key]) {
components[key].jest = extracted[key];
}
});
if (tests.testFiles.length) {
const testResults = await runProjectTests(tests.testFiles, projectFolder, {
collectCoverageOnlyFrom: tests.coverageFiles,
...options,
});
return testResults;
}
return undefined;
};
2 changes: 1 addition & 1 deletion core/instrument/test/esm-props-info-external.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import { fixtureToTest, TestCallback } from './loadTestFiles';
import { getComponentProps } from '../src/index';
import { getComponentProps } from '../src/misc/props-info';

const createTest = (fileName: string, callback: TestCallback) =>
fixtureToTest(['esm', 'props-info-external'], fileName, callback, {
Expand Down
2 changes: 1 addition & 1 deletion core/instrument/test/extract-props-info.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as path from 'path';
import { getComponentProps } from '../src/index';
import { getComponentProps } from '../src/misc/props-info';
import { ComponentInfo } from '@component-controls/core';

export type ComponentCallback = (component: ComponentInfo) => void;
Expand Down
Loading

0 comments on commit 9152498

Please sign in to comment.