1
votes

I'm having problems testing my durandal application with mocha using phantomJs. Here is what I did:

Firt I created a dummyPage to include the tests with the requireJs environment:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="mocha.css" />
    <script type="text/javascript" src="chai.js"></script>
    <script type="text/javascript" src="mocha.js"></script>
    <script type="text/javascript" src="../lib/jquery/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="../lib/knockout/knockout-2.3.0.js"></script>
    <script type="text/javascript" src="../lib/require/require.js"></script>
    <script type="text/javascript">
        require.config({
            baseUrl: '../app/',
            paths: {
                'app': '../app',
                'specs': '../sampleTest/specs/',
                'text': '../lib/require/text',
                'durandal': '../lib/durandal/js',
                'plugins' : '../lib/durandal/js/plugins',
                'transitions' : '/lib/durandal/js/transitions',
                'knockout': '../lib/knockout/knockout-2.3.0',
                'jquery': '../lib/jquery/jquery-1.9.1'
            }
        });
        var runTests = function (specfiles) {
            console.log('enter runTests');
            require(specfiles, function () {
                mocha.setup('bdd');
                assert = chai.assert;

                mocha.run();
            });
        };
    </script>
</head>
<body>
</body>
</html>

My dummyTest looks like that:

console.log('enter dummy test');

define(['viewmodels/flickr'], function (flickr) {
    describe('Test 1', function(){
        it('should append a value', function () {
            var arr = [];
            arr.push('foo');
            arr.push('bar');
            assert.equal(arr[0],'foo', 'foo is a string');
        });
    });
})

When I run phantomJs with the following js-file I'm getting the error:

Getting ReferenceError: Can't find variable: describe

page.onLoadFinished = function () {
    page.evaluate(function (specFiles) {
        runTests(specFiles);
    }, specFiles);
};

page.open('mocha.htm');
1

1 Answers

1
votes

It seems to me the problem is that Mocha is not correctly initialized before you start requiring the modules that contain your tests. Change your runTests function so that the initialization your test modules need is performed before they are loaded. like this:

var runTests = function (specfiles) {
    console.log('enter runTests');

    // Initialize mocha and leak assert into the global space.
    mocha.setup('bdd');
    assert = chai.assert;

    require(specfiles, function () {
        mocha.run();
    });
};