0
我有一個對象包含一些函數*類型生成器,我試圖寫摩卡單元測試。但是,摩卡不會調用這些函數,而且我很難弄清楚解決方案是什麼。摩卡單元測試不調用對象內的函數發生器
下面是一些示例代碼:
'use strict';
var ctr = {
fn: function(arg,cb) {
console.log('arg:', arg);
var err = (arg == true);
return cb(err, arg);
}
};
describe('Mocha test', function() {
it('should call the function', function(done) {
console.log('a');
ctr.fn(false, function(err,data) {
console.log('b');
if(err) return done(err);
console.log('c');
done();
console.log('d');
});
console.log('e');
});
});
當我運行$ mocha testfile.js
我得到這個輸出:
Mocha test
a
arg: false
b
c
✓ should call the function (39ms)
d
e
1 passing (172ms)
這是所有預期。但是,如果我用「fn: function*(...
」,在運行$ mocha testfile.js
結果替換「fn: function(...
」:
Mocha test
a
e
1) should call the function
0 passing (2s)
1 failing
1) Mocha test should call the function:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
我想我找到了答案,這個Mocha plugin,但運行mocha -r mocha-plugin-co testfile.js
給出了相同的結果,不指定它。
我認爲我需要「確保承諾解決」,但我不清楚這意味着什麼。我沒有[故意]使用Promises,但我使用的npm包需要在此表單中定義的函數。
是否有一個簡單的解決方案,使我的期望在這裏,我是否需要將我的代碼更改爲常規函數,並希望沒有任何突破的方式工作?
是的,這工作。儘管如此,一些更多的解釋會有幫助看起來函數*返回一個生成器,它是一種特殊類型的迭代器。 .next()從函數*捕獲返回和任何'yield'。我不太明白摩卡爲什麼會掛起,而關於函數*的MDN文章沒有說明。 也感謝您查找併發布正確的模塊鏈接。當我發佈時,我仍然有很多標籤打開... –