From 976ee74afc6ee6f1db07968636871d61140a0777 Mon Sep 17 00:00:00 2001 From: QSchlegel Date: Tue, 9 Jun 2026 21:37:37 +0200 Subject: [PATCH] fix(test): reset module registry in freeUtxos bot test for isolation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When freeUtxos.bot.test.ts runs in the full suite, three cases that exercise the canonical-scriptCbor fallback in resolveWalletScriptAddress fail intermittently — decodeNativeScriptFromCbor is never observed as called and the error path produces "unknown error" instead of the mocked message. In isolation the same tests pass. Root cause: nativeScriptUtils.test.ts imports the real @/utils/nativeScriptUtils (no mock), caching the module in Jest's registry. By the time this suite declares its jest.mock(..., {virtual: true}) replacement, the bindings inside the transitively imported @/lib/server/walletScriptAddress are already wired to the real implementation, so the mocks never fire. jest.resetModules() in beforeAll forces the dynamic handler import to walk a fresh module graph that honours this suite's mocks. Verified with `npx jest` (full suite): 357 pass / 0 fail / 2 skipped. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/__tests__/freeUtxos.bot.test.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/__tests__/freeUtxos.bot.test.ts b/src/__tests__/freeUtxos.bot.test.ts index 62a7407b..71638d0d 100644 --- a/src/__tests__/freeUtxos.bot.test.ts +++ b/src/__tests__/freeUtxos.bot.test.ts @@ -97,6 +97,14 @@ jest.mock("@/lib/security/rateLimit", () => ({ let handler: (req: NextApiRequest, res: NextApiResponse) => Promise; beforeAll(async () => { + // Other suites (notably nativeScriptUtils.test.ts) import the real + // @/utils/nativeScriptUtils and @/utils/common before this file runs; + // Jest caches those modules and the jest.mock(..., {virtual:true}) + // declarations above don't re-wire the bindings inside transitively + // imported files like @/lib/server/walletScriptAddress. Resetting the + // registry forces the dynamic handler import below to pick up our + // mocked versions instead of the cached reals. + jest.resetModules(); ({ default: handler } = await import("../pages/api/v1/freeUtxos")); });