test: add unit test for dvisvgm.ts

This commit is contained in:
Jörg Krzeslak
2025-07-24 11:40:11 +02:00
parent 3975c70de7
commit c47c1dd4f4
5 changed files with 99 additions and 5 deletions

View File

@@ -1,4 +1,5 @@
import { execFile } from "node:child_process"; import { execFile as execFileOriginal } from "node:child_process";
import { ExecFileFn } from "./types.ts";
export const properties = { export const properties = {
from: { from: {
@@ -14,8 +15,8 @@ export function convert(
fileType: string, fileType: string,
convertTo: string, convertTo: string,
targetPath: string, targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown, options?: unknown,
execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> { ): Promise<string> {
const inputArgs: string[] = []; const inputArgs: string[] = [];
if (fileType === "eps") { if (fileType === "eps") {

View File

@@ -4,7 +4,7 @@ import {
runConvertFailTest, runConvertFailTest,
runConvertLogsStderror, runConvertLogsStderror,
runConvertSuccessTest, runConvertSuccessTest,
} from "./convertersHelper.test.ts"; } from "./helpers/converters.ts";
test("convert resolves when execFile succeeds", async () => { test("convert resolves when execFile succeeds", async () => {
await runConvertSuccessTest(convert); await runConvertSuccessTest(convert);

View File

@@ -4,7 +4,7 @@ import {
runConvertFailTest, runConvertFailTest,
runConvertLogsStderror, runConvertLogsStderror,
runConvertSuccessTest, runConvertSuccessTest,
} from "./convertersHelper.test.ts"; } from "./helpers/converters.ts";
test("convert resolves when execFile succeeds", async () => { test("convert resolves when execFile succeeds", async () => {
await runConvertSuccessTest(convert); await runConvertSuccessTest(convert);

View File

@@ -0,0 +1,93 @@
import type { ExecFileException } from "node:child_process";
import { expect, test } from "bun:test";
import { convert } from "../../src/converters/dvisvgm.ts";
import { ExecFileFn } from "../../src/converters/types.ts";
import {
runConvertFailTest,
runConvertLogsStderror,
runConvertSuccessTest,
} from "./helpers/converters.ts";
test("convert resolves when execFile succeeds", async () => {
await runConvertSuccessTest(convert);
});
test("convert rejects when execFile fails", async () => {
await runConvertFailTest(convert);
});
test("convert logs stderr when present", async () => {
await runConvertLogsStderror(convert);
});
test("convert respects eps filetype", async () => {
const originalConsoleLog = console.log;
let loggedMessage = "";
console.log = (msg) => {
loggedMessage = msg;
};
const mockExecFile: ExecFileFn = (
_cmd: string,
_args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => {
callback(null, "Fake stdout", "");
};
const result = await convert("input.obj", "eps", "stl", "output.stl", undefined, mockExecFile);
console.log = originalConsoleLog;
expect(result).toBe("Done");
expect(loggedMessage).toBe("stdout: Fake stdout");
});
test("convert respects pdf filetype", async () => {
const originalConsoleLog = console.log;
let loggedMessage = "";
console.log = (msg) => {
loggedMessage = msg;
};
const mockExecFile: ExecFileFn = (
_cmd: string,
_args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => {
callback(null, "Fake stdout", "");
};
const result = await convert("input.obj", "pdf", "stl", "output.stl", undefined, mockExecFile);
console.log = originalConsoleLog;
expect(result).toBe("Done");
expect(loggedMessage).toBe("stdout: Fake stdout");
});
test("convert respects svgz conversion target type", async () => {
const originalConsoleLog = console.log;
let loggedMessage = "";
console.log = (msg) => {
loggedMessage = msg;
};
const mockExecFile: ExecFileFn = (
_cmd: string,
_args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => {
callback(null, "Fake stdout", "");
};
const result = await convert("input.obj", "eps", "svgz", "output.stl", undefined, mockExecFile);
console.log = originalConsoleLog;
expect(result).toBe("Done");
expect(loggedMessage).toBe("stdout: Fake stdout");
});

View File

@@ -1,6 +1,6 @@
import type { ExecFileException } from "node:child_process"; import type { ExecFileException } from "node:child_process";
import { expect } from "bun:test"; import { expect } from "bun:test";
import { ConvertFnWithExecFile, ExecFileFn } from "../../src/converters/types.ts"; import { ConvertFnWithExecFile, ExecFileFn } from "../../../src/converters/types.ts";
export async function runConvertSuccessTest(convertFn: ConvertFnWithExecFile) { export async function runConvertSuccessTest(convertFn: ConvertFnWithExecFile) {
const originalConsoleLog = console.log; const originalConsoleLog = console.log;