-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support JSON line output. #8242
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ import path from 'path'; | |
import chalk from 'chalk'; | ||
import {sync as realpath} from 'realpath-native'; | ||
import {CustomConsole} from '@jest/console'; | ||
import {formatTestResults} from 'jest-util'; | ||
import exit from 'exit'; | ||
import fs from 'graceful-fs'; | ||
import {JestHook, JestHookEmitter} from 'jest-watcher'; | ||
|
@@ -19,6 +18,8 @@ import {Config} from '@jest/types'; | |
import { | ||
AggregatedResult, | ||
makeEmptyAggregatedTestResult, | ||
formatTestResult, | ||
formatTestResults, | ||
} from '@jest/test-result'; | ||
import {ChangedFiles, ChangedFilesPromise} from 'jest-changed-files'; | ||
import getNoTestsFoundMessage from './getNoTestsFoundMessage'; | ||
|
@@ -69,20 +70,40 @@ const getTestPaths = async ( | |
|
||
type ProcessResultOptions = Pick< | ||
Config.GlobalConfig, | ||
'json' | 'outputFile' | 'testResultsProcessor' | ||
'json' | 'jsonLines' | 'outputFile' | 'testResultsProcessor' | ||
> & { | ||
collectHandles?: () => Array<Error>; | ||
onComplete?: (result: AggregatedResult) => void; | ||
outputStream: NodeJS.WritableStream; | ||
}; | ||
|
||
const processResults = ( | ||
const stdoutWriteBuffered = (data: string) => | ||
new Promise<void>(resolve => { | ||
if (!process.stdout.write(data)) { | ||
process.stdout.once('drain', resolve); | ||
} else { | ||
process.nextTick(resolve); | ||
} | ||
}); | ||
|
||
const getOutputFilePaths = (outputFile: string) => { | ||
const cwd = realpath(process.cwd()); | ||
const absoluteOutputFile = path.resolve(cwd, outputFile); | ||
const relativeOutputFile = path.relative(cwd, absoluteOutputFile); | ||
return { | ||
absoluteOutputFile, | ||
relativeOutputFile, | ||
}; | ||
}; | ||
|
||
const processResults = async ( | ||
runResults: AggregatedResult, | ||
options: ProcessResultOptions, | ||
) => { | ||
const { | ||
outputFile, | ||
json: isJSON, | ||
jsonLines: isJSONLines, | ||
onComplete, | ||
outputStream, | ||
testResultsProcessor, | ||
|
@@ -98,14 +119,73 @@ const processResults = ( | |
if (testResultsProcessor) { | ||
runResults = require(testResultsProcessor)(runResults); | ||
} | ||
if (isJSON) { | ||
if (isJSONLines) { | ||
let writeLine: (line: string) => Promise<void>; | ||
let onWriteFinished: (() => void) | undefined = undefined; | ||
if (outputFile) { | ||
const cwd = realpath(process.cwd()); | ||
const filePath = path.resolve(cwd, outputFile); | ||
const {absoluteOutputFile, relativeOutputFile} = getOutputFilePaths( | ||
outputFile, | ||
); | ||
fs.writeFileSync(absoluteOutputFile, ''); | ||
writeLine = line => | ||
new Promise<void>((resolve, reject) => | ||
fs.appendFile(absoluteOutputFile, line + '\n', {}, err => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(); | ||
}), | ||
); | ||
onWriteFinished = () => { | ||
outputStream.write( | ||
`Test results written as JSON lines to: ${relativeOutputFile}\n`, | ||
); | ||
}; | ||
} else { | ||
writeLine = line => stdoutWriteBuffered(line + '\n'); | ||
} | ||
|
||
fs.writeFileSync(filePath, JSON.stringify(formatTestResults(runResults))); | ||
await writeLine( | ||
JSON.stringify({ | ||
runResults: { | ||
...runResults, | ||
coverageMap: undefined, | ||
testResults: undefined, | ||
}, | ||
}), | ||
); | ||
for (const testResult of runResults.testResults) { | ||
await writeLine( | ||
JSON.stringify({testResults: formatTestResult(testResult)}), | ||
); | ||
} | ||
if (runResults.coverageMap) { | ||
const coverageMap: {[key: string]: unknown} = | ||
typeof runResults.coverageMap.toJSON === 'function' | ||
? runResults.coverageMap.toJSON() | ||
: runResults.coverageMap; | ||
const coverageMapFiles = Object.keys(coverageMap); | ||
for (const coverageMapFile of coverageMapFiles) { | ||
await writeLine( | ||
JSON.stringify({coverageMap: coverageMap[coverageMapFile]}), | ||
); | ||
} | ||
} | ||
if (onWriteFinished) { | ||
onWriteFinished(); | ||
} | ||
} else if (isJSON) { | ||
if (outputFile) { | ||
const {absoluteOutputFile, relativeOutputFile} = getOutputFilePaths( | ||
outputFile, | ||
); | ||
fs.writeFileSync( | ||
absoluteOutputFile, | ||
JSON.stringify(formatTestResults(runResults)), | ||
); | ||
outputStream.write( | ||
`Test results written to: ${path.relative(cwd, filePath)}\n`, | ||
`Test results written as JSON to: ${relativeOutputFile}\n`, | ||
); | ||
} else { | ||
process.stdout.write(JSON.stringify(formatTestResults(runResults))); | ||
|
@@ -183,7 +263,11 @@ export default (async function runJest({ | |
|
||
if (globalConfig.listTests) { | ||
const testsPaths = Array.from(new Set(allTests.map(test => test.path))); | ||
if (globalConfig.json) { | ||
if (globalConfig.jsonLines) { | ||
for (const testPath of testsPaths) { | ||
console.log(JSON.stringify({testPath})); | ||
} | ||
} else if (globalConfig.json) { | ||
console.log(JSON.stringify(testsPaths)); | ||
} else { | ||
console.log(testsPaths.join('\n')); | ||
|
@@ -253,9 +337,10 @@ export default (async function runJest({ | |
await runGlobalHook({allTests, globalConfig, moduleName: 'globalTeardown'}); | ||
} | ||
|
||
return processResults(results, { | ||
return await processResults(results, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's now async, since for JSON lines it'll now wait on stdout for buffering output. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole function is async already, |
||
collectHandles, | ||
json: globalConfig.json, | ||
jsonLines: globalConfig.jsonLines, | ||
onComplete, | ||
outputFile: globalConfig.outputFile, | ||
outputStream, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,15 +5,11 @@ | |
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {AggregatedResult} from '@jest/test-result'; | ||
|
||
type CoverageMap = AggregatedResult['coverageMap']; | ||
|
||
function summarize(coverageMap: CoverageMap): CoverageMap { | ||
if (!coverageMap) { | ||
return coverageMap; | ||
} | ||
import {AggregatedResult, CoverageMap} from '@jest/test-result'; | ||
|
||
function summarize( | ||
coverageMap: CoverageMap, | ||
): {[key: string]: {[key: string]: unknown}} { | ||
const summaries = Object.create(null); | ||
|
||
coverageMap.files().forEach(file => { | ||
|
@@ -32,12 +28,20 @@ function summarize(coverageMap: CoverageMap): CoverageMap { | |
} | ||
} | ||
|
||
summaries[file] = covered.join(''); | ||
summaries[file] = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a breaking change to Technically, before it was breaking the contract provided by TypeScript by just returning an |
||
path: file, | ||
}; | ||
}); | ||
|
||
return summaries; | ||
} | ||
|
||
export = function(results: AggregatedResult): AggregatedResult { | ||
return {...results, coverageMap: summarize(results.coverageMap)}; | ||
return { | ||
...results, | ||
coverageMap: | ||
results.coverageMap && typeof results.coverageMap.toJSON === 'function' | ||
? summarize(results.coverageMap as CoverageMap) | ||
: results.coverageMap, | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why weren't these being imported from
@jest/test-result
before?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missed it when refactoring. Will be good to trim out exports from jest-util for 25