通过 Postman 或 Karate 执行 HTTP 端点时如何使用 Istanbul 收集代码覆盖率

How to collect code coverage with Istanbul when executing HTTP endpoints via Postman or Karate

我有一个 JS 项目,它提供了一组利用具有典型 express/router 模式的 Express 的端点。

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

可用

我还可以使用 Postman 等工具或 Karate 等自动化工具成功测试这些端点。

我遇到的问题是,在通过 http://localhost:8080.

执行产品源 JS 时,我似乎无法使用 Istanbul 收集代码覆盖率

我试过 npm start 然后 nyc --all src/**/*.js gradlew test。后者是测试端点的自动化。这导致 0% 的覆盖率,我假设这是由于没有 运行ning nyc with npm start 造成的。

接下来我尝试 nyc --all src/**/*.js npm start 并注意到一些覆盖,但这只是启动 Express 服务器的覆盖。

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

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

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

接下来,我尝试了上述所有尝试,将它们包装到 package.json 脚本中,然后 运行ning npm run <scriptName> 得到完全相同的行为。

最后我尝试了 nyc instrument src instrumented/src --compact=false,然后是 npm run start:coverage,其中这个 start:coverage 脚本在 node ./instrumented/src/index.js 调用了检测的 index.js,然后是 gradlew test,然后是 gradlew test通过 nyc report --reporter=lcov。此尝试也未能从 gradlew 端点测试中产生任何额外的代码覆盖率。

我在网上做了一些研究,发现了这个 post How do I setup code coverage on my Express based API?

并认为这看起来与我的问题非常相似。例如,伊斯坦布尔在通过执行端点执行代码时不知道如何覆盖代码。

我决定还是post这个,因为上面的post有点老了,想征求意见,看看有没有更好的解决方案 https://github.com/gotwarlost/istanbul-middleware

编辑

添加更多关于我们如何在今天没有伊斯坦布尔的情况下启动 Express 服务器和 运行 自动化的细节。只是为了阐明我们正在使用什么以及我们投资的自动化工具。(主要是空手道和 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

如评论中所述,您永远不会启动服务器以 [​​=51=] 测试...当您 require 服务器文件时,测试将指向您的服务器。

在我的示例中,我正在 运行ning mocha with chai and the chai-http 包帮助调用服务器

server.js

const app = require("express")();

// everything else ...

exports.server = app;

在端到端测试中,您可以轻松拥有:

const chai = require('chai');
const chaiHttp = require('chai-http');
chai.use(chaiHttp);

const server = require("./server.js").server;

...

it("should calculate the circumference", done => {
    chai
       .request(server) // <-- attach your server here
       .get('/v1/circumference/10')
       .end((err, res) => {
         expect(res.status).to.be.eql(200);
         expect(res.type).to.be.eql('application/json');
         expect(res.body.result).to.eql(62.83185307179586);
         done();
       });
    });
});

我做了一个非常简单的项目并推送到 GitHub 这样你就可以检查并 运行 所有的东西,看看它们是如何协同工作的

GitHub Project


已添加

我添加了一条路由以便它可以显示覆盖率报告(我使用了 html report)并创建了一条指向它的静态路由...

当您 运行 覆盖范围 npm run coverage 时,它将在 ./report 文件夹内生成报告,并且指向该文件夹的简单快速路由将使人们能够将其视为端点.

commit info 这样的变化

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

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