Jest JSON Reporter
Creating a custom Jest reporter allows for flexible and tailored test result reporting. This challenge asks you to build a Jest reporter that outputs test results in JSON format, which can be easily consumed by other tools or systems for analysis, integration, or visualization. This is particularly useful for CI/CD pipelines or when you need to process test results programmatically.
Problem Description
You need to create a Jest reporter that takes the test results from Jest and formats them into a JSON string. The JSON output should include information about each test file, the tests within each file, the status of each test (passed, failed, skipped), and any error messages associated with failed tests. The reporter should be configurable to allow users to specify an output file path.
Key Requirements:
- JSON Output: The reporter must produce a valid JSON string representing the test results.
- File-Level Information: The JSON should include the name of each test file.
- Test-Level Information: For each test file, the JSON should include an array of test objects. Each test object should contain:
name: The name of the test.status: "passed", "failed", or "skipped".errorMessage: (Only present ifstatusis "failed") The error message associated with the failed test.
- Output File: The reporter should be able to write the JSON output to a specified file path. If no file path is provided, the JSON should be printed to the console.
- Jest API Integration: The reporter must correctly utilize the Jest API to access test results during the test run.
Expected Behavior:
When Jest runs with your reporter enabled, it should:
- Collect test results as usual.
- Format these results into a JSON string according to the structure described above.
- Write the JSON string to the specified output file (if provided) or print it to the console.
Edge Cases to Consider:
- No Tests Run: Handle the case where no tests are run gracefully.
- Skipped Tests: Ensure skipped tests are correctly reported with a "skipped" status.
- Error Messages: Properly capture and format error messages for failed tests. Ensure error messages are strings.
- File Path Handling: Handle invalid or inaccessible file paths gracefully (e.g., log an error and potentially fall back to console output).
- Asynchronous Tests: Ensure the reporter correctly handles asynchronous tests and their results.
Examples
Example 1:
Input: Jest run with a single test file containing one passing test and one failing test, and the reporter configured to output to 'results.json'.
Output:
```json
{
"testFiles": [
{
"name": "example.test.ts",
"tests": [
{
"name": "should pass",
"status": "passed"
},
{
"name": "should fail",
"status": "failed",
"errorMessage": "Expected 1 to be 2."
}
]
}
]
}
Explanation: The JSON output represents the test results from 'example.test.ts'. One test passed, and one test failed with the specified error message.
Example 2:
Input: Jest run with two test files, one passing and one with a skipped test, and no output file specified.
Output:
```json
{
"testFiles": [
{
"name": "file1.test.ts",
"tests": [
{
"name": "test1",
"status": "passed"
}
]
},
{
"name": "file2.test.ts",
"tests": [
{
"name": "test2",
"status": "skipped"
}
]
}
]
}
Explanation: The JSON output represents the test results from two files. 'file1.test.ts' has one passing test, and 'file2.test.ts' has one skipped test. The output is printed to the console.
Constraints
- TypeScript: The solution must be written in TypeScript.
- Jest API: You must use the Jest API to access test results.
- JSON Validity: The output must be valid JSON.
- Performance: The reporter should not significantly impact the overall test run time. While not a primary concern, avoid unnecessary computations or I/O operations.
- File Size: The JSON output should be reasonably sized. Avoid including unnecessary data.
Notes
- Start by familiarizing yourself with the Jest reporter API: https://jestjs.io/docs/creating-a-custom-reporter
- The
JestContextobject provides access to test results and other relevant information. - Consider using a JSON library (e.g.,
JSON.stringify) to ensure valid JSON output. - Think about how to handle errors gracefully, especially when writing to a file.
- The
onTestResultandonRunCompletecallbacks are key to accessing test results. - The reporter should be configurable via command-line arguments (e.g.,
--reporter=./json-reporter.js --jsonOutput=results.json).