Socket
Socket
Sign inDemoInstall

karma-coverage

Package Overview
Dependencies
131
Maintainers
3
Versions
42
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    karma-coverage

A Karma plugin. Generate code coverage.


Version published
Maintainers
3
Install size
13.3 MB
Created

Package description

What is karma-coverage?

The karma-coverage npm package is a plugin for the Karma test runner that generates code coverage reports. It uses Istanbul under the hood to instrument the code and then record the coverage data when the tests are run. The reports can be generated in various formats and can be integrated into continuous integration workflows to ensure code quality.

What are karma-coverage's main functionalities?

Coverage Reporting

This feature allows you to generate coverage reports in various formats such as HTML, LCOV, and JSON. The code sample shows how to configure Karma to use the karma-coverage plugin to generate an HTML coverage report.

module.exports = function(config) {
  config.set({
    // other Karma configuration...
    reporters: ['progress', 'coverage'],
    preprocessors: {
      '**/*.js': ['coverage']
    },
    coverageReporter: {
      type : 'html',
      dir : 'coverage/'
    }
  });
};

Threshold Enforcement

This feature allows you to enforce coverage thresholds. If the code does not meet the specified thresholds for statements, branches, functions, or lines, Karma will exit with an error. The code sample demonstrates how to set these thresholds in the Karma configuration.

module.exports = function(config) {
  config.set({
    // other Karma configuration...
    reporters: ['progress', 'coverage'],
    coverageReporter: {
      check: {
        global: {
          statements: 80,
          branches: 80,
          functions: 80,
          lines: 80
        }
      }
    }
  });
};

Other packages similar to karma-coverage

Changelog

Source

2.0.0 (2019-08-20)

Bug Fixes

  • build: Update to lodash 4 (d6d21d2), closes #242
  • reporter: replace colons in the output path (3b2bffa)
  • bump lodash for CVE-2018-16487 (15f8b12)

Chores

BREAKING CHANGES

  • deps: This set of changes may impact some use cases.

  • chore: Add Updated Istanbul Dependencies

The istanbul package is deprecated in favor several split packages that control different aspects of how istanbul works. This commit adds the recommended packages that will be used in future commits as karma-coverage's usage of istanbul is updated to the latest api.

  • refactor(reporter): Follow new report API

This commit refactors the in memory report implementation to use the new istanbul report API.

Report creation is removed from newer versions of the istanbul API, so this commit adds a set of utility functions to wrap around the new API and provide similar functionality as the old API. The top level export uses the new utility function to register the in-memory report.

  • refactor(preprocessor): Switch to istanbul-lib-instrument

This commit updates the preprocessor to use istanbul-lib-instrument instead of the deprecated istanbul package. The biggest change in this refactor is using a callable function instead of a constructor when creating instrumenters

The old istanbul package exposed the Instrumenter directly, allowing the preprocessor to create an instance of it. istanbul-lib-instrument, however, exposes a callable function that creates an Instrumenter. This commit updates the preprocessor to follow this new pattern of using a callable function. In order to ensure backwards compatibility, a utility function is added to wrap constructors with a callable function for creation automatically.

This change allows the following configuration for creating instrumenters:

  1. An object that contains an Instrumenter constructor
  2. An Instrumenter constructor itself
  3. A callable function that returns an Instrumenter instance.

This commit also uses the istanbul-lib-source-maps package to handle storing source maps. A global source map store registers source maps so they can be used later on in the reporter.

  • refactor(reporter): Switch to istanbul-lib-coverage

This commit updates the reporter by using the istanbul-lib-coverage package api for handling coverage checking/management and the istanbul-lib-report package api for handling reporting.

The new apis remove the need for collectors and remove the need to handle disposing collectors.

  • refactor: Remove unused source cache utilities

This commit removes the source-cache-store and source-cache files as they are no longer being used. The source-map-store and istanbul-lib-source-maps are used instead, so these files are no longer needed.

  • feat(util): Add Reset Functionality

