Skip to content
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

feat(jest-core): Add newline after Json output #13817

Merged
merged 4 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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