2015-07-06 42 views
3

我有茉莉花定製記者茉莉花,關閉默認記者

var myReporter = { 
    jasmineStarted: function(suiteInfo) { 

    console.log('Running suite with ' + suiteInfo.totalSpecsDefined); 
    }, 

    suiteStarted: function(result) { 

    console.log('Suite started: ' + result.description + ' whose full description is: ' + result.fullName); 
    }, 

    specStarted: function(result) { 

    console.log('Spec started: ' + result.description + ' whose full description is: ' + result.fullName); 
    }, 

    specDone: function(result) { 

    console.log('Spec: ' + result.description + ' was ' + result.status); 
    for(var i = 0; i < result.failedExpectations.length; i++) { 


     console.log('Failure: ' + result.failedExpectations[i].message); 
     console.log(result.failedExpectations[i].stack); 
    } 
    }, 

    suiteDone: function(result) { 

    console.log('Suite: ' + result.description + ' was ' + result.status); 
    for(var i = 0; i < result.failedExpectations.length; i++) { 

     console.log('AfterAll ' + result.failedExpectations[i].message); 
     console.log(result.failedExpectations[i].stack); 
    } 
    }, 
    jasmineDone: function() { 
    console.log('Finished suite'); 
    } 
}; 

jasmine.getEnv().addReporter(myReporter); 

describe('Top Level suite', function() { 
    it('spec', function() { 
    expect(1).toBe(1); 
    }); 

    describe('Nested suite', function() { 
    it('nested spec', function() { 
     expect(true).toBe(true); 
    }); 
    }); 
}); 

直播:http://jsfiddle.net/wLnmbh88/

現在的結果對HTML和控制檯顯示,但我需要關閉這個在HTML中。我如何僅在控制檯中顯示測試結果?代碼示例來自jasmine文檔。

回答

-1

在文件jasmine-html.jsboot.jsin your JSFiddle中添加默認的HTML記者。

jasmine-html.jssource) - 實際上是記者本身,通過Jasmine記者API實現; boot.jssource) - 一個文件,它爲Jasmine配置環境並執行它。此外,它還會激活默認的HTML記者,因爲這兩個文件都被認爲是Jasmine的核心,所以通過包含它們,您將擁有基本的Jasmine安裝。

在你的情況下,你想實現一個自定義記者+你想擺脫默認的HTML記者,所以你應該刪除jasmine-html.js並用自定義記者替換boot.js。 Jasmine文檔中有關於configuring custom bootannotated source的頁面。茉莉2.X

基本自定義啓動腳本,沒有任何記者應啓用看起來有點像這樣:

(function() { 

    window.jasmine = jasmineRequire.core(jasmineRequire); 

    var env = jasmine.getEnv(); 
    var jasmineInterface = jasmineRequire.interface(jasmine, env); 

    extend(window, jasmineInterface); 

    window.setTimeout = window.setTimeout; 
    window.setInterval = window.setInterval; 
    window.clearTimeout = window.clearTimeout; 
    window.clearInterval = window.clearInterval; 

    var currentWindowOnload = window.onload; 

    window.onload = function() { 
    if (currentWindowOnload) { 
     currentWindowOnload(); 
    } 
    env.execute(); 
    }; 

    function extend(destination, source) { 
    for (var property in source) destination[property] = source[property]; 
    return destination; 
    } 

}()); 

確保,您包括jasmine.js在此之後啓動腳本。

你有你的開機設置完成後,您可以將記者像往常一樣進行註冊:

jasmine.getEnv().addReporter(myReporter); 

,甚至更好 - 在啓動腳本中添加它。

檢查的jsfiddle:http://jsfiddle.net/wLnmbh88/2/

7

無需去除jasmine-html.js和小提琴與自定義boot.js。只需添加您所選擇的前記者刪除任何記者:

var jasmineReporters = require('jasmine-reporters'); 
jasmine.getEnv().clearReporters(); 
jasmine.getEnv().addReporter(new jasmineReporters.TapReporter()); 
+0

clearReporters不是一個函數 – Roel

+0

嗯它仍然是我的設置和快速搜索不顯示變化,所以也許'茉莉花reporters' WASN」添加不同於提問者的代碼 - 我添加了一行來顯示這個,高於前兩行 –

+1

'jasmine.clearReporters();'對我來說工作得很好 –