Search code examples
javascriptnode.jsgruntjsjasminejasmine-node

grunt jasmine-node tests are running twice


I set up grunt to run node.js jasmine tests. For some reason, with this config, the results always show double the tests.

Here is my config:

I'm using jasmine-node which plugs into grunt.

/spec/some-spec.js:

var myModule = require('../src/myModule.js');
describe('test', function(){
     it('works', function(done){
         setTimeout(function(){
             expect(1).toBe(1);
             done();
         }, 100);
     });
});

Gruntfile.js:

module.exports = function(grunt) {
    grunt.initConfig({
        jasmine_node: {
            options: {
                forceExit: true
            },
            all: ['spec/']
        }
    });
    grunt.loadNpmTasks('grunt-jasmine-node');
    grunt.registerTask('default', ['jasmine_node']);
};

This results in two tests running rather than one.

> grunt
Running "jasmine_node:all" (jasmine_node) task
..

Finished in 0.216 seconds
2 tests, 2 assertions, 0 failures, 0 skipped

Solution

  • The jasmine-node project is pretty old. The latest commit is from July of 2014. The grunt-jasmine-node plugin appears to be active, but running against something that is going stale seems a little pointless IMHO.

    To test CommonJS modules using Jasmine I'd recommend using Karma along with the karma-jasmine and karma-commonjs plugins. I got your example working with the following files:

    package.json

    {
      "private": "true",
      "devDependencies": {
        "grunt": "^0.4.5",
        "grunt-jasmine-node": "^0.3.1",
        "grunt-karma": "^0.10.1",
        "jasmine-core": "^2.3.4",
        "karma": "^0.12.31",
        "karma-commonjs": "0.0.13",
        "karma-jasmine": "^0.3.5",
        "karma-phantomjs-launcher": "^0.1.4"
      }
    }
    

    karma.conf.js

    module.exports = function(config) {
      config.set({
      basePath: '.',
      frameworks: ['jasmine', 'commonjs'],
      files: [{
            pattern: 'src/**/*.js'
          }, {
            pattern: 'spec/**/*.js'
          }],
    
      preprocessors: {
        'src/**/*.js': ['commonjs'],
        'spec/**/*.js': ['commonjs']
      },
    
      reporters: ['progress'],
    
      browsers: ['PhantomJS']
      });
    };
    

    Gruntfile.js (optional if you still want to use grunt)

    module.exports = function(grunt) {
      grunt.initConfig({
        karma: {
          unit: {
            configFile: 'karma.conf.js',
            options: {
              singleRun: true
            }
          }
        }
      });
      grunt.loadNpmTasks('grunt-karma');
      grunt.registerTask('default', ['karma:unit']);
    };
    

    You should also install the karma command line runner globally, just like you probably did with grunt. npm install -g karma-cli

    From your command line you can start karma by typing karma start. It will run the tests and then watch your files and re-run them on every save. (VERY NICE)

    Alternatively you can run karma start --single-run to have it just run your tests once and exit. If you also updated your Gruntfile you can also just run grunt to run the tests once.