GithubHelp home page GithubHelp logo

ahilke / js-crap-score Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 0.0 608 KB

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

License: MIT License

TypeScript 92.51% Handlebars 3.43% JavaScript 0.18% CSS 3.88%
crap-score code-quality cyclomatic-complexity javascript test-coverage typescript

js-crap-score's Introduction

CRAP Score

npm version

Use CRAP to estimate and visualize the change risk of your JS/TS project.

Example

The HTML 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 uses complexity and coverage information to give an estimate how likely it is that a function contains bugs or breaks with future changes.

CRAP is an acronym for Change Risk Anti-Patterns and is computed as follows: $comp^2 \cdot (1 - cov)^3 + comp$, where comp is the cyclomatic complexity of the function and cov is the statement coverage as number between 0 (no coverage) and 1 (fully covered).

A common guideline is to address functions with a CRAP score above 30 by either adding additional tests or refactoring the function to reduce complexity. This translates to simple functions with complexity 5 or lower not requiring any test coverage, while a function with complexity over 30 always requires refactoring.

How to Use

This package provides multiple ways to generate and collect information about coverage, complexity and CRAP for each function within your project. It generates both a human-readable HTML report and a JSON report for further processing.

Usage Options:

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.

Contributing

Got any feedback? I'm eager to hear it! Please open an issue or send me an email.

If you want to contribute code, please read CONTRIBUTING.md first.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.