我目前正在尝试使用 Mocha 和 NYC 在我的 fastify 路线上获得代码覆盖率。
我已经尝试预先检测代码,然后对检测代码运行测试,以及尝试以各种方式设置 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
我真的得到了 5% 的代码覆盖率const routes =
。我真的很想弄清楚这一点。任何帮助将不胜感激!我在这里调查过的其他解决方案都不起作用。