通过Postman或Karate执行HTTP端点时如何收集Istanbul的代码覆盖率 [英] How to collect code coverage with Istanbul when executing HTTP endpoints via Postman or Karate

查看:79
本文介绍了通过Postman或Karate执行HTTP端点时如何收集Istanbul的代码覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JS项目,提供了一组利用Express和典型Express/Router模式的端点.

I have a JS project that provides a set of endpoints leveraging Express with a typical express/router pattern.

const express = require('express');
const router = new express.Router();

router.post('/', async (req, res, next) => { });
router.get('/:abc', async (req, res, next) => { });

module.exports = router;

我可以使用 npm start 成功启动服务器,该服务器调用 node ./src/index.js 并使端点在 https://localhost可用:8080

I can successfully start the server with npm start which calls node ./src/index.js and makes the endpoints available at https://localhost:8080

我还可以使用Postman之类的工具或Karate之类的自动化工具来成功测试这些端点.

I can also successfully test these endpoints utilizing a tool like Postman or automation like Karate.

我遇到的问题是,当通过 http行使产品源JS时,我似乎无法使用Istanbul收集代码覆盖率://localhost:8080 .

The problem i'm having is that I can't seem to collect code coverage using Istanbul when exercising the product source JS through http://localhost:8080.

我尝试了 npm start ,然后尝试了 nyc --all src/**/*.js gradlew测试.后者是测试端点的自动化.这导致覆盖率为0%,我认为这是由于未在npm start下运行nyc.

I've tried npm start followed by nyc --all src/**/*.js gradlew test. The latter being automation that tests the endpoints. This results in 0% coverage which i'm assuming was due to not running nyc with npm start.

接下来,我尝试了 nyc --all src/**/*.js npm start ,并注意到了一些覆盖范围,但这仅仅是从启动Express服务器开始的覆盖范围.

Next I tried nyc --all src/**/*.js npm start and noticed some coverage, but this was just coverage from starting the Express server.

接下来,我尝试了 nyc --all src/**/*.js npm start ,然后进行了 gradlew test ,并注意到代码覆盖率结果与否时相同进行了端点测试.

Next I tried nyc --all src/**/*.js npm start followed by gradlew test and noticed the code coverage results were the same as when no endpoint tests were run.

接下来,我尝试将前面的两个命令放入一个异步运行的JS脚本(myscript.js)中,其中在gradle测试开始运行并运行 nyc --all src/**/之前,Express服务器已启动.* .js myscript.js .得出的结果与我以前的试验相同,在该试验中,只有npm start才收到代码覆盖.

Next I tried putting the prior two commands into a single JS script(myscript.js) running each asynchronously wherein the Express server was started before the gradle tests started running and ran nyc --all src/**/*.js myscript.js. The results from this were the same as my previous trial wherein only npm start received code coverage.

接下来我尝试了 nyc --all src/**/*.js npm start ,然后尝试了 nyc --all src/**/*.js -no-clean gradlew测试,并注意到代码覆盖率结果与未运行端点测试时相同.

Next I tried nyc --all src/**/*.js npm start followed by nyc --all src/**/*.js -no-clean gradlew test and noticed the code coverage results were the same as when no endpoint tests were run.

接下来,我通过将它们包装到package.json脚本中并运行 npm run< scriptName> 来获得完全相同的行为,从而尝试了上述所有尝试.

Next I tried all of the attempts above by wrapping them into package.json scripts and running npm run <scriptName> getting the same exact behavior.

最后,我尝试了 nyc instrument src instrumented/src --compact = false ,然后尝试了 npm run start:coverage ,其中该start:coverage脚本调用了已检测到的index.js在 node ./instrumented/src/index.js ,然后是 gradlew test ,然后是 nyc report --reporter = lcov .这种尝试还没有从gradlew端点测试中产生任何其他代码覆盖.

Finally I tried nyc instrument src instrumented/src --compact=false followed by npm run start:coverage wherein this start:coverage script calls the instrumented index.js at node ./instrumented/src/index.js followed by gradlew test followed by nyc report --reporter=lcov. This attempt also failing to produce any additional code coverage from the gradlew endpoint tests.

在网上做一些研究时,我发现了这篇文章如何设置基于Express的代码覆盖率API?

Doing some research online I came across this post How do I setup code coverage on my Express based API?

以为这看起来与我的问题十分相似.例如,伊斯坦布尔通过执行端点执行代码时,不知道如何覆盖代码.

And thought this looks eerily similar to my problems. Eg Istanbul doesn't know how to cover code when exercising the code through executing endpoints.

我决定仍然发布此内容,因为以上发布内容已过时,并且想征求意见,看看是否有比此方法更好的解决方案 https://github.com/gotwarlost/istanbul-middleware

I decided to still post this as the above post is quite a bit old and wanted to get opinions and see if there is a better solution than https://github.com/gotwarlost/istanbul-middleware

编辑

添加更多有关我们如何在今天不使用Istanbul的情况下启动Express服务器和运行自动化的详细信息.只是为了阐明我们正在使用的工具以及我们投资的自动化工具.(主要是空手道和Java)

Adding more specifics about how we start the Express server and run automation without Istanbul today. Just to clarify what we're working with and automation tools we're invested in. (Mainly Karate and Java)

/*
  calls --> node -r dotenv/config src/index.js
*/
npm start

/*
  calls --> gradlew clean test
  this effectively calls a tool called Karate
  Karate's base url is pointed to: https://locahost:8080
  Karate tests execute endpoints on that base url
  This would be akin to using Postman however Karate has quite a bit of configuration options
  https://github.com/intuit/karate
*/
npm test

推荐答案

经过许多小时的调查,我们设法解决了这一问题.@balexandre发布的先前项目已更新,以说明如何执行此操作.

Through many hours of investigation we've managed to solve this. Prior project posted by @balexandre has been updated to illustrate how to do this.

https://github.com/kirksl/karate-istanbul

这篇关于通过Postman或Karate执行HTTP端点时如何收集Istanbul的代码覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