-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4991b5b
commit e6daeec
Showing
16 changed files
with
226 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export interface JestTestResults { | ||
ancestorTitles: string[]; | ||
failureDetails: unknown[]; | ||
failureMessages: string[]; | ||
numPassingAsserts: number; | ||
status: 'passed' | 'failed' | 'skipped' | 'pending' | 'todo' | 'disabled'; | ||
title: string; | ||
} | ||
|
||
export interface CoverageMetrics { | ||
total: number; | ||
covered: number; | ||
skipped: number; | ||
pct: number; | ||
} | ||
|
||
export interface JestCoverage { | ||
lines: CoverageMetrics; | ||
functions: CoverageMetrics; | ||
statements: CoverageMetrics; | ||
branches: CoverageMetrics; | ||
} | ||
|
||
export interface JestTestFile { | ||
testFileName: string; | ||
testResults: JestTestResults[]; | ||
coverage: Record<string, JestCoverage>; | ||
} | ||
|
||
export type JestTests = JestTestFile[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import { createHash } from 'crypto'; | ||
import { JestTests } from '@component-controls/core'; | ||
import { JestConfig } from '@component-controls/jest-extract'; | ||
import { | ||
JestResults, | ||
runTests, | ||
getRelatedTests, | ||
} from '@component-controls/jest-extract'; | ||
import { CachedFileResource } from './chached-file'; | ||
|
||
export const extractJestTests = async ( | ||
filePath: string, | ||
options?: JestConfig, | ||
): Promise<JestTests | undefined> => { | ||
const testFiles = getRelatedTests(filePath); | ||
const dateModified = fs.statSync(filePath).mtime; | ||
|
||
const fileDir = path.dirname(filePath); | ||
if (testFiles.length) { | ||
const results: JestTests = []; | ||
for (const testFile of testFiles) { | ||
const fileHash = createHash('md5') | ||
.update(dateModified.toString()) | ||
.update(filePath) | ||
.digest('hex'); | ||
|
||
const cached = new CachedFileResource<JestResults>( | ||
'jest-tests', | ||
`${testFile}-${fileHash}`, | ||
testFile, | ||
); | ||
const cachedTest = cached.get(); | ||
if (cachedTest) { | ||
results.push({ | ||
testFileName: path.relative(fileDir, testFile), | ||
testResults: cachedTest.testResults, | ||
coverage: cachedTest.coverage, | ||
}); | ||
} | ||
const rootDir = path.relative( | ||
path.resolve(path.dirname(testFile), 'config'), | ||
fileDir, | ||
); | ||
const result = await runTests(testFile, { rootDir, ...options }); | ||
if (result) { | ||
cached.set(result); | ||
results.push({ | ||
testFileName: path.relative(fileDir, testFile), | ||
testResults: result.testResults, | ||
coverage: result.coverage, | ||
}); | ||
} | ||
} | ||
return results.length ? results : undefined; | ||
} | ||
return undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import path from 'path'; | ||
import { extractJestTests } from '../src/misc/jest-tests'; | ||
|
||
describe('component-tests', () => { | ||
it('', async () => { | ||
const tests = await extractJestTests( | ||
path.resolve(__dirname, '../../../ui/components/src/Header/Header.tsx'), | ||
); | ||
expect(tests).toMatchObject({ | ||
testFileName: 'Header.test.ts', | ||
testResults: [ | ||
{ | ||
ancestorTitles: ['Header'], | ||
failureDetails: [], | ||
failureMessages: [], | ||
numPassingAsserts: 0, | ||
status: 'passed', | ||
title: 'overview', | ||
}, | ||
], | ||
coverage: { | ||
'Header.stories.tsx': { | ||
lines: { | ||
total: 4, | ||
covered: 4, | ||
skipped: 0, | ||
pct: 100, | ||
}, | ||
functions: { | ||
total: 1, | ||
covered: 1, | ||
skipped: 0, | ||
pct: 100, | ||
}, | ||
statements: { | ||
total: 6, | ||
covered: 6, | ||
skipped: 0, | ||
pct: 100, | ||
}, | ||
branches: { | ||
total: 0, | ||
covered: 0, | ||
skipped: 0, | ||
pct: 100, | ||
}, | ||
}, | ||
'Header.tsx': { | ||
lines: { | ||
total: 2, | ||
covered: 2, | ||
skipped: 0, | ||
pct: 100, | ||
}, | ||
functions: { | ||
total: 1, | ||
covered: 1, | ||
skipped: 0, | ||
pct: 100, | ||
}, | ||
statements: { | ||
total: 6, | ||
covered: 6, | ||
skipped: 0, | ||
pct: 100, | ||
}, | ||
branches: { | ||
total: 0, | ||
covered: 0, | ||
skipped: 0, | ||
pct: 100, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}, 100000); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.