Skip to content

Commit

Permalink
feat: ability to store test files next to stories
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Apr 30, 2021
1 parent 5a68300 commit 644fb4d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
9 changes: 6 additions & 3 deletions core/instrument/src/babel/extract-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,15 @@ export const extractComponent = async (
};

/**
* instruemnt related metrics for the component
* instrument related metrics for the component
* @param store to retrieved stored packes info
* @param filePath current document file path
* @param component to be instrumented, will use component.request as the location
* @param options - instrumentaion options
*/
const componentRelatedMetrics = async (
store: LoadingDocStore,
filePath: string,
component: Component,
options?: InstrumentOptions,
) => {
Expand Down Expand Up @@ -198,7 +201,7 @@ const componentRelatedMetrics = async (
}
});
}
const testResults = await extractTests(testFiles, jest);
const testResults = await extractTests(filePath, testFiles, jest);
if (testResults) {
component.jest = testResults;
}
Expand Down Expand Up @@ -231,7 +234,7 @@ export const extractStoreComponent = async (
options,
);
if (component) {
await componentRelatedMetrics(store, component, options);
await componentRelatedMetrics(store, filePath, component, options);
const key = componentKey(
component.request ?? filePath,
componentName,
Expand Down
64 changes: 43 additions & 21 deletions core/instrument/src/misc/jest-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,64 @@ 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
* @param files key filePathName pair of all files to test
* @param options jest runCLI options
* @returns return key/value pairs with test results and coverage associated with each file
*/
export const extractTests = async (
documentPath: string,
files: string[],
options?: JestConfig,
): Promise<JestTests | undefined> => {
if (!files.length) {
return undefined;
}
const projectFolder = findJestConfig(files[0]);
const tests = files.reduce(
(
acc: {
testFiles: string[];
coverageFiles: string[];
snapshotFiles: string[];
},
file,
) => {
const testFiles = getRelatedTests(file);
acc.testFiles.push(...testFiles);
testFiles.forEach(f => {
const snapshotFile = resolveSnapshotFile(f);
if (fs.existsSync(snapshotFile)) {
acc.snapshotFiles.push(snapshotFile);
}
});
if (!acc.coverageFiles.includes(file)) {
acc.coverageFiles.push(file);
}
(acc: TestCumulators, file) => {
addCumulators(acc, file, true);
return acc;
},
{ testFiles: [], coverageFiles: [], snapshotFiles: [] },
addCumulators(
{
testFiles: [],
coverageFiles: [],
snapshotFiles: [],
},
documentPath,
false,
),
);
if (tests.testFiles.length) {
const cached = new CachedFileResource<JestTests>('jest-tests', files[0], [
Expand All @@ -59,6 +80,7 @@ export const extractTests = async (
if (cachedResults) {
return cachedResults;
}
const projectFolder = findJestConfig(tests.testFiles[0]);
const testResults = await runProjectTests({
testFiles: tests.testFiles.map(
f => `.${path.sep}${path.relative(projectFolder, f)}`,
Expand Down

0 comments on commit 644fb4d

Please sign in to comment.