通过更改几个 mocha 源代码文件,我能够使其在 Windows 上运行。
npm install mocha 之后(在我的例子中,我只是为我的项目安装它,而不是全局安装):
1)首先进入node_modules\mocha\lib\utils.js查找并修复watch函数如下:
exports.watch = function(files, fn) {
var options = { interval: 100 };
files.forEach(function(file) {
debug('file %s', file);
fs.watch(file, options, function(curr, prev) {
fn(file);
});
});
};
我用 fs.watch 替换了 fs.watchFile (参见https://github.com/fgnass/node-dev/issues/26 https://github.com/fgnass/node-dev/issues/26有关详细信息)因为第一个似乎不适用于 Windows。
2)现已开放node_modules\mocha\bin\_mocha并应用以下修复:
a) 找到并注释掉或删除以下代码:
process.on('SIGINT', function(){
showCursor();
console.log('\n');
process.exit();
});
由于上面没有等效的 POSIX 信号线,因此必须删除(理想情况下由正确的实现替换,请参阅Windows 中 Node.js 中 process.on('SIGINT') 的等价物是什么? https://stackoverflow.com/questions/10021373/what-is-the-windows-equivalent-of-process-onsigint-in-node-js更多细节)
b) 找到以下代码utils.watch(watchFiles, function(){...并将其替换为
var lastRun = new Date();
utils.watch(watchFiles, function(){
if (new Date() - lastRun > 300)
{
purge();
stop()
mocha.suite = mocha.suite.clone();
ui = interfaces[program.ui](mocha.suite);
loadAndRun();
lastRun = new Date();
}
});
它可以限制来自 fs.watch 的过多回调。
c) 最后的更改是删除或注释掉此行:
process.stdout.write('\r' + str);
in 函数播放(arr,间隔)。它只是消除噪音。