diff --git a/packages/domscribe-nuxt/src/runtime/plugin.spec.ts b/packages/domscribe-nuxt/src/runtime/plugin.spec.ts index 759da7d..4b5829b 100644 --- a/packages/domscribe-nuxt/src/runtime/plugin.spec.ts +++ b/packages/domscribe-nuxt/src/runtime/plugin.spec.ts @@ -98,6 +98,44 @@ describe('runtime/plugin', () => { }); }); + it('should wait for runtime initialization before initializing the overlay', async () => { + // Arrange + let resolveInitialize: (() => void) | undefined; + mockInitialize.mockReturnValueOnce( + new Promise((resolve) => { + resolveInitialize = resolve; + }), + ); + (globalThis as unknown as Record)[ + '__DOMSCRIBE_OVERLAY_OPTIONS__' + ] = {}; + + // Act + const pluginPromise = getPluginFn()(); + await vi.dynamicImportSettled(); + + // Assert + expect(mockInitOverlay).not.toHaveBeenCalled(); + + resolveInitialize?.(); + await pluginPromise; + expect(mockInitOverlay).toHaveBeenCalled(); + }); + + it('should skip overlay initialization when runtime initialization fails', async () => { + // Arrange + mockInitialize.mockRejectedValueOnce(new Error('runtime broken')); + (globalThis as unknown as Record)[ + '__DOMSCRIBE_OVERLAY_OPTIONS__' + ] = {}; + + // Act + await getPluginFn()(); + + // Assert + expect(mockInitOverlay).not.toHaveBeenCalled(); + }); + it('should initialize overlay when __DOMSCRIBE_OVERLAY_OPTIONS__ is set', async () => { (globalThis as unknown as Record)[ '__DOMSCRIBE_OVERLAY_OPTIONS__' diff --git a/packages/domscribe-nuxt/src/runtime/plugin.ts b/packages/domscribe-nuxt/src/runtime/plugin.ts index 54d96ef..8fa8174 100644 --- a/packages/domscribe-nuxt/src/runtime/plugin.ts +++ b/packages/domscribe-nuxt/src/runtime/plugin.ts @@ -15,10 +15,18 @@ export default defineNuxtPlugin(async () => { const runtimeOptions = (win['__DOMSCRIBE_RUNTIME_OPTIONS__'] as Record) ?? {}; - RuntimeManager.getInstance().initialize({ - ...runtimeOptions, - adapter: createVueAdapter({}), - }); + try { + await RuntimeManager.getInstance().initialize({ + ...runtimeOptions, + adapter: createVueAdapter({}), + }); + } catch (e) { + console.warn( + '[domscribe/nuxt] Failed to initialize runtime:', + e instanceof Error ? e.message : String(e), + ); + return; + } // Initialize overlay if options were injected by the module's head script if (window.__DOMSCRIBE_OVERLAY_OPTIONS__) {