From e991f834e25305574ca6fa58ca96f22d8d366105 Mon Sep 17 00:00:00 2001 From: jubnl Date: Tue, 7 Apr 2026 23:18:22 +0200 Subject: [PATCH] fix(tests): replace URL.createObjectURL mocking with vi.stubGlobal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Direct property assignment and Object.defineProperty both fail silently on CI when jsdom marks URL.createObjectURL as non-writable and non-configurable. vi.stubGlobal('URL', ...) replaces globalThis.URL entirely — which always succeeds — while extending the real URL class so all URL parsing behaviour is preserved. vi.unstubAllGlobals() is called at the start of beforeEach to reset cleanly between tests. --- client/tests/unit/api/authUrl.test.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/client/tests/unit/api/authUrl.test.ts b/client/tests/unit/api/authUrl.test.ts index 16e3aead..c0906b2b 100644 --- a/client/tests/unit/api/authUrl.test.ts +++ b/client/tests/unit/api/authUrl.test.ts @@ -9,12 +9,18 @@ const flushPromises = () => new Promise(r => setTimeout(r, 10)); beforeEach(() => { clearImageQueue(); vi.restoreAllMocks(); // restore any vi.spyOn() wrappers from the previous test + vi.unstubAllGlobals(); // restore any vi.stubGlobal() replacements from the previous test - // jsdom's URL.createObjectURL returns '' and may be non-configurable, so - // Object.defineProperty in setup.ts can fail silently on CI. Assign directly - // here (after restoreAllMocks) so every test in this file gets a fresh mock. - URL.createObjectURL = vi.fn(() => 'blob:mock') as typeof URL.createObjectURL; - URL.revokeObjectURL = vi.fn() as typeof URL.revokeObjectURL; + // jsdom's URL.createObjectURL is non-writable/non-configurable in some CI + // environments — direct assignment and Object.defineProperty both fail + // silently. vi.stubGlobal replaces globalThis.URL entirely, which always + // works. We extend the real URL so all URL parsing behaviour is preserved. + const OriginalURL = URL; + class TestURL extends OriginalURL { + static createObjectURL = vi.fn(() => 'blob:mock'); + static revokeObjectURL = vi.fn(); + } + vi.stubGlobal('URL', TestURL); }); // ── getAuthUrl ─────────────────────────────────────────────────────────────────