2014-09-10 138 views
3

我需要在nodeunit成功通过所有测试后运行一些代码。 我正在测试一些Firebase包装和Firebase参考块在所有测试运行后退出nodeunit。有没有办法知道nodeunit已经完成所有测试?

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

+0

的回答你的问题是使用安装和拆卸运行一个接一个。如果您在根级别声明它们,则它们将针对每个测试运行,以便确定运行了多少次测试以及何时完成。但是,这是[xy问题](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/66378#66378)。你真正的问题是为什么你的测试单元挂起,你如何阻止它们这样做?这将有助于更具体一些,并提供用户可以复制的内容。 – Kato 2014-09-11 22:29:20

+0

你其实给了我很好的答案,那可以解决问题。 – webduvet 2014-09-12 11:08:29

回答

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(); 
} 

在我而言,这是用来确保DB连接或某些连接网络被杀任何方式。它的工作原因是因为nodeunit连续运行测试。

这不是最好的,即使不是好的方法,只是让测试退出

对于nodeunit 0.9.0

0

根据nodeunit文档,我似乎无法找到一种方法来提供所有测试运行后的回调。

我建议你使用Grunt,所以你可以创建任务的测试工作流程,例如:

  1. 安装命令行工具:npm install -g grunt-cli
  2. 安装咕噜到项目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. 创建将试验后通过改变你的咕噜文件到以下运行自定义任务:

    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

+0

以及我的问题是与Firebase参考相关的,它在所有测试完成后仍在运行并阻止它们完成。因此,我需要在它后面进行一些家务管理。我不咕噜会在这里帮助。 – webduvet 2014-09-10 16:04:11

+0

Humm ...不确定Firebase是什么,但是如果你的测试没有完成那么IMO我认为你应该改变你的问题。 – renatoargh 2014-09-10 16:08:53

+0

测试正在完成,全部通过,因Firebase连接而不退出进程。我想要的是知道在所有测试通过之后和nodeunit退出之前是否有钩子可以运行代码。我将编辑我的问题。 – webduvet 2014-09-10 16:12:25

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); 
+0

这不适用于我的情况。 ** exports.tearDown **运行每个测试。 – 2014-12-17 06:41:56

+0

是的,它每次都被调用,这就是为什么我们保持一个计数并且只在最终测试结束后运行拆卸。 – Kato 2014-12-17 15:28:17

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

相关问题