Search code examples
javascriptrequirejscode-coveragemocha.jsblanket.js

Mocha + BlanketJS + RequireJS, No method 'reporter'


I'm using Mocha with RequireJS and tests are running fine, however, when I try to add in blanket code coverage I'm getting Uncaught TypeError: Object #<HTMLDivElement> has no method 'reporter',

Here's the code I'm running:

<div id="mocha"></div>

<script src="../src/js/vendor/requirejs/require.js"></script>

<script src="../src/js/vendor/blanket/dist/qunit/blanket.js"
data-cover-adapter="../src/js/vendor/blanket/src/adapters/mocha-blanket.js"></script>

<script src="SpecRunner.js" data-cover></script>

and my specrunner:

require(["../src/js/require-config.js"], function () {

// Set testing config
require.config({
    baseUrl: "../src/js",
    paths: {
        "mocha": "vendor/mocha/mocha",
        "chai": "vendor/chai/chai"
    },
    urlArgs: "bust=" + (new Date()).getTime()
});

require([
    "require",
    "chai",
    "mocha"
], function (require, chai) {
    var should = chai.should();
    mocha.setup("bdd");

    require([
        "specs.js",
    ], function(require) {
        if (window.mochaPhantomJS) {
            mochaPhantomJS.run();
        } else {
            mocha.run();
        }
    });

});

});

Like I said - my tests are all running fine, I just can't figure out why blanket's not working.

Update:

I can get it to run by including the script tag for mocha at the beginning, however, now it runs the mocha tests twice.


Solution

  • I figured it out and did a write-up on getting Blanket to work with Mocha in AMD. Here's a blog post outlining the process as well as a repo with the working code.

    I'm using the following to load my tests:

    require(["../src/js/require-config"], function () {
    
      require.config({
        baseUrl: "../src/js",
        paths: {
            chai: "vendor/chai/chai"
        }
      });
    
      require([
        "chai"
      ], function (chai) {
        chai.should();
        window.expect = chai.expect;
        mocha.setup("bdd");
    
        require([
            "specs.js"
        ], function () {
            mocha.run();
        });
      });
    
    });
    

    And then the following code on the page:

    <div id="mocha"></div>
    
    <script src="../src/js/vendor/mocha/mocha.js"></script>
    
    <script data-main="main-tests" src="../src/js/vendor/requirejs/require.js"></script>
    
    <script src="../src/js/vendor/blanket/dist/qunit/blanket.js" data-cover-only="../src/js/component"></script>
    <script type="text/javascript" src="../node_modules/grunt-blanket-mocha/support/mocha-blanket.js"></script>
    
    <script>
    /* global blanket */
    if (window.PHANTOMJS) {
        blanket.options("reporter", "../node_modules/grunt-blanket-mocha/support/grunt-reporter.js");
    }
    </script>