Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit be021fa

Browse files
feat(jest-core): Add newline after Json output (#13817)
1 parent 9fa2a46 commit be021fa

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Features
44

5+
- `[jest-core]` Add newlines to JSON output ([#13817](https://github.com/facebook/jest/pull/13817))
6+
57
### Fixes
68

79
- `[@jest/expect-utils]` `toMatchObject` diffs should include `Symbol` properties ([#13810](https://github.com/facebook/jest/pull/13810))

e2e/__tests__/jsonReporter.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ describe('JSON Reporter', () => {
2828
runJest('json-reporter', ['--json', `--outputFile=${outputFileName}`]);
2929
const testOutput = fs.readFileSync(outputFilePath, 'utf8');
3030

31+
expect(testOutput.endsWith('\n')).toBe(true);
32+
3133
try {
3234
jsonResult = JSON.parse(testOutput);
3335
} catch (err: any) {
@@ -71,12 +73,16 @@ describe('JSON Reporter', () => {
7173
});
7274

7375
it('outputs coverage report', () => {
74-
const result = runJest('json-reporter', ['--json']);
76+
const result = runJest('json-reporter', ['--json'], {
77+
keepTrailingNewline: true,
78+
});
7579
let jsonResult: FormattedTestResults;
7680

7781
expect(result.stderr).toMatch(/1 failed, 1 skipped, 2 passed/);
7882
expect(result.exitCode).toBe(1);
7983

84+
expect(result.stdout.endsWith('\n')).toBe(true);
85+
8086
try {
8187
jsonResult = JSON.parse(result.stdout);
8288
} catch (err: any) {

e2e/runJest.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {normalizeIcons} from './Utils';
2020
const JEST_PATH = path.resolve(__dirname, '../packages/jest-cli/bin/jest.js');
2121

2222
type RunJestOptions = {
23+
keepTrailingNewline?: boolean; // keep final newline in output from stdout and stderr
2324
nodeOptions?: string;
2425
nodePath?: string;
2526
skipPkgJsonCheck?: boolean; // don't complain if can't find package.json
@@ -97,6 +98,7 @@ function spawnJest(
9798
cwd: dir,
9899
env,
99100
reject: false,
101+
stripFinalNewline: !options.keepTrailingNewline,
100102
timeout: options.timeout || 0,
101103
};
102104

packages/jest-core/src/runJest.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,17 @@ const processResults = async (
106106
const cwd = tryRealpath(process.cwd());
107107
const filePath = path.resolve(cwd, outputFile);
108108

109-
fs.writeFileSync(filePath, JSON.stringify(formatTestResults(runResults)));
109+
fs.writeFileSync(
110+
filePath,
111+
`${JSON.stringify(formatTestResults(runResults))}\n`,
112+
);
110113
outputStream.write(
111114
`Test results written to: ${path.relative(cwd, filePath)}\n`,
112115
);
113116
} else {
114-
process.stdout.write(JSON.stringify(formatTestResults(runResults)));
117+
process.stdout.write(
118+
`${JSON.stringify(formatTestResults(runResults))}\n`,
119+
);
115120
}
116121
}
117122

0 commit comments

Comments
 (0)