This commit updates the report creator utility to allow resetting the custom reporter map.

  • fix(preprocessor): Track Coverage Maps Properly

This commit updates the preprocessor to properly access file coverage when storing it in the global coverage map (when includeAllSources is true). The previous method did not work because the returned instrumented code from the default istanbul instrumenter returns the coverage map in a POJO object instead of JSON notation. This breaks the coverage regex used to match and parse the coverage map.

The istanbul instrumenter offers the ability to receive the coverage map for the last instrumented file through a separate function, so that is tested for and used if it is supported. The original method is used as a fallback for backwards compatibility.

This commit also addresses changes from the v0 instanbul instrumenter options. The changes are additive only to maintain backwards compatibility for other instrumenters.

  • fix(reporter): Access Data Properly to Check Coverage

This commit fixes errors with accessing data properly during the checkCoverage method. A previous commit updated the implementation to use istanbul-lib-coverage, but this involved an api change to access the raw coverage data (which checkCoverage uses).

This commit also fixes the checking coverage for each file by using a map to store file coverage summaries instead of merging summaries like the global results. Per file coverage now works as expected.

  • test: Update Unit Tests to use new Istanbul API

This commit updates the mocking done in unit tests to properly mock the new istanbul API. Additionally, new unit test suites are added for the utility methods report-creator and source-map-store.

  • drop support for node < 8
  • reporter: the output folder names change, they no longer contain :

<a name="1.1.2"></a>

Readme

Source

karma-coverage

js-standard-style npm version npm downloads

Build Status Dependency Status devDependency Status

Generate code coverage using Istanbul.

Installation

The easiest way is to install karma-coverage as a devDependency, by running

npm install karma karma-coverage --save-dev

Configuration

For configuration details see docs/configuration.

Examples

Basic

// karma.conf.js
module.exports = function(config) {
  config.set({
    files: [
      'src/**/*.js',
      'test/**/*.js'
    ],

    // coverage reporter generates the coverage
    reporters: ['progress', 'coverage'],

    preprocessors: {
      // source files, that you wanna generate coverage for
      // do not include tests or libraries
      // (these files will be instrumented by Istanbul)
      'src/**/*.js': ['coverage']
    },

    // optionally, configure the reporter
    coverageReporter: {
      type : 'html',
      dir : 'coverage/'
    }
  });
};

CoffeeScript

For an example on how to use with CoffeeScript see examples/coffee. For an example of how to use with CoffeeScript and the RequireJS module loader, see examples/coffee-requirejs (and also see the useJSExtensionForCoffeeScript option in docs/configuration.md).

Advanced, multiple reporters

// karma.conf.js
module.exports = function(config) {
  config.set({
    files: [
      'src/**/*.js',
      'test/**/*.js'
    ],
    reporters: ['progress', 'coverage'],
    preprocessors: {
      'src/**/*.js': ['coverage']
    },
    coverageReporter: {
      // specify a common output directory
      dir: 'build/reports/coverage',
      reporters: [
        // reporters not supporting the `file` property
        { type: 'html', subdir: 'report-html' },
        { type: 'lcov', subdir: 'report-lcov' },
        // reporters supporting the `file` property, use `subdir` to directly
        // output them in the `dir` directory
        { type: 'cobertura', subdir: '.', file: 'cobertura.txt' },
        { type: 'lcovonly', subdir: '.', file: 'report-lcovonly.txt' },
        { type: 'teamcity', subdir: '.', file: 'teamcity.txt' },
        { type: 'text', subdir: '.', file: 'text.txt' },
        { type: 'text-summary', subdir: '.', file: 'text-summary.txt' },
      ]
    }
  });
};

FAQ

Don't minify instrumenter output

When using the istanbul instrumenter (default), you can disable code compaction by adding the following to your configuration.

// karma.conf.js
module.exports = function(config) {
  config.set({
    coverageReporter: {
      instrumenterOptions: {
        istanbul: { noCompact: true }
      }
    }
  });
};

For more information on Karma see the homepage.

Keywords

FAQs

Last updated on 20 Aug 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc