From 8d1e734213ef9dda1b0582883e05e4dcc20c4494 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 20 Apr 2026 22:16:07 +0100 Subject: [PATCH] test: share cron store rename spy helper --- src/cron/store.test.ts | 47 +++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/cron/store.test.ts b/src/cron/store.test.ts index 381ce0dfd1c..4e7724d0291 100644 --- a/src/cron/store.test.ts +++ b/src/cron/store.test.ts @@ -48,6 +48,23 @@ function makeStore(jobId: string, enabled: boolean): CronStoreFile { }; } +async function captureRenameDestinations(action: () => Promise): Promise { + const renamedDestinations: string[] = []; + const origRename = fs.rename.bind(fs); + const spy = vi.spyOn(fs, "rename").mockImplementation(async (src, dest) => { + renamedDestinations.push(String(dest)); + return origRename(src, dest); + }); + + try { + await action(); + } finally { + spy.mockRestore(); + } + + return renamedDestinations; +} + describe("resolveCronStorePath", () => { afterEach(() => { vi.unstubAllEnvs(); @@ -216,18 +233,9 @@ describe("cron store", () => { const configRawBefore = await fs.readFile(store.storePath, "utf-8"); await fs.rm(statePath); - const renamedDestinations: string[] = []; - const origRename = fs.rename.bind(fs); - const spy = vi.spyOn(fs, "rename").mockImplementation(async (src, dest) => { - renamedDestinations.push(String(dest)); - return origRename(src, dest); - }); - - try { - await saveCronStore(store.storePath, payload); - } finally { - spy.mockRestore(); - } + const renamedDestinations = await captureRenameDestinations(() => + saveCronStore(store.storePath, payload), + ); const configRawAfter = await fs.readFile(store.storePath, "utf-8"); const stateFile = JSON.parse(await fs.readFile(statePath, "utf-8")); @@ -249,18 +257,9 @@ describe("cron store", () => { const stateRawBefore = await fs.readFile(statePath, "utf-8"); await fs.rm(store.storePath); - const renamedDestinations: string[] = []; - const origRename = fs.rename.bind(fs); - const spy = vi.spyOn(fs, "rename").mockImplementation(async (src, dest) => { - renamedDestinations.push(String(dest)); - return origRename(src, dest); - }); - - try { - await saveCronStore(store.storePath, payload); - } finally { - spy.mockRestore(); - } + const renamedDestinations = await captureRenameDestinations(() => + saveCronStore(store.storePath, payload), + ); const config = JSON.parse(await fs.readFile(store.storePath, "utf-8")); const stateRawAfter = await fs.readFile(statePath, "utf-8");