有没有办法知道nodeunit已完成所有测试?

时间:2014-09-10 15:51:33

标签: node.js firebase nodeunit

我需要在nodeunit成功通过所有测试后运行一些代码。 我正在测试运行所有测试后退出nodeunit的一些Firebase包装器和Firebase个引用块。

我正在寻找一些钩子或回调在所有单元测试通过后运行。因此,我可以终止Firebase流程,以便nodeunit能够退出。

4 个答案:

答案 0 :(得分:4)

没找到正确的方法。

我的临时解决方案:

//Put a *LAST* test to clear all if needed:
exports.last_test = function(test){
    //do_clear_all_things_if_needed();
    setTimeout(process.exit, 500); // exit in 500 milli-seconds    
    test.done(); 
} 

就我而言,这用于确保数据库连接或某些网络连接以任何方式被杀死。它起作用的原因是因为 nodeunit 连续运行测试。

这不是最好的,甚至不是好方法,只是让测试退出

对于 nodeunit 0.9.0

答案 1 :(得分:2)

对于最近的项目,我们通过迭代exports来计算测试,然后调用tearDown来计算完成次数。在最后一次测试退出后,我们调用了process.exit()。

See the spec了解详情。请注意,这是在文件末尾(在将所有测试添加到导出后)

(function(exports) {
  // firebase is holding open a socket connection
  // this just ends the process to terminate it
  var total = 0, expectCount = countTests(exports);
  exports.tearDown = function(done) {
    if( ++total === expectCount ) {
      setTimeout(function() {
        process.exit();
      }, 500);
    }
    done();
  };

  function countTests(exports) {
    var count = 0;
    for(var key in exports) {
      if( key.match(/^test/) ) {
        count++;
      }
    }
    return count;
  }
})(exports);

答案 2 :(得分:0)

根据nodeunit docs,我似乎找不到在所有测试运行后提供回调的方法。

我建议您使用Grunt,以便创建包含任务的测试工作流程,例如:

  1. 安装命令行工具:npm install -g grunt-cli
  2. 将grunt安装到您的项目npm install grunt --save-dev
  3. 安装nodeunit grunt插件:npm install grunt-contrib-nodeunit --save-dev
  4. 创建如下的Gruntfile.js:

    module.exports = function(grunt) {
    
        grunt.initConfig({
            nodeunit : {
                all : ['tests/*.js'] //point to where your tests are
            }
        });
    
        grunt.loadNpmTasks('grunt-contrib-nodeunit');
    
        grunt.registerTask('test', [
            'nodeunit'
        ]);
    };
    
  5. 通过将您的grunt文件更改为以下内容,创建将在测试后运行的自定义任务:

    module.exports = function(grunt) {
    
        grunt.initConfig({
            nodeunit : {
                all : ['tests/*.js'] //point to where your tests are
            }
        });
    
        grunt.loadNpmTasks('grunt-contrib-nodeunit');
    
        //this is just an example you can do whatever you want
        grunt.registerTask('generate-build-json', 'Generates a build.json file containing date and time info of the build', function() {
            fs.writeFileSync('build.json', JSON.stringify({
                platform: os.platform(),
                arch: os.arch(),
                timestamp: new Date().toISOString()
            }, null, 4));
    
            grunt.log.writeln('File build.json created.');
        });
    
        grunt.registerTask('test', [
            'nodeunit',
            'generate-build-json'
        ]);
    };
    
  6. 使用grunt test

  7. 运行测试任务

答案 3 :(得分:0)

我遇到了另一种解决方案,如何处理这个解决方案。我不得不说这里的所有答案都是正确的。然而,当检查grunt时,我发现Grunt正在通过记者运行nodeunit测试,并且记者在所有测试完成后提供回调选项。可以这样做:

在文件夹

test_scripts/
   some_test.js

test.js可以包含以下内容:

//loads default reporter, but any other can be used
var reporter = require('nodeunit').reporters.default;
// safer exit, but process.exit(0) will do the same in most cases
var exit = require('exit');

reporter.run(['test/basic.js'], null, function(){
    console.log(' now the tests are finished');
    exit(0);
});

可以添加脚本,让我们在脚本对象

中说package.json
  "scripts": {
    "nodeunit": "node scripts/some_test.js",
  },

现在它可以作为

完成
npm run nodeunit

some_tests.js中的测试可以链接,也可以使用npm逐个运行

相关问题