我目前正在尝试使用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%的代码覆盖率。我真的在想办法解决这个问题。任何帮助将不胜感激!我在这里研究过的其他解决方案都没有。