From 3bae4322ba16f2d86d4a82a980401148646a91a6 Mon Sep 17 00:00:00 2001 From: atanasster Date: Sun, 9 May 2021 18:54:20 +0300 Subject: [PATCH] feat: configurable document test and coverage files --- core/core/src/document.ts | 14 +++ .../instrument/src/babel/extract-component.ts | 25 +++- core/instrument/src/misc/jest-tests.ts | 114 ++++++------------ core/instrument/test/jest-tests.test.ts | 30 +++-- 4 files changed, 89 insertions(+), 94 deletions(-) diff --git a/core/core/src/document.ts b/core/core/src/document.ts index ebd726d7f..bdb1a4e5d 100644 --- a/core/core/src/document.ts +++ b/core/core/src/document.ts @@ -251,6 +251,20 @@ export type Document = { */ 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 diff --git a/core/instrument/src/babel/extract-component.ts b/core/instrument/src/babel/extract-component.ts index 14852b987..b8b2e6ba4 100644 --- a/core/instrument/src/babel/extract-component.ts +++ b/core/instrument/src/babel/extract-component.ts @@ -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'; @@ -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) @@ -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; } diff --git a/core/instrument/src/misc/jest-tests.ts b/core/instrument/src/misc/jest-tests.ts index 90815ac9e..9dd16165f 100644 --- a/core/instrument/src/misc/jest-tests.ts +++ b/core/instrument/src/misc/jest-tests.ts @@ -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 @@ -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 => { - 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('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('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; }; diff --git a/core/instrument/test/jest-tests.test.ts b/core/instrument/test/jest-tests.test.ts index 2737ba8fb..24ee43248 100644 --- a/core/instrument/test/jest-tests.test.ts +++ b/core/instrument/test/jest-tests.test.ts @@ -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( @@ -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, @@ -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({