使用打字稿编写的使用NYC / Istanbul的Fastize集成测试没有代码覆盖率

我目前正在尝试使用mocha和NYC在我的固定路线上进行代码覆盖。

我已经尝试过预先检测代码,然后对检测到的代码运行测试,以及尝试以各种方式设置NYC以使其正常工作。

这是我当前的配置。先前所有代码都产生了相同的代码覆盖率输出):

nyc配置

"nyc": {
  "extends": "@istanbuljs/nyc-config-typescript","extension": [
        ".ts",".tsx"
    ],"exclude": [
        "**/*.d.ts","**/*.test.ts"
    ],"reporter": [
        "html","text"      
    ],"sourceMap": true,"instrument": true
}

路由文件:

const routes = async (app: FastifyInstance,options) => {

  app.post('/code',async (request: FastifyRequest,response: FastifyReply<ServerResponse>) => {
    // route logic in here
  });
};

集成测试:

import * as fastify from fastify;
import * as sinon from 'sinon';
import * as chai from 'chai';

const expect = chai.expect;
const sinonChai = require('sinon-chai');
chai.use(sinonChai);

describe('When/code POST is called',() => {
  let app;

  before(() => {
    app = fastify();

    // load routes for integration testing
    app.register(require('../path/to/code.ts'));
  });
  after(() => {
    app.close();
  });

  it('then a code is created and returned',async () => {
    const {statusCode} = await apiTester.inject({
      url: '/code',method: 'POST',payload:{ code: 'fake_code' }
    });
    expect(statusCode).to.equal(201);
  });
});

我的单元测试调用如下所示:

nyc mocha './test/unit/**/*.test.ts' --require ts-node/register --require source-map-support/register --recursive

我实际上只为const routes =获得5%的代码覆盖率。我真的在想办法解决这个问题。任何帮助将不胜感激!我在这里研究过的其他解决方案都没有。

lvyinkiller 回答:使用打字稿编写的使用NYC / Istanbul的Fastize集成测试没有代码覆盖率

我有一个有关打字稿+摩卡咖啡+纽约市的详细示例。它还包含固定测试,包括路由测试(注入)以及使用sinon的模拟+存根和间谍测试。所有异步也都在等待。

它使用了所有现代版本,还涵盖了未使用的文件以及VsCode启动配置。随时在这里查看:

https://github.com/Flowkap/typescript-node-template

我特别认为

instrumentation: true

弄乱了结果。这是我的工作.nycrc.yml

extends: "@istanbuljs/nyc-config-typescript"

reporter:
  - html
  - lcovonly
  - clover
  # those 2 are for commandline outputs
  - text
  - text-summary
report-dir: coverage

在上述示例中,即使是模拟的ans浴缸部分,我也有适当的报道。

本文链接:https://www.f2er.com/2625410.html

大家都在问