-
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
c236316
commit 1106e89
Showing
21 changed files
with
206 additions
and
83 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
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,12 @@ | ||
module.exports = { | ||
"preset": "ts-jest", | ||
"transform": { | ||
"^.+\\.(ts|tsx)?$": "ts-jest", | ||
"^.+\\.(js|jsx)$": "babel-jest" | ||
}, | ||
"collectCoverageFrom": [ | ||
"**/*.{js,jsx,tsx,ts}", | ||
"!**/jest.config.js" | ||
] | ||
} | ||
|
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,8 @@ | ||
module.exports = { | ||
"preset": "ts-jest", | ||
"transform": { | ||
"^.+\\.(ts|tsx)?$": "ts-jest", | ||
"^.+\\.(js|jsx)$": "babel-jest" | ||
} | ||
} | ||
|
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,8 @@ | ||
module.exports = { | ||
"preset": "ts-jest", | ||
"transform": { | ||
"^.+\\.(ts|tsx)?$": "ts-jest", | ||
"^.+\\.(js|jsx)$": "babel-jest" | ||
} | ||
} | ||
|
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,8 @@ | ||
module.exports = { | ||
"preset": "ts-jest", | ||
"transform": { | ||
"^.+\\.(ts|tsx)?$": "ts-jest", | ||
"^.+\\.(js|jsx)$": "babel-jest" | ||
} | ||
} | ||
|
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,12 @@ | ||
module.exports = { | ||
"preset": "ts-jest", | ||
"transform": { | ||
"^.+\\.(ts|tsx)?$": "ts-jest", | ||
"^.+\\.(js|jsx)$": "babel-jest" | ||
}, | ||
"collectCoverageFrom": [ | ||
"**/*.{js,jsx,tsx,ts}", | ||
"!**/jest.config.js" | ||
] | ||
} | ||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
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,96 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { runCLI } from 'jest'; | ||
import { Config } from '@jest/types'; | ||
import { AssertionResult, AggregatedResult } from '@jest/test-result'; | ||
import { FileCoverage, CoverageSummary } from 'istanbul-lib-coverage'; | ||
|
||
export const getUniqueFolder = (folder: string): string => { | ||
let fnId = 1; | ||
const fName = () => path.resolve(folder, `__jest-${fnId}`); | ||
while (fs.existsSync(fName())) { | ||
fnId += 1; | ||
} | ||
const folderName = fName(); | ||
fs.mkdirSync(folderName); | ||
return folderName; | ||
}; | ||
export interface JestResults { | ||
/** | ||
* test results | ||
*/ | ||
testResults: AssertionResult[]; | ||
/** | ||
* coverage summary data, by file | ||
*/ | ||
coverage: Record<string, CoverageSummary>; | ||
} | ||
export const run = async ( | ||
testFilePath: string, | ||
jestConfig?: Partial<Config.Argv>, | ||
): Promise<JestResults | undefined> => { | ||
const testFolder = path.dirname(testFilePath); | ||
const testFile = path.basename(testFilePath); | ||
const configFolder = getUniqueFolder(testFolder); | ||
const configFile = path.resolve(configFolder, 'jest.config.js'); | ||
fs.writeFileSync( | ||
configFile, | ||
`module.exports = ${JSON.stringify( | ||
{ | ||
rootDir: '..', | ||
preset: 'ts-jest', | ||
transform: { | ||
'^.+\\.(ts|tsx)?$': 'ts-jest', | ||
'^.+\\.(js|jsx)$': 'babel-jest', | ||
}, | ||
collectCoverageFrom: [ | ||
'**/*.{js,jsx,tsx,ts}', | ||
'!**/jest.config.js', | ||
'!**/*.{test,spec}.{js,ts}', | ||
], | ||
}, | ||
null, | ||
2, | ||
)} | ||
`, | ||
'utf8', | ||
); | ||
let runResults: { | ||
results: AggregatedResult; | ||
globalConfig: Config.GlobalConfig; | ||
}; | ||
try { | ||
runResults = await runCLI( | ||
{ | ||
$0: testFile, | ||
silent: true, | ||
verbose: false, | ||
reporters: [], | ||
coverage: true, | ||
coverageReporters: ['none'], | ||
watchman: false, | ||
...jestConfig, | ||
} as Config.Argv, | ||
[configFolder], | ||
); | ||
} catch (err) { | ||
console.error(err); | ||
return undefined; | ||
} finally { | ||
fs.unlinkSync(configFile); | ||
fs.rmdirSync(configFolder, { recursive: true }); | ||
} | ||
const cov = runResults.results.coverageMap; | ||
const result: JestResults = { | ||
testResults: runResults.results.testResults[0].testResults, | ||
coverage: {}, | ||
}; | ||
if (cov) { | ||
Object.keys(cov.data).forEach(file => { | ||
const fc = cov.data[file] as FileCoverage; | ||
result.coverage[path.relative(testFolder, file)] = fc.toSummary(); | ||
}); | ||
} | ||
|
||
return result; | ||
}; |
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,57 @@ | ||
import path from 'path'; | ||
import { run } from '../src'; | ||
|
||
type Await<T> = T extends PromiseLike<infer U> ? U : T; | ||
|
||
let results: Await<ReturnType<typeof run>>; | ||
beforeAll(async () => { | ||
results = await run(path.resolve(__dirname, '../.fixtures/sum.test.js')); | ||
}, 50000); | ||
|
||
describe('small test', () => { | ||
it('testResults ', () => { | ||
expect(results?.testResults[0]).toMatchObject({ | ||
ancestorTitles: ['testing sum'], | ||
failureDetails: [], | ||
failureMessages: [], | ||
fullName: 'testing sum sum', | ||
location: null, | ||
numPassingAsserts: 0, | ||
status: 'passed', | ||
title: 'sum', | ||
}); | ||
}); | ||
|
||
it('coverage ', () => { | ||
expect(results?.coverage).toMatchObject({ | ||
'sum.js': { | ||
data: { | ||
lines: { | ||
total: 1, | ||
covered: 1, | ||
skipped: 0, | ||
pct: 100, | ||
}, | ||
functions: { | ||
total: 1, | ||
covered: 1, | ||
skipped: 0, | ||
pct: 100, | ||
}, | ||
statements: { | ||
total: 2, | ||
covered: 2, | ||
skipped: 0, | ||
pct: 100, | ||
}, | ||
branches: { | ||
total: 0, | ||
covered: 0, | ||
skipped: 0, | ||
pct: 100, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); |
File renamed without changes.