grunt jasmine-node tests are running twice

2019-06-16 17:32发布

问题:

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

回答1:

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.



回答2:

I was able to reproduce the behavior. This is what seems to be happening:

  1. The task looks in the specified folder (spec in your case) for files with spec in the name.
  2. Then it looks again in every folder in the whole project for files with spec in the name.

What it ends up with is 2 overlapping sets of test files to run.

My first attempt at trying to coerce it into more logical behavior was to set specNameMatcher: null (default is 'spec'), and leave the folder set to 'spec/'. This results in no tests being run, since apparently both conditions (name and folder) must be met for files in the specified folder. You get the same problem if specNameMatcher is left at the default value, but the files in the folder don't have 'spec' in the name.

What does work is to set the folder (or 'test set' or whatever you want to call it) to []:

    jasmine_node: {
        options: {
            forceExit: true
        },
        all: []
    }

The catch is that if you have any other files somewhere else in the project with 'spec' in the name, they'll be mistaken for tests by jasmine.

I would consider this behavior a bug, and it should probably be reported via the project's github issues page.



回答3:

This grunt plugin ( https://github.com/jasmine-contrib/grunt-jasmine-node ) seems to be dead ( https://github.com/jasmine-contrib/grunt-jasmine-node/issues/60 ).

Maybe it is a better to switch to https://github.com/onury/grunt-jasmine-nodejs ?



回答4:

The current up voted answer isn't the solution. You simply modify the expression that's going to match your tests. The answer is as follows:

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

Here you can see that 'all' is set to *'spec/spec.js'. This will search for all tests.

Secondly, just because a project hasn't had a recently commit, doesn't mean it's "old". jasmine-node is simply stable.



回答5:

I have the same issue using grunt-jasmine-node, and as aeryaguzov points out, that project is no longer maintained. Switching to grunt-jasmine-node-new solves the issue for me.

grunt-jasmine-node-new is a fork of grunt-jasmine-node that is actively maintained, and can be found here: https://www.npmjs.com/package/grunt-jasmine-node-new