我在 TypeScript 中有一个 HapiJS 项目,想开始添加一些单元测试。现在代码很简单:
服务器.ts
import * as Hapi from "@hapi/hapi";
import routes from "./routes";
const server = new Hapi.Server({
port: 80,
host: "0.0.0.0",
debug: {
request: ["error"]
}
});
let serverSetup = false;
const setupServer = async () =>
{
if (serverSetup) return;
await server.register([routes]);
serverSetup = true;
};
export const init = async () =>
{
await setupServer();
await server.initialize();
return server;
};
export const start = async () =>
{
await setupServer();
await server.start();
console.log(`Server running at: ${server.info.uri}`);
return server;
};
路线.ts:
import * as Hapi from "@hapi/hapi";
export default {
name: "RouteIndex",
version: "1.0.0",
register: function (server:Hapi.Server, options:Hapi.ServerRegisterOptions)
{
server.route({
method: "GET",
path: "/",
handler: (request, h) =>
{
return {message: "Hello, World!"};
}
});
server.route({
method: "*",
path: "/{any*}",
handler: (request, h) =>
{
return "404!";
}
});
}
};
然后我的单元测试:
server.test.js
const Lab = require("@hapi/lab");
const { expect } = require("@hapi/code");
const { afterEach, beforeEach, describe, it } = exports.lab = Lab.script();
const { init, start } = require("../src/server");
const HTTP_PORT = 80;
const HTTP_STATUS_OK = 200;
describe("General Server Tests", () =>
{
let server;
beforeEach(async () =>
{
server = await init();
});
afterEach(async () =>
{
await server.stop();
});
it("Starts successfully", async () =>
{
server = await start();
expect(server.type).to.equal("tcp");
expect(server.settings.port).to.equal(HTTP_PORT);
expect(server.settings.host).to.equal("0.0.0.0");
});
it("Responds to GET requests", async () =>
{
const res = await server.inject({
method: "GET",
url: "/"
});
expect(res.statusCode).to.equal(HTTP_STATUS_OK);
});
});
要运行我的测试,我的 package.json 中有以下内容:
{
// ...
"scripts": {
// ...
"test": "lab -vclS -T node_modules/lab-transform-typescript **/*.test.js",
// ...
}
// ...
}
-v
添加标志是因为我更喜欢详细输出- 添加了
-c
标志,因此我可以获得代码覆盖率报告,这就是我的问题所在 - 必须添加该
-l
标志,否则我会收到错误消息:The following leaks were detected:__extends, __assign, __rest, __decorate, __param, __metadata, __awaiter, __generator,...
(TypeScript 编译器创建的所有全局变量) - 该
-S
标志是根据lab-transform-typescript 文档(-S
==--sourcemaps
)添加的 - 标志
-T
是加载(==lab-transform-typescript
)-T
--transform
当我运行测试时,结果如下:
stevenbarnett@MacBook-Pro hapi-test % npm run test
> hapi-test@1.0.0 test /Users/stevenbarnett/Repos/hapi-test
> lab -vclS -T node_modules/lab-transform-typescript **/*.test.js
Server running at: http://0.0.0.0:80
General Server Tests
✔ 1) Starts successfully (4 ms)
✔ 2) Responds to GET requests (7 ms)
3 tests complete
Test duration: 111 ms
Coverage: 71.50% (61/214)
src/server.ts missing coverage from file(s):
null on line(s): , , , , , , , , , , , , , , , , , ,
src/routes.ts missing coverage from file(s):
null on line(s): , , , , , , , , , , , , , , , , , , , , ,
src/routes/index.ts on line(s): 24
所以我知道src/routes/index.ts
第 24 行缺少覆盖(这是 404 错误,我没有测试 404)——但其他行是荒谬的:
null on line(s): , , , , , , , , , , , , , , , , , ,
为什么会发生这种情况,我该如何解决?