25

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.

xam developer
  • 1,923
  • 5
  • 27
  • 39

3 Answers3

8

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
      }
    }
  }
Tim
  • 539
  • 2
  • 9
  • So, even when I do that, I still do not see the code coverage. I added `var blanket = require("blanket")({ "pattern": "/unit-tests/" }); blanket.instrument({}, function(result) { console.log(result); });` Yet, I see an error that says: `TypeError: Cannot read property 'replace' of undefined`. I'm so confused. – xam developer Dec 25 '15 at 14:23
  • I only used blanket once (also with mocha unit tests). I recall the only tricky bit was in the config files (the package.json file i believe). From the blanket page it shows require("blanket")({ /* optional options */ }); – Tim Dec 26 '15 at 01:04
  • Sorry travelling at the moment without my computer - I presume you already know but here is the blanket page: http://blanketjs.org – Tim Dec 26 '15 at 01:07
  • Thank you for sharing. You are correct that the config stuff goes in package.json. You are also correct that there is a blanketjs page. I've reviewed each of those, however, I'm still running into issues. It makes me wonder if something has changed and the change isn't documented or something similar. Thank you for your response though. I sincerely appreciate it. Especially, over the holidays. – xam developer Dec 26 '15 at 12:55
  • Just reviewed how I got blanket to generate reports. I used the following to get me started (linked to from the blanketjs.org page): https://github.com/alex-seville/blanket/blob/master/docs/getting_started_node.md I put the following package.json file directly in my project's root directory: { "config": { "blanket": { "pattern": [ "directoryContainingCodeNeedingCoverageReport" ], "data-cover-never": [ "node_modules", "tests" ] } } } And I used the following command line arguments to mocha (not jasmine): mocha -R html-cov -r blanket – Tim Dec 26 '15 at 15:47
  • Tim - Thank you for your persistence. Unfortunately, I am still stuck. I would love to award you the bounty for your effort. However, its not working yet. I've updated the question showing the code in the current state. I've read the docs again and something just seems off. Does the code I provided in the question above look correct to you? – xam developer Dec 27 '15 at 13:28
3

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

BornReady
  • 305
  • 1
  • 9
1

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);
};
wookieb
  • 4,099
  • 1
  • 14
  • 17
  • Unfortunately, this approach did not work for me. I added your code above the `TerminalReporter..` line in my question. So, the following block was added: `// Setup the coverage reporter var blanket = require('blanket')(); blanket.customReporter = function(coverage_results){ console.log(coverage_results); };` I now get an error that says: `Error: Bad file instrument indicator. Must be a string, regex, function, or array.` What am I doing wrong? The customReporter is a function. – xam developer Dec 21 '15 at 15:52