test: add convert string body branch coverage tests
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 19m48s
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 19m48s
This commit is contained in:
parent
e0c4214e53
commit
6ce773a071
1 changed files with 55 additions and 0 deletions
55
src/__tests__/convert-string-body.test.ts
Normal file
55
src/__tests__/convert-string-body.test.ts
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||||
|
import express from "express";
|
||||||
|
import request from "supertest";
|
||||||
|
|
||||||
|
vi.mock("node:dns/promises", () => ({
|
||||||
|
default: { lookup: vi.fn() },
|
||||||
|
lookup: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
let app: express.Express;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
vi.resetModules();
|
||||||
|
|
||||||
|
const { renderPdf } = await import("../services/browser.js");
|
||||||
|
vi.mocked(renderPdf).mockResolvedValue({ pdf: Buffer.from("%PDF-1.4 mock"), durationMs: 10 });
|
||||||
|
|
||||||
|
const { convertRouter } = await import("../routes/convert.js");
|
||||||
|
app = express();
|
||||||
|
// Parse application/json as text to produce a string req.body,
|
||||||
|
// exercising the typeof === "string" branches in convert routes.
|
||||||
|
app.use(express.text({ type: "application/json", limit: "500kb" }));
|
||||||
|
app.use("/v1/convert", convertRouter);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("convert routes handle string body (branch coverage)", () => {
|
||||||
|
it("POST /v1/convert/html with string body parses it as HTML", async () => {
|
||||||
|
const { renderPdf } = await import("../services/browser.js");
|
||||||
|
|
||||||
|
const res = await request(app)
|
||||||
|
.post("/v1/convert/html")
|
||||||
|
.set("content-type", "application/json")
|
||||||
|
.send("<h1>Hello</h1>");
|
||||||
|
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(vi.mocked(renderPdf)).toHaveBeenCalledOnce();
|
||||||
|
const html = vi.mocked(renderPdf).mock.calls[0]![0] as string;
|
||||||
|
expect(html).toContain("<h1>Hello</h1>");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("POST /v1/convert/markdown with string body parses it as markdown", async () => {
|
||||||
|
const { renderPdf } = await import("../services/browser.js");
|
||||||
|
|
||||||
|
const res = await request(app)
|
||||||
|
.post("/v1/convert/markdown")
|
||||||
|
.set("content-type", "application/json")
|
||||||
|
.send("# Hello");
|
||||||
|
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(vi.mocked(renderPdf)).toHaveBeenCalledOnce();
|
||||||
|
const html = vi.mocked(renderPdf).mock.calls[0]![0] as string;
|
||||||
|
expect(html).toContain("Hello");
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue