javascriptblanket.js

Blanket.js coverage of dynamically loaded script file


I am using Blanket.js to test code coverage (with Jasmine, though I don't think this matters). I have some scripts which are only loaded as needed. These are loaded using jQuery's $.getScript() function. How can I get Blanket.js to report coverage for a dynamically loaded script?

I have tried loading the script by adding appending a script tag with the src to the end of the head (e.g., $('<script/>', {src: (javascript path), 'data-cover': 'data-cover'}).appendTo('head');).

I don't see any obvious functions to add a script within the blanket.js source code.


Solution

  • To answer my own question, here is a function that does what I wanted:

    function addCoveredScript(url) {
        $('<script/>', {src: url}).appendTo('head');
        blanket.utils.cache[url] = {};
        blanket.utils.attachScript({url:url}, function (content) {
            blanket.instrument({inputFile: content, inputFileName: url},
                               function (instrumented) {
                blanket.utils.cache[url].loaded = true;
                blanket.utils.blanketEval(instrumented);
                blanket.requiringFile(url, true);
            });
       });
    }
    

    I don't know if there is a simpler way to do this, but it works.