Skip to content

Commit b4984d1

Browse files
committed
fix(test): ensure useEncryption hook and test share same crypto module instance
In Vitest's jsdom environment, a static import inside a hook (captured at module evaluation time) and a dynamic import() inside a test body can resolve to different module instances. When vi.stubGlobal patches crypto after static imports have already been evaluated, the hook's closed-over decryptContent has a stale crypto reference (no .subtle), while the test's dynamic import() gets the patched one. Fix: use vi.mock + vi.importActual so both the test's enc() helper and the hook's internal decryptContent are guaranteed to come from the same module instance, with the same crypto reference.
1 parent 7353085 commit b4984d1

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

src/__tests__/useEncryption.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ import { renderHook, act } from '@testing-library/react'
33
import { useEncryption } from '@/hooks/useEncryption'
44
import type { DecryptedNote } from '@/types/note'
55

6+
// Hoist the actual implementation so both the test helpers and the hook's
7+
// internal static import share the same module instance and the same
8+
// crypto.subtle reference — avoiding stale-closure issues in jsdom.
9+
vi.mock('@/lib/crypto', async () => {
10+
const actual = await vi.importActual<typeof import('@/lib/crypto')>('@/lib/crypto')
11+
return actual
12+
})
13+
614
function makeNote(overrides: Partial<DecryptedNote> = {}): DecryptedNote {
715
return {
816
id: 'note-1',

0 commit comments

Comments
 (0)