3

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

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

4

4 回答 4

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 连续运行测试。

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

对于节点单元0.9.0

于 2014-12-17T06:48:13.117 回答
2

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

有关完整详细信息,请参阅规范。请注意,这在文件末尾(在所有测试都添加到导出之后)

(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);
于 2014-09-11T22:31:51.840 回答
0

As per nodeunit docs I can't seem to find a way to provide a callback after all tests have ran.

I suggest that you use Grunt so you can create a test workflow with tasks, for example:

  1. Install the command line tool: npm install -g grunt-cli
  2. Install grunt to your project npm install grunt --save-dev
  3. Install the nodeunit grunt plugin: npm install grunt-contrib-nodeunit --save-dev
  4. Create a Gruntfile.js like the following:

    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. Create your custom task that will be run after the tests by changing your grunt file to the following:

    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. Run your test tasks with grunt test

于 2014-09-10T16:00:23.220 回答
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 一个一个地运行

于 2015-05-07T16:02:40.143 回答