node.js - How to use the istanbul API to load config from grunt -
i trying steer istanbul grunt without resorting .istanbul.yml
file. idea run grunt-jasmine-nodejs unit testing , integrate istanbul custom reporter:
module.exports = function(grunt) { grunt.initconfig({ jasmine_nodejs: { options: { specnamesuffix: 'spec.js', usehelpers: false, reporters: { console: {} }, customreporters: [ require('./tests/lib/istanbul-reporter.js')({ coveragevar: '__coverage__', dir: './tests/coverage' reports: ['text','json','html'] }) ] }, default: { specs: [ 'tests/unit/**' ] } } }); grunt.loadnpmtasks('grunt-jasmine-nodejs'); };
the jasmine reporter notifies sandboxed-module should instrument tested files. istanbul api doc gives idea loads configuration .loadfile()
:
module.exports = function (opt) { var istanbul = require('istanbul'); istanbul.config.loadfile(false, { instrumentation: { variable: opt.coveragevar }, reporting: { dir: opt.dir } }); var sandboxedmodule = require('sandboxed-module'); sandboxedmodule.registerbuiltinsourcetransformer('istanbul'); global[opt.coveragevar] = {}; var collector, reporter; return { jasminestarted: function () { collector = new istanbul.collector(); reporter = new istanbul.reporter(); reporter.addall(opt.reports); }, suitestarted: function () {}, suitedone: function () {}, specstarted: function () {}, specdone: function () {}, jasminedone: function () { console.log('\n\n'); collector.add(global[opt.coveragevar]); reporter.write(collector, true, function () { return; }); } }; };
it doesn't work. i've tried several variants, including config.loadobject(config.defaultconfig(), {...})
, overrides not applied.
apart writing .istanbul.yml
file grunt config data, there programatic way?
istanbul.config.loadfile
not initialize anything, gives object further usage.
in addition, sandboxed-module not take options instrumenter uses. there open pull request handle this.
so time being, custom source transformer needed. rewritten custom jasmine reporter looks this:
var istanbul = require('istanbul'); var instrumentmethod; exports.init = function (opt) { var collector, reporter; global[opt.instrumenting.coveragevariable] = {}; return { jasminestarted: function () { var configobj = istanbul.config.loadfile(false, opt); var instrumenter = new istanbul.instrumenter(opt.instrumenting); instrumentmethod = instrumenter.instrumentsync.bind(instrumenter); collector = new istanbul.collector(); reporter = new istanbul.reporter(configobj); reporter.addall(opt.reporting.reports); }, suitestarted: function () {}, suitedone: function () {}, specstarted: function () {}, specdone: function () {}, jasminedone: function () { console.log('\n\n'); collector.add(global[opt.instrumenting.coveragevariable]); reporter.write(collector, true, function () { return; }); } }; }; exports.transformer = function (source) { source = instrumentmethod(source, this.filename); return source; };
the opt
parameter not compatible yaml file. there 2 reasons:
- large parts deal handling multiple files, while setup sandboxed-module dealing single files: file, transform it.
hooks
,checks
handled outside defining instrumenters, collectors , reporters.
to avoid confusion, i've named first part instrumenting
, not instrumentation
. handed new instrumenter(opt.instrumenting)
. note documentation outdated (v0.3.0). second part reporting
compatible counterpart in yaml.
in gruntfile, noting defaults:
require('./tests/lib/istanbul-reporter.js').init({ instrumenting: { debug: false, // not in yaml walkdebug: false, // not in yaml codegenerationoptions: undefined, // not in yaml, see escodegen noautowrap: false, // not in yaml coveragevariable: '__coverage__', // yaml name variable nocompact: false, // yaml states compact: true embedsource: false, preservecomments: false, esmodules: false }, reporting: { // same yaml } }
and finally, in jasmine spec:
var sandboxedmodule = require('sandboxed-module'); var isr = require('../lib/istanbul-reporter.js'); var testedmodule = sandboxedmodule.require('../../lib/tested-module.js', { // requires: { }, // globals: { }, ... sourcetransformers: { istanbul: isr.transformer } });
Comments
Post a Comment