test: guard utility mock call helpers

This commit is contained in:
Peter Steinberger
2026-05-11 23:20:17 +01:00
parent ccce9aa417
commit d3edbaabcb
4 changed files with 24 additions and 4 deletions

View File

@@ -59,6 +59,14 @@ vi.mock("./random-token.js", () => ({
randomToken: randomTokenMock,
}));
function firstReplaceConfigRequest(): unknown {
const [call] = replaceConfigFileMock.mock.calls;
if (!call) {
throw new Error("expected config replace call");
}
return call[0];
}
describe("resolveGatewayInstallToken", () => {
beforeEach(() => {
vi.clearAllMocks();
@@ -193,7 +201,7 @@ describe("resolveGatewayInstallToken", () => {
expect(result.warnings.join("\n")).toContain("saving to config");
expect(replaceConfigFileMock).toHaveBeenCalledOnce();
expect(replaceConfigFileMock.mock.calls[0]?.[0]).toStrictEqual({
expect(firstReplaceConfigRequest()).toStrictEqual({
nextConfig: {
gateway: {
auth: {

View File

@@ -21,7 +21,11 @@ function createRuntime(): RuntimeEnv {
}
function readJsonLog(runtime: RuntimeEnv): unknown {
return JSON.parse(String(vi.mocked(runtime.log).mock.calls[0]?.[0]));
const [call] = vi.mocked(runtime.log).mock.calls;
if (!call) {
throw new Error("expected runtime log call");
}
return JSON.parse(String(call[0]));
}
async function withTaskJsonStateDir(run: () => Promise<void>): Promise<void> {

View File

@@ -62,7 +62,11 @@ const createWritableStreamMock = () => {
};
function requireFirstWrite(write: ReturnType<typeof vi.fn>): string {
const value = write.mock.calls[0]?.[0];
const [call] = write.mock.calls;
if (!call) {
throw new Error("expected systemd status write");
}
const [value] = call;
if (value === undefined) {
throw new Error("expected systemd status write");
}

View File

@@ -2,7 +2,11 @@ import { describe, expect, it, vi } from "vitest";
import { fireAndForgetBoundedHook, fireAndForgetHook } from "./fire-and-forget.js";
function requireFirstLog(logger: ReturnType<typeof vi.fn>): string {
const message = logger.mock.calls[0]?.[0];
const [call] = logger.mock.calls;
if (!call) {
throw new Error("expected log call");
}
const [message] = call;
if (typeof message !== "string") {
throw new Error("expected string log message");
}