简体   繁体   English

某些测试文件的Node.js代码覆盖率

[英]Node.js Code Coverage of Certain Test Files

I just came into a Node.js project that has a ton of unit tests, most of which, unfortunately, are outdated and don't run. 我刚进入一个有大量单元测试的Node.js项目,不幸的是,其中大多数已过时且无法运行。 There's no test runner or any kind of code coverage tool implemented, and it is certainly not in my scope of work to refactor everything. 没有实现测试运行程序或任何类型的代码覆盖率工具,并且重构所有内容当然不在我的工作范围内。

That being said, that doesn't stop me from wanting to check the code coverage of my work. 话虽如此,但这并不能阻止我检查工作的代码覆盖率。 I've looked at both Istanbul and Blanket and can't find a way to only have them run certain unit tests. 我已经研究了Istanbul和Blanket,但找不到一种只能让它们运行某些单元测试的方法。 Surely, there is a way so that I can get a report that, obviously, reports a very low percentage of coverage, but that I can dive into certain files and such (those that my tests hit) to ensure my code is throughly tested. 当然,有一种方法可以使我得到一份报告,很明显,报告的覆盖率很低,但是我可以深入研究某些文件,并确保文件(经过测试的文件)经过全面测试。

With istanbul.js, you can easily get the coverage information this by for example specifying the following command (Following example uses mocha but it will be similar for any test framework): 使用istanbul.js,您可以通过例如指定以下命令来轻松获取覆盖率信息(以下示例使用mocha,但对于任何测试框架而言,它都相似):

istanbul cover node_modules/.bin/_mocha -- -u exports -R spec test/test1.spec.js test/test2.spec.js

You can also specify all the tests in particular sub-directory eg: test/yourfeaturetest/*.spec.js . 您还可以在特定子目录中指定所有测试,例如: test/yourfeaturetest/*.spec.js You also have something like test/myfeature/**/*.spec.js to cover all the tests in the test/myfeature directory including tests that could have been created recursively in sub-directories. 您还可以使用诸如test/myfeature/**/*.spec.js来覆盖test / myfeature目录中的所有测试,包括可以在子目录中递归创建的测试。

As for me, I use gulp and thus utilize plugins such as gulp-istanbul and run tests and coverage via gulp tasks. 对我而言,我使用gulp并因此利用了gulp-istanbul之类的插件,并通过gulp任务运行测试和涵盖范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM