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

crap-score

v1.2.0

Published

Calculate and visualize the CRAP score of a JS/TS project using the provided `jest` integration, CLI, or API.

Downloads

11

Readme

CRAP Score

npm version Maintainability Test Coverage

Calculate and visualize the CRAP score of a JS/TS project using the provided jest integration, CLI, or API.

Example

The CRAP report of the project itself can be found under https://ahilke.github.io/js-crap-score/.

What is CRAP?

The CRAP score is a measure of the risk of a function ranging from 1 (best) to infinity (worst). It stands for Change Risk Anti-Patterns and is computed as follows:

complexity^2 * (1 - coverage)^3 + complexity

where complexity is the cyclomatic complexity of the function and coverage is the statement coverage as number between 0 (no coverage) and 1 (fully covered).

Combining complexity and coverage information, the CRAP score gives you insight into your riskiest functions, i.e. functions that are the most likely to contain bugs. You can reduce the risk and thus the CRAP score by either improving test coverage or refactoring your function to decrease complexity (e.g. by extracting functions).

How to Use

Jest Reporter

Add crap-score as test reporter to jest. When jest is run with coverage enabled, this will also generate a CRAP report. Example for jest.config.json:

"reporters": ["default", "crap-score"]

The reporter also accepts options, for example:

"reporters": [
    "default",
    [
        "crap-score",
        {
            "jsonReportFile": "crap.json",
        }
    ]
]

A typed interface for the reporter options is available via import type { ReporterOptions } from "crap-score";.

Available options:

| Option | Description | | -------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | | jsonReportFile | Specifies path where the JSON report will be written to. Defaults to crap-report/crap-report.json. Pass false to disable JSON report. | | htmlReportDir | Specifies path where the HTML report will be written to. Defaults to crap-report/html/. Pass false to disable HTML report. | | log | Changes log behaviour. "silent" suppresses all logs. "debug" prints additional logs. |

CLI

Install the package (or use it directly via npx), then just run npx crap <path-to-coverage>. The command expects an istanbul JSON coverage report as input (see JSON Coverage Report) and generates both an HTML and a JSON report in the crap-report folder, containing the CRAP score of each function in the original istanbul report.

crap --help

API

ESM

import { getCrapReport, CrapReport } from "crap-score";

const report: CrapReport = await getCrapReport({
    testCoverage: "./coverage/coverage-final.json",
});

CommonJS

To use the library API in a CommonJS project, you will need to use dynamic import statements as this is a ESM library:

async function main() {
    const { getCrapReport } = await import("crap-score");
    const report = await getCrapReport({
        testCoverage: "./coverage/coverage-final.json",
    });
}

If you are using TypeScript, make sure to have "moduleResolution": "node16" to avoid import being transformed into require. If that is not an option, you can work around it via eval:

async function main() {
    const { getCrapReport } = await eval("import('crap-score')");
    const report = await getCrapReport({
        testCoverage: "./coverage/coverage-final.json",
    });
}

istanbul JSON Coverage Report

If you are using jest, you can generate an istanbul JSON coverage report by adding collectCoverage: true and coverageReporters: ["json"] to your configuration. This will generate a JSON report under coverage-final.json.

Make sure to also review other configuration related to coverage, especially collectCoverageFrom. This allows you to include files in your report even if they are not covered. This is important, since any uncovered function in your project may have a very high CRAP score.