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

@jimwong/grunt-istanbul

v0.9.1

Published

JavaScript codecoverage tool for Grunt

Downloads

15

Readme

@jimwong/grunt-istanbul Build Status

基于 [email protected] 版本修改,增加参数和注入覆盖率上报的代码到各个 js 文件中,主要修改内容如下:

  1. 增加可选参数 relativePathPrefix ,用于在相对路径的前面加上特定的前缀,比如 code/,其中可以选填某些已配置好的 git 相关的参数,比如 store/${project_name}/${branch}/code,参数如下:
    1. ${commit_hash},会替换成 commit 的 hash 值
    2. ${version},会替换成 git 仓库的 version
    3. ${branch},会替换成当前分支名
    4. ${last_commit_datetime},会替换成上次提交的时间
    5. ${remote},会替换成远程仓库的地址
    6. ${project_name},会替换成项目名
  2. 增加可选参数 incrementCoverageDir,表示生成增量代码覆盖率时,增量增量代码的生效路径,比如 src,表示只有 src 下的文件变化才会被计算增量覆盖率,如果不设置,则表示所有文件都会被计算增量覆盖率
  3. 增加可选参数 coverageVariable,表示覆盖率数据在全局对象下面的变量名,默认是 __coverage__
  4. 增加可选参数 reportURL,覆盖率上报地址
  5. 增加可选参数 autoReportInterval,覆盖率自动上报的时间间隔(ms) 其他参数为此库原本的参数,未作修改

发布:不需要打包

JavaScript codecoverage tool for Grunt

Getting Started

This plugin requires Grunt ~0.4.1

Install this grunt plugin next to your project's Gruntfile.js with: npm install grunt-istanbul

Then add this line to your project's Gruntfile.js gruntfile:

grunt.loadNpmTasks("grunt-istanbul");

Documentation

This grunt plugin uses Istanbul to perform the code coverage tasks. It provides the following grunt tasks:

  • instrument: instruments a file or a directory tree
  • reloadTasks: override instrumented tasks
  • storeCoverage: store coverage from global
  • makeReport: make coverage report

To use this grunt-istanbul plugin, register a grunt task to run the following:

  1. Instrument your source code
  2. Run your test suite against your instrumented source code
  3. Store your coverage results
  4. Make the report

For step 2, an environment variable can be used to determine which path to use for loading the source code to run the tests against. For example, when you normally run your tests you want them to point directly at your source code. But when you run your instanbul code coverage task you want your tests to point at your instrumented source code. The grunt-env plugin can be used for setting an environment variable in a grunt task. Here's an example solution that solves this problem using grunt-env and grunt-mocha-test:

// in Gruntfile.js
module.exports = function (grunt) {
	grunt.initConfig({
		env: {
			coverage: {
				APP_DIR_FOR_CODE_COVERAGE: "../test/coverage/instrument/app/",
			},
		},
		instrument: {
			files: "app/*.js",
			options: {
				lazy: true,
				basePath: "test/coverage/instrument/",
			},
		},
		mochaTest: {
			options: {
				reporter: "spec",
			},
			src: ["test/*.js"],
		},
		storeCoverage: {
			options: {
				dir: "test/coverage/reports",
			},
		},
		makeReport: {
			src: "test/coverage/reports/**/*.json",
			options: {
				type: "lcov",
				dir: "test/coverage/reports",
				print: "detail",
			},
		},
	});

	grunt.registerTask("coverage", [
		"env:coverage",
		"instrument",
		"mochaTest",
		"storeCoverage",
		"makeReport",
	]);
};
// require_helper.js
module.exports = function (path) {
	return require((process.env.APP_DIR_FOR_CODE_COVERAGE || "../app/") + path);
};
// using requireHelper in a test
var requireHelper = require("../require_helper");
var formValidator = requireHelper("form_validator");

You can also pass an instrumenter argument to the instrument options as well as any other arguments that your instrumenter takes.

// in Gruntfile.js
module.exports = function (grunt) {
 var isparta = require('isparta');
  grunt.initConfig({
    instrument: {
      files: 'app/*.es6',
      options: {
        lazy: true,
        basePath: 'test/coverage/instrument/'
        babel: {ignore: false, experimental: true, extensions: ['.es6']},
        instrumenter: isparta.Instrumenter
      }
    }
  });
};

If you want to specify a current working directory, you can specify a path the cwd options :

// in Gruntfile.js
module.exports = function (grunt) {
 var isparta = require('isparta');
  grunt.initConfig({
    instrument: {
      files: '**/*.es6',
      options: {
        cwd: 'app/'
        lazy: true,
        basePath: 'test/coverage/instrument/'
        babel: {ignore: false, experimental: true, extensions: ['.es6']}
      }
    }
  });
};

If you wan to insturment multiple locations, you specify multiple task targets:

// in Gruntfile.js
module.exports = function (grunt) {

  grunt.initConfig({
    env: {
      coverage: {
        APP_DIR_FOR_CODE_COVERAGE: '../test/coverage/instrument/app/'
      }
    },
    instrument: {
      api: {
        files: 'app/*.js',
        options: {
          basePath: 'test/coverage/instrument/'
        }
      },
      web: {
        files: 'web/*.js',
        options: {
          basePath: 'test/coverage/instrument/'
        }
      }
    },
    mochaTest: {
      options: {
        reporter: 'spec'
      },
      src: ['test/*.js']
    },
    storeCoverage: {
      options: {
        dir: 'test/coverage/reports'
      }
    },
    makeReport: {
      src: 'test/coverage/reports/**/*.json',
      options: {
        type: 'lcov',
        dir: 'test/coverage/reports',
        print: 'detail'
      }
    }
  });

  grunt.registerTask('coverage', function(arg) {
    var insturment = arg ? 'insturment:' + arg : 'insturment';

    grunt.task.run(['env:coverage', instrument, 'mochaTest',
      'storeCoverage', 'makeReport']);
  });
# coverage with api and web insturmentation
$ grunt coverage
# coverage with api insturmentation
$ grunt coverage:api
# coverage with web insturmentation
$ grunt coverage:web

By default only files that have coverage are stored. When the 'include-all-sources' option is set to true it will show all instrumented files even if their coverage percentage is 0. To see all instrumented files you can init the storeCoverage task as follows:

grunt.initConfig({
	storeCoverage: {
		options: {
			dir: "test/coverage/reports",
			"include-all-sources": true,
		},
	},
});

Also, checkout the example Gruntfile.js in this repo (note that you do not need to implement the reloadTasks task in this example): Gruntfile.js

For information about the configuration options use the command line istanbul help config.

more examples

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.

Release History

(Nothing yet)

License

Copyright (c) 2014 taichi Licensed under the MIT license.