Skip to content

Commit

Permalink
feat(jest-core): Add newline after Json output (#13817)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessevanassen authored Jan 25, 2023
1 parent 9fa2a46 commit be021fa
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
8 changes: 7 additions & 1 deletion e2e/__tests__/jsonReporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions e2e/runJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -97,6 +98,7 @@ function spawnJest(
cwd: dir,
env,
reject: false,
stripFinalNewline: !options.keepTrailingNewline,
timeout: options.timeout || 0,
};

Expand Down
9 changes: 7 additions & 2 deletions packages/jest-core/src/runJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
);
}
}

Expand Down

1 comment on commit be021fa

@blackdevilb
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing

Please sign in to comment.