Running Blanket.js

1.2k Views Asked by At

I am testing some code programmatically using Jasmine from Node. To do this, I've setup the following:

function runTests() {
    var Jasmine = require('jasmine');
    var jasmine = new Jasmine();    

    jasmine.loadConfig({
        spec_dir: 'unit-tests',
        spec_files: [
            'tests-*.js'
        ]
    });

    var blanket = require('blanket')();

    var TerminalReporter = require('jasmine-terminal-reporter');
    var reporter = new TerminalReporter({});
    jasmine.addReporter(reporter);      

    jasmine.execute();  
}

runTests();

When those tests run, I would like to get the code coverage details. While attempting this, I stumbled upon blanket.js. My question is, how do I programmatically output the code coverage results? Using the code above, I get an error. The error simply says:

Error: Bad file instrument indicator.  Must be a string, regex, function, or array.

Why? What am I doing wrong?

Update

In my package.son file, I have the following section:

"config": {
  "blanket": {      
    "data-cover-flags": {
      "engineOnly":true
    }
  }      
}

I have updated my runTests function to look like this:

function runTests() {
    var Jasmine = require('jasmine');
    var jasmine = new Jasmine();    

    jasmine.loadConfig({
        spec_dir: 'unit-tests',
        spec_files: [
            'tests-*.js'
        ]
    });

    // Setup the coverage reporter
    var blanket = require("blanket")();
    var blanketReporter = function(coverageData) {
        console.log(coverageData);
    };
    blanket.customReporter = blanketReporter;

    blanket.instrument({
        inputFile: 'library.js'
    }, function(result) { });

    var TerminalReporter = require('jasmine-terminal-reporter');
    var reporter = new TerminalReporter({});
    jasmine.addReporter(reporter);      

    jasmine.execute();  
}

library.js

'use strict';

class Processor
{
    execute(vals) {
      let result = 0;
      vals.forEach(function(v) {
        result += v;
      });
      return result;
    }
}
module.exports = Processor;

The code above is in a file called "main.js" which I run by calling node main.js from the console window. "library.js" is at the same level and the tests are in a child directory at "./unit-tests/tests.js". When the above runs, the customerReporter code is never called. I don't understand why.

3

There are 3 best solutions below

1
wookieb On

Try custom reporter https://github.com/alex-seville/blanket/blob/master/docs/advanced_browser.md#reporters

blanket.customReporter=function(coverage_results){
    console.log(coverage_results);
};
0
BornReady On

It would seem that you need to add the reporter to the Jasmine environment.

jasmine.getEnv().addReporter(reporter);

Source: http://jasmine.github.io/2.1/custom_reporter.html

6
Tim On

https://github.com/alex-seville/blanket/issues/248

If you don't specify the below in your package.json, blanket throws a "Bad file instrument indicator. Must be a string, regex, function, or array." error. As soon as you require('blanket'); from anywhere within node.

  "scripts": {
    "blanket": {
      "data-cover-flags": {
        "engineOnly":true
      }
    }
  }