咕噜茉莉节点测试运行两次

咕噜茉莉节点测试运行两次

本文介绍了咕噜茉莉节点测试运行两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了grunt来运行node.js茉莉花测试。出于某种原因,使用此配置,结果总是显示双倍的测试结果。



以下是我的配置:

我正在使用插入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']);
};

这导致两次测试运行,而不是一次。

 > grunt 
运行jasmine_node:all(jasmine_node)任务
..

在0.216秒内完成
2个测试,2个断言,0个失败,0个跳过


解决方案

项目相当古老。最新的是从2014年7月开始的。插件看起来很活跃,但是针对一些陈旧的东西运行似乎毫无意义。恕我直言。



使用Jasmine测试CommonJS模块我推荐使用以及
和插件。我将你的例子用于以下文件:

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'
}],

预处理器:{
'src / ** / *。js':['commonjs'],
'spec / ** / *。js':['commonjs']
},

记者:[ '进度'],

浏览器:['PhantomJS']
});
};

Gruntfile.js (可选,如果您仍想使用grunt)

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

您应该也在全局安装karma命令行转义程序,就像您可能用grunt做的那样。 npm install -g karma-cli



在命令行中,您可以通过键入业力开始。它将运行测试,然后观察文件并在每次保存时重新运行它们。 (非常好)



或者,你可以运行 karma start --single-run 来运行你的测试一次并退出。如果您还更新了Gruntfile,您也可以运行 grunt 来运行一次测试。


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
解决方案

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 thekarma-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.

这篇关于咕噜茉莉节点测试运行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:07