histoire
Version:
A fast story book powered by Vite
22 lines • 3 kB
TypeScript
/**
* Generated-code helpers installed around Vitest's `ModuleMocker`, embedded
* verbatim by both browser runtimes (the story preview iframe and the app
* shell): they do the same job in two windows, so they are emitted from one
* source instead of being kept in sync by hand.
*
* - `registerNativeFactoryResolver` answers the mocker's factory-resolution
* requests over the HMR channel,
* - `enableManualMockPreload` resolves `__mocks__` files eagerly, so a module
* mocked with a factory is already registered when the story imports it.
*/
export declare const MOCKER_HELPERS_SNIPPET = "function registerNativeFactoryResolver(mocker) {\n const hot = import.meta.hot\n if (!hot) {\n return\n }\n\n hot.on('vitest:interceptor:resolve', async url => {\n const exports = await mocker.resolveFactoryModule(url)\n hot.send('vitest:interceptor:resolved', {\n url,\n keys: Object.keys(exports),\n })\n })\n}\n\nfunction enableManualMockPreload(mocker) {\n const originalQueueMock = mocker.queueMock.bind(mocker)\n const originalPrepare = mocker.prepare.bind(mocker)\n const preloads = new Set()\n let preloadingFactories = 0\n\n // Dynamic imports wait for prepare(), i.e. the whole mock queue. A factory's\n // own imports (vi.mock(id, async () => { await import('vue') })) run while\n // its preload is queued: waiting for preloads there deadlocks, so they only\n // wait for mock registrations, as in Vitest.\n mocker.prepare = () => preloadingFactories\n ? Promise.all(Array.from(mocker.queue).filter(promise => !preloads.has(promise)))\n : originalPrepare()\n\n mocker.queueMock = (rawId, importer, factoryOrOptions) => {\n originalQueueMock(rawId, importer, factoryOrOptions)\n\n if (typeof factoryOrOptions !== 'function') {\n return\n }\n\n const pendingRegistration = Array.from(mocker.queue).at(-1)\n if (!pendingRegistration) {\n return\n }\n\n const preloadPromise = pendingRegistration.then(async () => {\n for (const mockUrl of mocker.registry.keys()) {\n const mock = mocker.registry.get(mockUrl)\n if (mock?.type === 'manual' && !mock.cache) {\n preloadingFactories++\n try {\n await mock.resolve()\n }\n finally {\n preloadingFactories--\n }\n }\n }\n }).finally(() => {\n mocker.queue.delete(preloadPromise)\n preloads.delete(preloadPromise)\n })\n\n preloads.add(preloadPromise)\n mocker.queue.add(preloadPromise)\n }\n}";
/**
* Generated expression creating the dev-mode `ModuleMocker`: mock resolution
* goes through Histoire's own HTTP RPC (see `mock-rpc-snippet.ts`) rather than
* Vitest's HMR-based transport.
*
* Expects `ModuleMocker`, `createMockInstance`, `createMockInterceptor` and
* `mockRpc` to be in scope, and to be evaluated under `import.meta.hot`.
*/
export declare const DEV_MODULE_MOCKER_SNIPPET: string;
//# sourceMappingURL=mocker-snippet.d.ts.map