UNPKG

@dbs-portal/tool-mock

Version:

API mocking toolkit using MSW for DBS Portal development workflows

96 lines (88 loc) 3.2 kB
/** * Testing setup utilities for MSW - thin integration layer with Jest/Vitest */ /** * Global test server instance */ let globalTestServer = null; /** * Setup MSW for Jest - follows MSW's official Jest integration pattern * This function should be called in your Jest setup file (e.g., setupTests.js) */ export async function setupMSWJest(handlers = [], _config = {}) { // Dynamic import to avoid bundling Node.js code in browser builds const { setupServer } = await import('msw/node'); const server = setupServer(...handlers); globalTestServer = server; global.__MSW_SERVER__ = server; return server; } /** * Setup MSW for Vitest - follows Vitest's testing patterns * This function should be called in your Vitest setup file */ export async function setupMSWVitest(handlers = [], _config = {}) { // Dynamic import to avoid bundling Node.js code in browser builds const { setupServer } = await import('msw/node'); const server = setupServer(...handlers); globalTestServer = server; // Make server available globally for Vitest tests if (typeof globalThis !== 'undefined') { ; globalThis.__MSW_SERVER__ = server; } return server; } /** * Get global test server - simple accessor for Jest/Vitest tests */ export function getGlobalTestServer() { return globalTestServer; } /** * Check if MSW is setup for testing */ export function isMSWSetupForTesting() { return globalTestServer !== null; } /** * Generate Jest setup file content for MSW */ export function generateJestSetupFile(handlers = []) { const handlerImports = handlers.map(handler => `import { ${handler} } from './mocks/${handler}';`).join('\n'); const allHandlers = handlers.length > 0 ? `[${handlers.join(', ')}]` : '[]'; return ` import { setupMSWJest } from '@dbs-portal/tool-mock/testing'; ${handlerImports} // Setup MSW for Jest following official MSW + Jest integration const server = setupMSWJest(${allHandlers}); // Establish API mocking before all tests beforeAll(() => server.listen({ onUnhandledRequest: 'error' })); // Reset any request handlers that we may add during the tests, // so they don't affect other tests afterEach(() => server.resetHandlers()); // Clean up after the tests are finished afterAll(() => server.close()); `; } /** * Generate Vitest setup file content for MSW */ export function generateVitestSetupFile(handlers = []) { const handlerImports = handlers.map(handler => `import { ${handler} } from './mocks/${handler}';`).join('\n'); const allHandlers = handlers.length > 0 ? `[${handlers.join(', ')}]` : '[]'; return ` import { setupMSWVitest } from '@dbs-portal/tool-mock/testing'; ${handlerImports} // Setup MSW for Vitest following official MSW + Vitest integration const server = setupMSWVitest(${allHandlers}); // Establish API mocking before all tests beforeAll(() => server.listen({ onUnhandledRequest: 'error' })); // Reset any request handlers that we may add during the tests, // so they don't affect other tests afterEach(() => server.resetHandlers()); // Clean up after the tests are finished afterAll(() => server.close()); `; } //# sourceMappingURL=setup.js.map