1

I'm using mocha-grunt to be able to run/see my client side tests on the command line. For some reason, it seems that decrementColumn is being called twice when I run my tests on the command line.

I wonder if some sort of phantomJS window scoping problem with beforeEach, because if I copy that block of code ( new Level instantiation) to the top of the decrementColumn test, everything works for both browser and command line.

FUNCTION

decrementColumn: function( columnPosition ){
    --this.columns[ 0 ]
},

TEST

describe( 'Level', function(){
    beforeEach( function(){
        level = new Level( [ 2, 3, 5 ] )
    })
describe( '#decrementColumn()', function(){
    it( 'should reduce column by one', function(){
        level.decrementColumn( 1 )
        expect ( level.columns[ 0 ] ).to.equal( 1 )
    })
})

})

GRUNTFILE

module.exports = function( grunt ){
    grunt.initConfig({
        pkg: grunt.file.readJSON( 'package.json' ),
        mocha: {
            all: {
                src: [ 'test/runner.html' ],
            },
            options: {
                reporter: 'Spec',
                run: true
            }
        }
    });

    grunt.loadNpmTasks( 'grunt-mocha' )

    grunt.registerTask( 'default', [ 'mocha' ] )
};

SPECRUNNER

<html>
<head>
    <meta charset="utf-8">
    <title>Mocha Tests</title>
    <link rel="stylesheet" href="../lib/mocha.css" />
</head>
<body>
    <div id="mocha"></div>
    <!-- mocha dependencies -->
    <script type="text/javascript" src="../lib/jquery.min.js"></script>
    <script type="text/javascript" src="../lib/expect.js"></script>
    <script type="text/javascript" src="../lib/mocha.js"></script>
    <script type="text/javascript" src="../lib/chai.js"></script>

    <!-- application files -->
    <script type="text/javascript" src='../client/view.js'></script>
    <script type="text/javascript" src='../client/logic.js'></script>
    <script type="text/javascript" src='../client/level.js'></script>
    <script type="text/javascript" src='../client/game.js'></script>
    <script type="text/javascript" src='../client/init.js'></script>

    <!-- configure mocha && chai -->
    <script>
        mocha.setup( 'bdd' )
        expect = chai.expect
    </script>

    <!-- test files -->
    <script type="text/javascript" src='level-spec.js'></script>

    <script>
        mocha.globals(['jQuery']);
        mocha.run();
    </script>
</body>
</html>

browser command line

Salar
  • 861
  • 9
  • 13

1 Answers1

3

Are you following this guide? https://gist.github.com/maicki/7781943

What does your test runner file look like? You might be missing a line...

<script>
    // Only tests run in real browser, injected script run if options.run == true
    if (navigator.userAgent.indexOf('PhantomJS') < 0) {
      mocha.run();
    }
</script>
smartalek
  • 56
  • 2
  • Updated question with test runner file. Yeah, that was the guide I was following, and the check for PhantomJS was the line I was missing. Nice eyes – Salar Jun 06 '14 at 17:28