Custom Coverage Reporter in Jest
Jest's built-in coverage reporting is useful, but sometimes you need more control over the output format or want to integrate with external tools. This challenge asks you to implement a custom Jest coverage reporter that takes the coverage data and outputs it in a simple JSON format. This is a valuable exercise in understanding Jest's internals and customizing its behavior.
Problem Description
You need to create a Jest reporter that receives coverage data after each test run and formats it as a JSON string. The reporter should accept a single argument: the absolute path to a file where the JSON output should be written. The JSON output should contain the following keys:
total: The total number of statements.covered: The number of statements covered by the tests.uncovered: The number of statements not covered by the tests.percentage: The percentage of statements covered (rounded to two decimal places).functions: An object containingtotalandcoveredkeys representing the total and covered functions respectively.branches: An object containingtotalandcoveredkeys representing the total and covered branches respectively.lines: An object containingtotalandcoveredkeys representing the total and covered lines respectively.
The reporter should write the JSON string to the specified file. If the file cannot be written to, the reporter should log an error to the console.
Examples
Example 1:
Input: coverageData = {
total: 100,
covered: 80,
uncovered: 20,
functions: { total: 20, covered: 16 },
branches: { total: 10, covered: 8 },
lines: { total: 100, covered: 80 }
}, filePath = "/tmp/coverage.json"
Output: (Written to /tmp/coverage.json)
{
"total": 100,
"covered": 80,
"uncovered": 20,
"percentage": "80.00",
"functions": {
"total": 20,
"covered": 16
},
"branches": {
"total": 10,
"covered": 8
},
"lines": {
"total": 100,
"covered": 80
}
}
Explanation: The reporter receives the coverage data and calculates the percentage. It then formats the data into a JSON string and writes it to the specified file.
Example 2:
Input: coverageData = {
total: 50,
covered: 0,
uncovered: 50,
functions: { total: 10, covered: 0 },
branches: { total: 5, covered: 0 },
lines: { total: 50, covered: 0 }
}, filePath = "/path/to/nonexistent/coverage.json"
Output: (Written to /tmp/coverage.json)
{
"total": 50,
"covered": 0,
"uncovered": 50,
"percentage": "0.00",
"functions": {
"total": 10,
"covered": 0
},
"branches": {
"total": 5,
"covered": 0
},
"lines": {
"total": 50,
"covered": 0
}
}
Explanation: The reporter receives the coverage data and calculates the percentage. It then formats the data into a JSON string and writes it to the specified file.
Example 3: (Edge Case - File Writing Error)
Input: coverageData = { total: 100, covered: 50, uncovered: 50, functions: {total: 10, covered: 5}, branches: {total: 5, covered: 2}, lines: {total: 100, covered: 50}}, filePath = "/root/coverage.json" (assuming no write permissions)
Output: (Console Output)
Error: EACCES: permission denied, open '/root/coverage.json'
Explanation: The reporter attempts to write to the file but fails due to permission issues. It logs an error message to the console.
Constraints
- The
coverageDataobject will always have the keystotal,covered,uncovered,functions,branches, andlines. - The
functions,branches, andlineskeys will contain objects withtotalandcoveredkeys. - The
filePathwill be a string representing an absolute path. - The percentage should be rounded to two decimal places.
- The reporter should handle file writing errors gracefully and log them to the console.
- The reporter should be implemented as a Jest reporter, meaning it should be compatible with Jest's reporter API.
Notes
- You'll need to create a Jest reporter plugin. This involves creating a class that extends
jest.reporter.BaseCoverageReporter. - The
onRunCompletemethod of the reporter is where you'll receive the coverage data. - Use the
fsmodule to write the JSON data to the file. - Consider using a try-catch block to handle potential file writing errors.
- Remember to export your reporter so Jest can find it. You'll likely need to configure Jest to use your custom reporter in your
jest.config.jsorpackage.json. - Focus on the core functionality of formatting and writing the JSON data. Error handling and Jest integration are also important.