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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Add \n to the results when running with --json #13816

Closed
jessevanassen opened this issue Jan 25, 2023 · 3 comments 路 Fixed by #13817
Closed

[Feature]: Add \n to the results when running with --json #13816

jessevanassen opened this issue Jan 25, 2023 · 3 comments 路 Fixed by #13817

Comments

@jessevanassen
Copy link
Contributor

馃殌 Feature Proposal

Currently, when running Jest with the --json flag, the result will be a stringified Json object, without a trailing newline character.

When running with the --watch or --watchAll flag, Jest will produce an object for every test run. Because those individual results also don't end with a newline character, all those objects will be glued together on the same line.

My proposal is to add a newline character after every Json object, to make it easier to separate test runs.

Motivation

The implication of the current behavior is that it makes it a lot harder to build external integrations that use the output of jest --json --watchAll.
If every test run would result in output ending with a newline, you can parse every line of the stdout as an individual Json test result.
Without newline separators, it becomes a lot harder to separate the test runs in the output, and will in most cases require a custom parse step to find the end of the object.

It also means that the output of jest --json doesn't conform to the Unix file specification, which dictates that all lines in a text file should end with a newline character.

Example

reporter.py:

#!/usr/bin/env python3

import sys
import json

for line in sys.stdin:
    parsed = json.loads(line)
    print(f"{ parsed['numPassedTests'] }/{ parsed['numTotalTests'] } tests passed")
npx jest --json --watchAll 2>/dev/null | python3 ./reporter.py

If the test outputs would end with a newline, the Python file would print for every test run.

Currently, the python file won't print anything, because all run results are appended to the same line.
When the Jest process is terminated, the Python script throws an error because the line cannot be parsed as valid Json (because it contains multiple Json documents).

Pitch

The code that prints the Json representation is in the jest-core package.

@SimenB
Copy link
Member

SimenB commented Jan 25, 2023

Happy to take a PR adding this 馃憤

@SimenB
Copy link
Member

SimenB commented Jan 26, 2023

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants