https://github.com/bacebu4/cobertura
A Cobertura reporter for `node:test`
https://github.com/bacebu4/cobertura
cobertura node-test reporter
Last synced: 6 months ago
JSON representation
A Cobertura reporter for `node:test`
- Host: GitHub
- URL: https://github.com/bacebu4/cobertura
- Owner: bacebu4
- License: mit
- Created: 2023-11-19T16:54:47.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-12-27T06:03:34.000Z (9 months ago)
- Last Synced: 2025-04-12T15:04:36.755Z (6 months ago)
- Topics: cobertura, node-test, reporter
- Language: JavaScript
- Homepage:
- Size: 38.1 KB
- Stars: 9
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.npmjs.com/package/cobertura)

[](https://codecov.io/gh/bacebu4/cobertura)# Cobertura Reporter
A Cobertura reporter for `node:test`. Primarily was created in order to support GitLab's [Test coverage visualization](https://docs.gitlab.com/ee/ci/testing/test_coverage_visualization.html).
Also you can see the reporter in action in [this GitLab repo](https://gitlab.com/bacebu4/cobertura-test/-/merge_requests/1/diffs).
## Installation
```bash
npm install --save-dev cobertura
```or
```bash
yarn add --dev cobertura
```## Usage
Define your `test` script:
```bash
node --test \
--experimental-test-coverage \
--test-reporter=cobertura --test-reporter-destination=cobertura.xml \
--test-reporter=spec --test-reporter-destination=stdout
```Your `.gitlab-ci.yml` can look something like this:
```yml
stages:
- testtest:
stage: test
image: node:22-alpine
artifacts:
when: always
reports:
coverage_report:
coverage_format: cobertura
path: ./cobertura.xml
script:
- node -v
- npm run test
coverage: '/all files[^|]*\|[^|]*\s+([\d\.]+)/'
```## Supported Versions
The reporter can be used with **node v20 and later**, detailed explanation is [here](https://github.com/bacebu4/cobertura/issues/3#issuecomment-2370444363)
## Acknowledgements
This test reporter is heavily inspired by test reporters of [this GitHub repo](https://github.com/MoLow/reporters) and some code parts might be directly copied from there.
## Example
Source file:
```js
export function fooOne(x) {
if (x === 1) {
return x + 1;
}if (x === 2) {
return x + 1;
}const result = x + 1;
return result + 1;
}
```Test file:
```js
import { describe, it } from 'node:test';
import assert from 'node:assert';
import { fooOne } from './foo.js';describe('fooTest', () => {
it('returns result', () => {
const result = fooOne(12);assert.strictEqual(result, 14);
});it('handles when x equals to 2', () => {
const result = fooOne(2);assert.strictEqual(result, 3);
});
});
```Output:
```xml
/Users/bacebu4/dev/cobertura-test
```