Running Blanket.js
Asked Answered
L

3

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.

Lorrainelorrayne answered 17/12, 2015 at 2:37 Comment(4)
Instructions for using the Jasmine test runner.Desiderate
Still no luck running blanket from Node.Lorrainelorrayne
What does library.js look like?Tempi
@EdinM library.js is very basic. I included it above. I attempted to isolate the issue so I reduced it down to the above.Lorrainelorrayne
H
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
      }
    }
  }
Hirai answered 23/12, 2015 at 16:25 Comment(6)
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.Lorrainelorrayne
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 */ });Hirai
Sorry travelling at the moment without my computer - I presume you already know but here is the blanket page: blanketjs.orgHirai
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.Lorrainelorrayne
Just reviewed how I got blanket to generate reports. I used the following to get me started (linked to from the blanketjs.org page): github.com/alex-seville/blanket/blob/master/docs/… 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 <js file that runs tests>Hirai
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?Lorrainelorrayne
E
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

Expurgatory answered 23/12, 2015 at 16:21 Comment(0)
P
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);
};
Partly answered 21/12, 2015 at 15:35 Comment(1)
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.Lorrainelorrayne

© 2022 - 2024 — McMap. All rights reserved.