最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to generate LCOV report based on Jasmine's SpecRunner.html? - Stack Overflow

programmeradmin3浏览0评论

We are using Jasmine for our JavaScript unit tests. We have a SpecRunner.html file to run the tests. Does there exist a tool to which I can pass the path to SpecRunner.html and the path to the directory of JavaScript (not the specs) files and it would generate a LCOV report. For example, something like this:

phantomjs jasmine_lcov.js SpecRunner.html WebContent/js

We are using Jasmine for our JavaScript unit tests. We have a SpecRunner.html file to run the tests. Does there exist a tool to which I can pass the path to SpecRunner.html and the path to the directory of JavaScript (not the specs) files and it would generate a LCOV report. For example, something like this:

phantomjs jasmine_lcov.js SpecRunner.html WebContent/js
Share Improve this question edited May 6, 2015 at 10:23 SBel asked May 5, 2015 at 21:23 SBelSBel 3,3597 gold badges32 silver badges49 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 11

I agree with @zaabalonso that Karma is the correct choice. Since you want LCOV reports, you'll also need the karma-coverage plugin and presuming you want to run headless in CI you'll probably want the karma-phantomjs-launcher. Running through Grunt is optional as you can always run karma directly from the command line with karma-cli (npm install -g karma-cli).

A basic setup (with requireJS) looks something like this:

package.json

{
  "private": "true",
  "devDependencies": {
    "grunt": "^0.4.5",
    "grunt-jasmine-node": "^0.3.1",
    "grunt-karma": "^0.10.1",
    "jasmine-core": "^2.3.4",
    "karma": "^0.12.32",
    "karma-coverage": "^0.3.1",
    "karma-jasmine": "^0.3.5",
    "karma-phantomjs-launcher": "^0.1.4",
    "karma-requirejs": "^0.2.2",
    "requirejs": "^2.1.17"
  }
}

karma.conf.js (Notice the preprocessors and coverageReporter sections

module.exports = function(config) {
  config.set({
  basePath: '.',
  frameworks: ['jasmine', 'requirejs'],
  files: [{
        pattern: 'src/**/*.js',
        included: false
      }, {
        pattern: 'spec/**/*.js',
        included: false
      },
      "test-main.js"],

  preprocessors: {
    'src/**/*.js': ['coverage']
  },

  reporters: ['progress', 'coverage'],

  coverageReporter: {
    // specify a common output directory
    dir: 'build/reports/coverage',
    reporters: [
      { type: 'lcov', subdir: 'report-lcov' },
      { type: 'lcovonly', subdir: '.', file: 'report-lcovonly.txt' }
    ]
  },

  browsers: ['PhantomJS']
  });
};

test-main.js

var allTestFiles = [];
var TEST_REGEXP = /^\/base\/spec\/\S*(spec|test)\.js$/i;

var pathToModule = function (path) {
    return path.replace(/^\/base\//, '').replace(/\.js$/, '');
};

Object.keys(window.__karma__.files).forEach(function (file) {
    if (TEST_REGEXP.test(file)) {
        // Normalize paths to RequireJS module names.
        allTestFiles.push(pathToModule(file));
    }
});

require.config({
    // Karma serves files under /base, which is the basePath from your config file
    baseUrl: '/base/',
    enforceDefine: true,
    xhtml: false,
    waitSeconds: 30,

    // dynamically load all test files
    deps: allTestFiles,
    callback: window.__karma__.start
});

Gruntfile.js (Optional if you want to use Grunt)

module.exports = function(grunt) {
  grunt.initConfig({
    karma: {
      unit: {
        configFile: 'karma.conf.js',
        options: {
          singleRun: true
        }
      }
    }
  });
  grunt.loadNpmTasks('grunt-karma');
  grunt.registerTask('default', ['karma:unit']);
};

You can run the tests command line with karma start. That will launch the karma server and run the tests once. It will keep the server up and will re-run the tests anytime you modify your source or test sources. If you want to run the test only once (in CI perhaps) you simply run karma start --single-run.

Chutzpah will also do this. It is focused on Windows platform however, so that may or may not work for you. Here is the full command line options documentation, but your command might be something like this:

chutzpah.console.exe SpecRunner.html /coverage /lcov coverage.dat

If you need to fine tune things like coverage excludes or references, etc, you can use json config files places in the area where the tests are as described here. There is no need to specify location of your Javascript code under test on command line as that is automatically detected by references in SpecRunner.html.

I have found Chutzpah to be very slick and easy to use.

We are using Karma over grunt the config looks like that:

  options = {
        karma: {
            unit: {
                options: {
                    files: ['test/unit/specs/*.js'],
                    reporters: ['progress', 'coverage'],
                    preprocessors: {
                        'src/js/*.js': ['coverage']
                    },
                    coverageReporter: {
                        type : 'html',
                        dir : 'build/coverage/'
                    },
                    frameworks: ['jasmine'],
                    singleRun: true
                }
            }
        }
}

you don't specify the

SpecRunner.js

but you can specify *.js for all your spec files.

you can run it with

grunt karma

that will generate your report similar to the one you showed.

发布评论

评论列表(0)

  1. 暂无评论