npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cobertura

v1.0.2

Published

A cobertura reporter for `node:test`

Downloads

10,149

Readme

npm version tests codecov

Cobertura Reporter

A Cobertura reporter for node:test. Primarily was created in order to support GitLab's Test coverage visualization

Installation

npm install --save-dev cobertura

or

yarn add --dev cobertura

Usage

Define your test script:

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:

stages:
  - test

test:
  stage: test
  image: node:21-alpine
  artifacts:
    when: always
    reports:
      coverage_report:
        coverage_format: cobertura
        path: ./cobertura.xml
  script:
    - node -v
    - npm run test
  coverage: '/all files[^|]*\|[^|]*\s+([\d\.]+)/'

Acknowledgements

This test reporter is heavily inspired by test reporters of this GitHub repo and some code parts might be directly copied from there.

Example

Source file:

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:

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 version="1.0" ?>
<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">
<coverage lines-valid="30" lines-covered="28" line-rate="0.9333" branches-valid="8" branches-covered="7" branch-rate="0.8750" timestamp="1700416562185" complexity="0" version="0.1">
	<sources >
		<source >
/Users/bacebu4/dev/cobertura-test
		</source>
	</sources>
	<packages >
		<package name="cobertura-test.src" line-rate="0.9333" branch-rate="0.8750">
			<class name="foo.js" filename="src/foo.js" line-rate="0.8462" branch-rate="0.7500">
				<methods >
					<method name="fooOne" hits="2" signature="()V">
						<lines >
							<line number="1" hits="2"/>
						</lines>
					</method>
				</methods>
				<lines >
					<line number="1" hits="1" branch="true" condition-coverage="100% (2/2)"/>
					<line number="2" hits="2" branch="true" condition-coverage="0% (0/1)"/>
					<line number="3" hits="0" branch="false"/>
					<line number="4" hits="0" branch="false"/>
					<line number="5" hits="2" branch="false"/>
					<line number="6" hits="2" branch="true" condition-coverage="100% (1/1)"/>
					<line number="7" hits="1" branch="false"/>
					<line number="8" hits="1" branch="false"/>
					<line number="9" hits="1" branch="false"/>
					<line number="10" hits="1" branch="false"/>
					<line number="11" hits="1" branch="false"/>
					<line number="12" hits="1" branch="false"/>
					<line number="13" hits="2" branch="false"/>
				</lines>
			</class>
			<class name="foo.test.js" filename="src/foo.test.js" line-rate="1.0000" branch-rate="1.0000">
				<methods >
					<method name="(anonymous_0)" hits="1" signature="()V">
						<lines >
							<line number="5" hits="1"/>
						</lines>
					</method>
					<method name="(anonymous_1)" hits="1" signature="()V">
						<lines >
							<line number="6" hits="1"/>
						</lines>
					</method>
					<method name="(anonymous_2)" hits="1" signature="()V">
						<lines >
							<line number="12" hits="1"/>
						</lines>
					</method>
				</methods>
				<lines >
					<line number="1" hits="1" branch="true" condition-coverage="100% (1/1)"/>
					<line number="2" hits="1" branch="false"/>
					<line number="3" hits="1" branch="false"/>
					<line number="4" hits="1" branch="false"/>
					<line number="5" hits="1" branch="true" condition-coverage="100% (1/1)"/>
					<line number="6" hits="1" branch="true" condition-coverage="100% (1/1)"/>
					<line number="7" hits="1" branch="false"/>
					<line number="8" hits="1" branch="false"/>
					<line number="9" hits="1" branch="false"/>
					<line number="10" hits="1" branch="false"/>
					<line number="11" hits="1" branch="false"/>
					<line number="12" hits="1" branch="true" condition-coverage="100% (1/1)"/>
					<line number="13" hits="1" branch="false"/>
					<line number="14" hits="1" branch="false"/>
					<line number="15" hits="1" branch="false"/>
					<line number="16" hits="1" branch="false"/>
					<line number="17" hits="1" branch="false"/>
				</lines>
			</class>
		</package>
	</packages>
</coverage>