unit-testingrequirejsmocha.jsmocha-phantomjs

TestRunner Not Running My Mocha / Chai Tests


Despite my best efforts, I can't seem to get my testRunner.html to acknowledge my tests when I run the testRunner.html page in the browser. I've confirmed that it pulls in the test files and runs through the expect but the test runner is still saying that zero passed and zero failed. I've also tried moving the mocha.run() command into the testRunner.html page as an inline script to no effect.

What have I configured incorrectly?

testRunner.html

<!DOCTYPE html>
<html lang = "en">
    <head>
        <meta charset = "utf-8" />
        <title> Tests </title>
        <link href = "../node_modules/mocha/mocha.css" rel = "stylesheet">
    </head>
    <body>
        <div id="mocha"></div>
        <script src="../node_modules/mocha/mocha.js"></script>
        <script>
            mocha.setup('bdd');
        </script>
        <script src = "../node_modules/requirejs/require.js" data-main = "test.config.js"></script>
    </body>
</html>

test.config.js

require.config({
    baseUrl: '../src/public/js',
    paths: {
        jquery: '//code.jquery.com/jquery-2.1.1.min',
        chai: '/../../../node_modules/chai/chai',
        underscore: '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min',
        backbone: '//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min',
        marionette: 'http://marionettejs.com/downloads/backbone.marionette',
        handlebars: '//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.3.0/handlebars',
        syphon: '//cdnjs.cloudflare.com/ajax/libs/backbone.syphon/0.4.1/backbone.syphon.min'
    },
    shim: {
        underscore: {
            exports: '_'
        },
        backbone: {
            deps: ['jquery', 'underscore'],
            exports: 'Backbone'
        },
        marionette: {
            deps: ['backbone'],
            exports: 'Marionette'
        },
        syphon: {
            deps: ['backbone', 'marionette'],
            exports: 'Backbone.Syphon'
        },
        handlebars: {
            exports: 'Handlebars'
        }
    }
});

require([
    '../../../test/src/appTest'
], function() {
    if (typeof mochaPhantomJS !== "undefined") {
        mochaPhantomJS.run();
    }
    else {
        mocha.run();
    }
});

appTest.js

define(['chai'], function(chai) {
    describe('array', function() {
        chai.expect(1+1).to.equal(2);
    });
});

Solution

  • You need to put your test in an it call:

    define(['chai'], function(chai) {
        describe('array', function() {
            it("1 + 1 = 2", function () {
                chai.expect(1+1).to.equal(2);
            });
        });
    });
    

    This is wholly an issue with how you are using Mocha. RequireJS is not a factor at all here.