Skip to content

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

Closed

Description

🚀 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions