chore: ensure the file operations are async in the json reporter. (#24356)

This commit is contained in:
Marcin Strzyz 2023-07-23 00:26:32 -07:00 committed by GitHub
parent f453a17ede
commit 2070861807
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -55,7 +55,7 @@ class JSONReporter extends EmptyReporter {
}
override async onEnd(result: FullResult) {
outputReport(this._serializeReport(), this.config, this._outputFile);
await outputReport(this._serializeReport(), this.config, this._outputFile);
}
private _serializeReport(): JSONReport {
@ -222,13 +222,13 @@ class JSONReporter extends EmptyReporter {
}
}
function outputReport(report: JSONReport, config: FullConfig, outputFile: string | undefined) {
async function outputReport(report: JSONReport, config: FullConfig, outputFile: string | undefined) {
const reportString = JSON.stringify(report, undefined, 2);
if (outputFile) {
assert(config.configFile || path.isAbsolute(outputFile), 'Expected fully resolved path if not using config file.');
outputFile = config.configFile ? path.resolve(path.dirname(config.configFile), outputFile) : outputFile;
fs.mkdirSync(path.dirname(outputFile), { recursive: true });
fs.writeFileSync(outputFile, reportString);
await fs.promises.mkdir(path.dirname(outputFile), { recursive: true });
await fs.promises.writeFile(outputFile, reportString);
} else {
console.log(reportString);
}