2最近在学习alsotang大佬写的《Node.js 包教不包会》系列, 过程中犯了不少二, 做个笔记~~
Lesson 6: RangeError: Maximum call stack size exceeded
先甩出来最后发现的问题……
代码并没有错, 只是运行 js 文件的时候, 没有传入对应的参数 arg, 然后却在 main.js 中尝试通过 argv[2] 使用该变量
node main.js [arg]
Lesson 6: 《测试用例:mocha,should,istanbul》
犯二过程:
照着代码敲了一遍, 然后运行 main.js 之后一直报堆栈溢出的错, 如下…..
然后 copy 了课程源码, 之后运行也一直报错……..
就这么怼了老半天….然后 console.log 输出变量, 发现 argv[2] 是 undefined
然后去查 argv 是啥 ….. 太粗心了……. 还是应该多熟悉 api
var fibonacci = function(n) {
if (n === 0) {
return 0;
}
if (n === 1) {
return 1;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
exports.fibonacci = fibonacci;
if (require.main === module) {
// 如果是直接执行 main.js,则进入此处
// 如果 main.js 被其他文件 require,则此处不会执行。
var n = Number(process.argv[2]);
console.log('fibonacci(' + n + ') is', fibonacci(n));
}
Lesson 6: istanbul 不能输出测试覆盖率
解决方案
Providing the relative path to the _mocha JS file should solve this problem. As in:
$ istanbul cover node_modules/mocha/bin/_mocha -- -R spec