2

I have tried setting up Blanket.js to get reports on the code coverage of our Dojo app, which we are testing using Mocha. So far, Blanket seems to load, instrument the correct files, and it also seems to figure out which lines it should look for. However, every single file/module in the report shows up as having one line tested. It looks to me like it's the top "define" line. Here's a not minimal example, but it minimally represents our app setup and reproduces the problem.

File structure

▾ dojo-mocha-blanket/
  ▾ modules/
      GUIWidget.html
      GUIWidget.js
  ▾ test/
    ▾ lib/
        blanket_mocha.js // from the blanket /dist
        chai.js
        mocha-blanket.js // adapter as per instructions
        mocha.css
        mocha.js
    ▾ spec/
        GUIWidget.js
      testrunner.html
    index.html

Dojo setup

<html>
<head>
    <title>Dojo-Mocha-Blanket</title>
</head>
<body>
    <div id="GUIWidgetContainer"></div>
    <script>
        var dojoConfig = {
            async: true,
            baseUrl: "",
            modulePaths: {
                "modules": "modules"
            }
        };
    </script>
    <script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.3/dojo/dojo.js"></script>
    <script>
        require([
            "dojo/dom",
            "modules/GUIWidget"
        ], function (dom, GUIWidget) {
            var widget = new GUIWidget({}, "GUIWidgetContainer");
        });
    </script>
</body>
</html>

Module to test

define([
    "dojo/_base/declare",
    "dijit/form/Select",
    "dijit/_TemplatedMixin",
    "dijit/_WidgetsInTemplateMixin",
    "dijit/_WidgetBase",
    "dojo/text!modules/GUIWidget.html"
], function(
    declare,
    Select,
    _TemplatedMixin,
    _WidgetsInTemplateMixin,
    _WidgetBase,
    template
) {
    return declare("GUIWidget", [_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], {

        templateString: template,

        addNumbers: function(lhs, rhs) {
            return lhs + rhs;
        },

        postCreate: function() {
            this.inherited(arguments);
            this.header.innerHTML = this.addNumbers(40, 2);
        }

    });
});

Test setup

<html>
<head>
    <title>Mocha spec runner</title>
    <link rel="stylesheet" href="lib/mocha.css">
</head>
<body>
<div id="mocha"></div>
<script src="lib/mocha.js"></script>
<script 
    src="lib/blanket_mocha.js"
    data-cover-adapter="lib/mocha-blanket.js"
    data-cover-only="/modules"></script>
<script src="lib/chai.js"></script>
<script>
    mocha.setup("bdd");
    mocha.globals(["dojo", "dijit"]);
    mocha.setup();
    expect = chai.expect;
</script>
<script>
    var dojoConfig = {
        async: true,
        packages : [{
            name : "modules",
            location : "/modules"
        } ]
    };
</script>
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.3/dojo/dojo.js"></script>
<script>
    require(
        {
            paths: {
                "spec": "/test/spec"
            }
        },
        ["spec/GUIWidget"], function() {
            mocha.run();
        });
</script>
</body>
</html>

The test

define([
    "modules/GUIWidget"
], function(
    GUIWidget
) {
    describe("GUIWidget", function() {
        var widget;

        beforeEach(function() {
            widget = new GUIWidget();
            widget.startup();
        });

        it("Should set header to 42", function() {
            expect(widget.header.innerHTML).to.equal("42");
        });

        it("Should add numbers", function() {
            expect(widget.addNumbers(1, 2)).to.equal(3);
        })

        afterEach(function() {
            widget.destroyRecursive();
        });
    });
});

Results (larger image here) enter image description here

Apologies for the long code and all the bootstrapping stuff, but I've found the setup to be non-trivial :) If anyone has any ideas of why this might be happening, I'd be all ears. Thanks!

Victor Sand
  • 2,270
  • 1
  • 14
  • 32
  • 1
    The testing system used by Dojo is [Intern](http://theintern.io). Could you let me know what it does not provide that you are trying to get by rolling together Mocha and some other libraries, so I can add an enhancement request to make it work for your use case? Thanks! – C Snover Mar 05 '15 at 08:40
  • @CSnover the application used Mocha before I started working on it, and it seems like Intern has not been tried yet. I might find some time to play around with it, perhaps it will be easier. Thanks for the tip! – Victor Sand Mar 05 '15 at 09:09
  • @CSnover so after a little tinkering, I've found that to get code coverage one would have to set up a number of things, including lcov and some tasks for actually generating the HTML and then ideally stick it onto the test HTML report. What we specifically looking for is to add code coverage reports to the already existing HTML runner report (hence the Blanket attempt). Adding a bunch of more dependencies and/or tasks might not be worth it. – Victor Sand Mar 05 '15 at 12:21

0 Answers0