From be021fa8b722f1d367833e8f48bbb08e6e3dd687 Mon Sep 17 00:00:00 2001 From: Jesse van Assen Date: Wed, 25 Jan 2023 21:35:54 +0100 Subject: [PATCH] feat(jest-core): Add newline after Json output (#13817) --- CHANGELOG.md | 2 ++ e2e/__tests__/jsonReporter.test.ts | 8 +++++++- e2e/runJest.ts | 2 ++ packages/jest-core/src/runJest.ts | 9 +++++++-- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d874dbd4acee..ca3b22b37435 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +- `[jest-core]` Add newlines to JSON output ([#13817](https://github.com/facebook/jest/pull/13817)) + ### Fixes - `[@jest/expect-utils]` `toMatchObject` diffs should include `Symbol` properties ([#13810](https://github.com/facebook/jest/pull/13810)) diff --git a/e2e/__tests__/jsonReporter.test.ts b/e2e/__tests__/jsonReporter.test.ts index 93132ac5e8a7..df2dba4830a1 100644 --- a/e2e/__tests__/jsonReporter.test.ts +++ b/e2e/__tests__/jsonReporter.test.ts @@ -28,6 +28,8 @@ describe('JSON Reporter', () => { runJest('json-reporter', ['--json', `--outputFile=${outputFileName}`]); const testOutput = fs.readFileSync(outputFilePath, 'utf8'); + expect(testOutput.endsWith('\n')).toBe(true); + try { jsonResult = JSON.parse(testOutput); } catch (err: any) { @@ -71,12 +73,16 @@ describe('JSON Reporter', () => { }); it('outputs coverage report', () => { - const result = runJest('json-reporter', ['--json']); + const result = runJest('json-reporter', ['--json'], { + keepTrailingNewline: true, + }); let jsonResult: FormattedTestResults; expect(result.stderr).toMatch(/1 failed, 1 skipped, 2 passed/); expect(result.exitCode).toBe(1); + expect(result.stdout.endsWith('\n')).toBe(true); + try { jsonResult = JSON.parse(result.stdout); } catch (err: any) { diff --git a/e2e/runJest.ts b/e2e/runJest.ts index b5680c6c0d97..aefa390bc93e 100644 --- a/e2e/runJest.ts +++ b/e2e/runJest.ts @@ -20,6 +20,7 @@ import {normalizeIcons} from './Utils'; const JEST_PATH = path.resolve(__dirname, '../packages/jest-cli/bin/jest.js'); type RunJestOptions = { + keepTrailingNewline?: boolean; // keep final newline in output from stdout and stderr nodeOptions?: string; nodePath?: string; skipPkgJsonCheck?: boolean; // don't complain if can't find package.json @@ -97,6 +98,7 @@ function spawnJest( cwd: dir, env, reject: false, + stripFinalNewline: !options.keepTrailingNewline, timeout: options.timeout || 0, }; diff --git a/packages/jest-core/src/runJest.ts b/packages/jest-core/src/runJest.ts index 5f0d74cb25d1..9aa77a4346ec 100644 --- a/packages/jest-core/src/runJest.ts +++ b/packages/jest-core/src/runJest.ts @@ -106,12 +106,17 @@ const processResults = async ( const cwd = tryRealpath(process.cwd()); const filePath = path.resolve(cwd, outputFile); - fs.writeFileSync(filePath, JSON.stringify(formatTestResults(runResults))); + fs.writeFileSync( + filePath, + `${JSON.stringify(formatTestResults(runResults))}\n`, + ); outputStream.write( `Test results written to: ${path.relative(cwd, filePath)}\n`, ); } else { - process.stdout.write(JSON.stringify(formatTestResults(runResults))); + process.stdout.write( + `${JSON.stringify(formatTestResults(runResults))}\n`, + ); } }