Mocha
全局安装 mocha
npm install -g mocha
在项目根目录执行 mocha
命令,默认执行 test
目录下的所有测试文件
mocha
测试覆盖率 istanbul
npm install -g istanbul
$ istanbul cover _mocha $ istanbul cover _mocha test/test.sqrt.js
gyw@gyw:~/Desktop/code/project/StressTestCode/test$ istanbul cover _mocha . test case ✓ app.listen() ✓ test autobahnHandle() ✓ test upload() 3 passing (17ms) ============================================================================= Writing coverage object [/home/gyw/Desktop/code/project/StressTestCode/test/coverage/coverage.json] Writing coverage reports at [/home/gyw/Desktop/code/project/StressTestCode/test/coverage] ============================================================================= =============================== Coverage summary =============================== Statements : 40.91% ( 27/66 ) Branches : 0% ( 0/8 ) Functions : 8.33% ( 1/12 ) Lines : 40.91% ( 27/66 ) ================================================================================
运行单元测试出现如下错误
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
解决方式: 添加 timeout
it('test()', function (done) { done(); }).timeout(1000000);