2016-07-01 33 views
0

我曾經在一檔名爲寫了這以下的代碼「example.js」:代碼爲什麼我在摩卡的before()鉤子根本不能運行?

console.log('HI HI HI HI'); 

describe('hooks', function() { 
    console.log('before before'); 
    before(function() { 
    console.log('ok'); 
    }); 
    console.log('after before'); 
}) 

輸出當我運行「摩卡example.js」是:

HI HI HI HI 
before before 
after before 
    0 passing (1ms) 

爲什麼沒」 「OK」會打印出來嗎?我認爲before()鉤子在describe()塊中的所有代碼之前運行?

回答

5

它沒有打印,因爲before在測試之前運行,而您沒有。

嘗試添加測試,那麼它應該運行

console.log('1'); 

describe('hooks', function() { 
    console.log('2'); 
    before(function() { 
    console.log('4'); 
    }); 

    console.log('3'); 
    it('description', function() { 
    console.log('5'); 
    // nothing more here but still a test 
    }) 
}) 
+0

哦~~我不知道它在測試之前,只跑了。我認爲它在任何代碼之前運行。謝謝! – user3835653

相關問題