Jest Code Coverage with Cobertura Output
This challenge focuses on leveraging Jest to generate code coverage reports in the Cobertura XML format. Cobertura reports are a widely adopted standard for visualizing code coverage, making it easier to integrate with CI/CD pipelines and code quality tools. You will configure Jest to produce this specific output format and understand how to interpret the generated report.
Problem Description
Your task is to configure a Jest testing environment in a TypeScript project to generate code coverage reports in the Cobertura XML format. This involves setting up Jest's configuration to specify the desired output format and potentially writing a simple TypeScript function and a corresponding Jest test to demonstrate the coverage report generation.
Key Requirements:
- Jest Configuration: Modify or create a
jest.config.js(orjest.config.ts) file to instruct Jest to produce Cobertura reports. - Coverage Report Generation: Execute Jest in a way that generates the Cobertura XML file.
- TypeScript Project: The solution should be for a TypeScript project.
- Basic Functionality: Include a simple TypeScript function and a Jest test for it to ensure there's code to measure coverage against.
Expected Behavior:
- Running Jest should produce a
cobertura-coverage.xmlfile (or a similarly named file, depending on Jest configuration) in the project's root or specified coverage directory. - This XML file should adhere to the Cobertura report schema.
- The report should accurately reflect the code coverage of the provided TypeScript function by its Jest test.
Edge Cases to Consider:
- No Tests Executed: What happens if no tests are run, or if the tests don't cover any of the code?
- Multiple Files: How does the report handle coverage across multiple TypeScript files? (For this specific challenge, one file is sufficient to demonstrate the concept).
Examples
Example 1: Basic Coverage
Project Structure:
/src
- calculator.ts
/tests
- calculator.test.ts
jest.config.js
src/calculator.ts:
export function add(a: number, b: number): number {
return a + b;
}
export function subtract(a: number, b: number): number {
return a - b;
}
tests/calculator.test.ts:
import { add } from '../src/calculator';
describe('Calculator', () => {
test('adds two numbers', () => {
expect(add(1, 2)).toBe(3);
});
});
jest.config.js:
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverage: true,
coverageReporters: ['cobertura'],
coverageDirectory: './coverage',
};
Execution:
Run npx jest --coverage
Expected Output (Simplified coverage/cobertura-coverage.xml snippet):
<?xml version="1.0" ?>
<coverage branch-rate="0" branches-covered="0" branches-valid="0" complexity="0" line-rate="0.5" lines-covered="1" lines-valid="2" timestamp="1678886400" version="6.0.0">
<sources>
<source>/path/to/your/project</source>
</sources>
<packages>
<package branch-rate="0" complexity="0" line-rate="0.5" name="src">
<classes>
<class branch-rate="0" complexity="0" filename="src/calculator.ts" line-rate="0.5" name="calculator">
<methods/>
<lines>
<line hits="1" number="2"/>
<line hits="0" number="6"/>
</lines>
</class>
</classes>
</package>
</packages>
</coverage>
Explanation:
The jest.config.js is configured to collect coverage and use the cobertura reporter. When npx jest --coverage is run, Jest analyzes the calculator.ts file and its tests. The add function is covered (hits="1"), while subtract is not (hits="0"), resulting in a line-rate of 0.5 for the calculator.ts file.
Constraints
- The project must be set up for TypeScript development (e.g., using
ts-jest). - Jest must be installed as a development dependency.
- The generated Cobertura report should be at least parsable by standard Cobertura viewers/tools.
Notes
- You will need to install
jest,ts-jest, and@types/jestas development dependencies. - The
coverageReportersoption injest.config.jsis crucial for specifying the output format. - You can use
npx jest --coverageto run Jest and generate coverage reports. The--coverageflag is shorthand for enabling coverage collection. - Explore the generated
cobertura-coverage.xmlfile to understand how lines of code are tracked and whether they were executed by tests. - Consider how you might integrate this report into a CI/CD pipeline.