2014-09-10 139 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

相關問題