Skip to content

fix(test): jest.resetModules in freeUtxos bot test to clear cached real impl#270

Open
QSchlegel wants to merge 1 commit into
preprodfrom
claude/fix-freeutxos-test-isolation
Open

fix(test): jest.resetModules in freeUtxos bot test to clear cached real impl#270
QSchlegel wants to merge 1 commit into
preprodfrom
claude/fix-freeutxos-test-isolation

Conversation

@QSchlegel

Copy link
Copy Markdown
Collaborator

Why

The `checks` job on #229 failed with 3 flakes in `src/tests/freeUtxos.bot.test.ts`:

  • `falls back to canonical scriptCbor when multisig wallet is unavailable` — `decodeNativeScriptFromCbor` never observed as called
  • `returns clear 500 when canonical script fallback cannot decode` — error message is "unknown error" instead of the mocked "invalid canonical cbor"

In isolation (`jest src/tests/freeUtxos.bot.test.ts`) all 4 tests pass. In the full suite, those 3 fail.

Root cause

`src/tests/nativeScriptUtils.test.ts` imports the real `@/utils/nativeScriptUtils` (no mock) and Jest caches it in the module registry. When `freeUtxos.bot.test.ts` runs later, its top-level `jest.mock("@/utils/nativeScriptUtils", () => …, { virtual: true })` doesn't re-wire the bindings inside the transitively imported `@/lib/server/walletScriptAddress` — those bindings already point at the real implementation. The mocks never fire, the real `decodeNativeScriptFromCbor` is invoked on the test fixture `"canonical-cbor"`, throws a non-Error, and the handler returns "unknown error".

Fix

A single `jest.resetModules()` in `beforeAll` before the dynamic handler import forces a fresh module graph that honors this suite's mocks.

Verification

```
$ npx jest
Test Suites: 1 skipped, 46 passed, 46 of 47 total
Tests: 2 skipped, 357 passed, 359 total
```
(was 1 failed / 3 tests failing before the fix)

🤖 Generated with Claude Code

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) <noreply@anthropic.com>
@vercel

vercel Bot commented Jun 9, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
multisig Ready Ready Preview, Comment Jun 9, 2026 7:42pm

Request Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant