Skip to content

Commit

Permalink
feat: configurable document test and coverage files
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed May 9, 2021
1 parent 709f1c9 commit 3bae432
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 94 deletions.
14 changes: 14 additions & 0 deletions core/core/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,20 @@ export type Document<Props = any> = {
*/
package?: string;

/**
* optional specify which test files are asociated with the document
*/
testFiles?: string[];

/**
* optional specify which files to use for test coverage
*/
testCoverage?: string[];

/**
* optional test data file
*/
testData?: string;
/**
* lookup into the global store.components
* since multiple components of the same name can be used
Expand Down
25 changes: 22 additions & 3 deletions core/instrument/src/babel/extract-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ImportTypes,
isLocalImport,
} from '@component-controls/core';
import { getRelatedTests } from '@component-controls/jest-extract';
import { componentKey } from '../misc/hashStore';
import { followImports } from './follow-imports';
import { analyze_components } from './analyze-component';
Expand Down Expand Up @@ -186,7 +187,16 @@ const componentRelatedMetrics = async (
}
if (jest !== false && component.request) {
const componentFolder = path.dirname(component.request);
const testFiles: string[] = [component.request];
const docTestFiles = store.doc?.testFiles;
const testFiles: string[] = docTestFiles || [
...getRelatedTests(component.request),
];
const docCoverageFiles = store.doc?.testCoverage;
const coverageFiles: string[] = docCoverageFiles
? docCoverageFiles
.map(f => path.resolve(filePath, f))
.filter(f => fs.existsSync(f))
: [];
//add local dependecnies from same folder to include in coverage.
if (component.localDependencies) {
Object.keys(component.localDependencies)
Expand All @@ -197,11 +207,20 @@ const componentRelatedMetrics = async (
basedir: componentFolder,
});
if (fs.existsSync(fileName)) {
testFiles.push(fileName);
if (!docTestFiles) {
testFiles.push(fileName);
}
if (!docCoverageFiles) {
coverageFiles.push(fileName);
}
}
});
}
const testResults = await extractTests(filePath, testFiles, jest);
const testResults = await extractTests(
Array.from(new Set(testFiles)),
Array.from(new Set(coverageFiles)),
jest,
);
if (testResults) {
component.jest = testResults;
}
Expand Down
114 changes: 35 additions & 79 deletions core/instrument/src/misc/jest-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,12 @@ import {
JestConfig,
runProjectTests,
findJestConfig,
getRelatedTests,
} from '@component-controls/jest-extract';
import { log } from '@component-controls/logger';
import { resolveSnapshotFile } from '@component-controls/core/node-utils';
import { JestTests } from '@component-controls/core';
import { CachedFileResource } from './chached-file';

type TestCumulators = {
testFiles: string[];
coverageFiles: string[];
snapshotFiles: string[];
};

const addCumulators = (
acc: TestCumulators,
filePath: string,
inCoverage: boolean,
) => {
const testFiles = getRelatedTests(filePath);
testFiles.forEach(f => {
if (!acc.testFiles.includes(f)) {
acc.testFiles.push(f);
}
const snapshotFile = resolveSnapshotFile(f);
if (
!acc.snapshotFiles.includes(snapshotFile) &&
fs.existsSync(snapshotFile)
) {
acc.snapshotFiles.push(snapshotFile);
}
});
if (inCoverage && !acc.coverageFiles.includes(filePath)) {
acc.coverageFiles.push(filePath);
}
return acc;
};
/**
* Separates the files into projects and runs jest tests
* @param documentPath full path to the document/story
Expand All @@ -48,60 +18,46 @@ const addCumulators = (
* @returns return key/value pairs with test results and coverage associated with each file
*/
export const extractTests = async (
documentPath: string,
files: string[],
testFiles: string[],
coverageFiles: string[],
options?: JestConfig,
): Promise<JestTests | undefined> => {
if (!files.length) {
if (!testFiles.length) {
return undefined;
}
const tests = files.reduce(
(acc: TestCumulators, file) => {
addCumulators(acc, file, true);
return acc;
},
addCumulators(
{
testFiles: [],
coverageFiles: [],
snapshotFiles: [],
},
documentPath,
false,
const snapshotFiles = testFiles
.map(f => {
const resolved = resolveSnapshotFile(f);
if (fs.existsSync(resolved)) {
return resolved;
}
return '';
})
.filter(f => f);
const cached = new CachedFileResource<JestTests>('jest-tests', testFiles[0], [
...testFiles,
...coverageFiles,
...snapshotFiles,
]);
const cachedResults = cached.get();
if (cachedResults) {
return cachedResults;
}
const projectFolder = findJestConfig(testFiles[0]);
log('jest tests', testFiles[0], [229, 232, 211]);
const testResults = await runProjectTests({
testFiles: testFiles.map(
f => `.${path.sep}${path.relative(projectFolder, f)}`,
),
);
if (tests.testFiles.length) {
if (options?.color) {
console.log('tests.testFiles', tests.testFiles);
console.log('tests.coverageFiles', tests.coverageFiles);
}
const cached = new CachedFileResource<JestTests>('jest-tests', files[0], [
...tests.testFiles,
...tests.coverageFiles,
...tests.snapshotFiles,
...files,
]);
const cachedResults = cached.get();
if (cachedResults) {
return cachedResults;
}
const projectFolder = findJestConfig(tests.testFiles[0]);
log('jest tests', tests.testFiles[0], [229, 232, 211]);
const testResults = await runProjectTests({
testFiles: tests.testFiles.map(
projectFolder,
relativeFolder: path.dirname(testFiles[0]),
options: {
...options,
collectCoverageOnlyFrom: coverageFiles.map(
f => `.${path.sep}${path.relative(projectFolder, f)}`,
),
projectFolder,
relativeFolder: path.dirname(files[0]),
options: {
...options,
collectCoverageOnlyFrom: tests.coverageFiles.map(
f => `.${path.sep}${path.relative(projectFolder, f)}`,
),
},
});
cached.set(testResults);
return testResults;
}
return undefined;
},
});
cached.set(testResults);
return testResults;
};
30 changes: 18 additions & 12 deletions core/instrument/test/jest-tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ describe('component-tests', () => {
});
it('Table', async () => {
const tests = await extractTests(
path.resolve(
__dirname,
'../../../ui/components/src/Table/Table.stories.tsx',
),
[
path.resolve(
__dirname,
'../../../ui/components/src/Table/Table.test.ts',
),
],
[
path.resolve(__dirname, '../../../ui/components/src/Table/Table.tsx'),
path.resolve(
Expand Down Expand Up @@ -400,10 +402,12 @@ describe('component-tests', () => {

it('Catalog', async () => {
const tests = await extractTests(
path.resolve(
__dirname,
'../../../plugins/addon-catalog/src/Catalog/Catalog.stories.tsx',
),
[
path.resolve(
__dirname,
'../../../plugins/addon-catalog/src/Catalog/Catalog.test.ts',
),
],
[
path.resolve(
__dirname,
Expand Down Expand Up @@ -468,10 +472,12 @@ describe('component-tests', () => {
}, 100000);
it('Header', async () => {
const tests = await extractTests(
path.resolve(
__dirname,
'../../../ui/components/src/Header/Header.stories.tsx',
),
[
path.resolve(
__dirname,
'../../../ui/components/src/Header/Header.test.ts',
),
],
[path.resolve(__dirname, '../../../ui/components/src/Header/Header.tsx')],
);
expect(tests).toMatchObject({
Expand Down

0 comments on commit 3bae432

Please sign in to comment.