Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/domscribe-nuxt/src/runtime/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>((resolve) => {
resolveInitialize = resolve;
}),
);
(globalThis as unknown as Record<string, unknown>)[
'__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<string, unknown>)[
'__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<string, unknown>)[
'__DOMSCRIBE_OVERLAY_OPTIONS__'
Expand Down
16 changes: 12 additions & 4 deletions packages/domscribe-nuxt/src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ export default defineNuxtPlugin(async () => {
const runtimeOptions =
(win['__DOMSCRIBE_RUNTIME_OPTIONS__'] as Record<string, unknown>) ?? {};

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__) {
Expand Down