|
| 1 | +import { parentPort } from 'node:worker_threads'; |
| 2 | +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; |
| 3 | + |
| 4 | +import type { FederationConfig } from '../../models'; |
| 5 | +import { compileTypes } from '../compileTypes'; |
| 6 | +import type { CompileTypesWorkerMessage, ExitMessage } from '../compileTypesWorker'; |
| 7 | +import { rewritePathsWithExposedFederatedModules } from '../rewritePathsWithExposedFederatedModules'; |
| 8 | +import { workerLogger } from '../workerLogger'; |
| 9 | + |
| 10 | +vi.mock('node:worker_threads', () => ({ |
| 11 | + parentPort: { |
| 12 | + on: vi.fn(), |
| 13 | + postMessage: vi.fn(), |
| 14 | + }, |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock('../compileTypes', () => ({ |
| 18 | + compileTypes: vi.fn(), |
| 19 | +})); |
| 20 | + |
| 21 | +vi.mock('../rewritePathsWithExposedFederatedModules', () => ({ |
| 22 | + rewritePathsWithExposedFederatedModules: vi.fn(), |
| 23 | +})); |
| 24 | + |
| 25 | +vi.mock('../workerLogger', () => ({ |
| 26 | + workerLogger: { |
| 27 | + log: vi.fn(), |
| 28 | + info: vi.fn(), |
| 29 | + }, |
| 30 | +})); |
| 31 | + |
| 32 | +describe('compileTypesWorker', () => { |
| 33 | + const mockParentPort = vi.mocked(parentPort); |
| 34 | + const mockCompileTypes = vi.mocked(compileTypes); |
| 35 | + const mockRewritePaths = vi.mocked(rewritePathsWithExposedFederatedModules); |
| 36 | + |
| 37 | + let messageHandler: (message: CompileTypesWorkerMessage | ExitMessage) => void; |
| 38 | + |
| 39 | + beforeEach(async () => { |
| 40 | + vi.resetAllMocks(); |
| 41 | + vi.resetModules(); |
| 42 | + vi.useFakeTimers(); |
| 43 | + |
| 44 | + messageHandler = vi.fn(); |
| 45 | + mockParentPort!.on.mockImplementation((event, handler) => { |
| 46 | + if (event === 'message') { |
| 47 | + messageHandler = handler; |
| 48 | + } |
| 49 | + return mockParentPort!; |
| 50 | + }); |
| 51 | + |
| 52 | + await import('../compileTypesWorker'); |
| 53 | + }); |
| 54 | + |
| 55 | + afterEach(() => { |
| 56 | + vi.resetModules(); |
| 57 | + }); |
| 58 | + |
| 59 | + test('handles exit message', () => { |
| 60 | + const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => undefined as never); |
| 61 | + const exitMessage: ExitMessage = { type: 'exit' }; |
| 62 | + |
| 63 | + messageHandler(exitMessage); |
| 64 | + |
| 65 | + expect(workerLogger.log).toHaveBeenCalledWith('Exiting by request'); |
| 66 | + expect(exitSpy).toHaveBeenCalledWith(0); |
| 67 | + }); |
| 68 | + |
| 69 | + test('handles successful compilation and rewrite', () => { |
| 70 | + const workerMessage: CompileTypesWorkerMessage = { |
| 71 | + tsconfigPath: 'tsconfig.json', |
| 72 | + exposedModules: ['moduleA', 'moduleB'], |
| 73 | + outFile: 'dist/types.d.ts', |
| 74 | + dirGlobalTypes: 'src/@types', |
| 75 | + federationConfig: {} as FederationConfig, |
| 76 | + }; |
| 77 | + |
| 78 | + mockCompileTypes.mockReturnValue({ isSuccess: true, typeDefinitions: 'type definitions' }); |
| 79 | + |
| 80 | + messageHandler(workerMessage); |
| 81 | + |
| 82 | + expect(mockCompileTypes).toHaveBeenCalledWith( |
| 83 | + expect.objectContaining({ |
| 84 | + tsconfigPath: 'tsconfig.json', |
| 85 | + exposedModules: ['moduleA', 'moduleB'], |
| 86 | + outFile: 'dist/types.d.ts', |
| 87 | + dirGlobalTypes: 'src/@types', |
| 88 | + }), |
| 89 | + workerLogger, |
| 90 | + ); |
| 91 | + |
| 92 | + expect(mockRewritePaths).toHaveBeenCalledWith( |
| 93 | + {}, |
| 94 | + 'dist/types.d.ts', |
| 95 | + 'type definitions', |
| 96 | + workerLogger, |
| 97 | + ); |
| 98 | + |
| 99 | + expect(mockParentPort?.postMessage).toHaveBeenCalledWith({ status: 'success' }); |
| 100 | + }); |
| 101 | + |
| 102 | + test('handles compilation failure', () => { |
| 103 | + const workerMessage: CompileTypesWorkerMessage = { |
| 104 | + tsconfigPath: 'tsconfig.json', |
| 105 | + exposedModules: ['moduleA'], |
| 106 | + outFile: 'dist/types.d.ts', |
| 107 | + dirGlobalTypes: 'src/@types', |
| 108 | + federationConfig: {} as FederationConfig, |
| 109 | + }; |
| 110 | + |
| 111 | + mockCompileTypes.mockReturnValue({ isSuccess: false, typeDefinitions: '' }); |
| 112 | + |
| 113 | + messageHandler(workerMessage); |
| 114 | + |
| 115 | + expect(mockParentPort?.postMessage).toHaveBeenCalledWith({ status: 'failure' }); |
| 116 | + }); |
| 117 | + |
| 118 | + test('handles errors during compilation', () => { |
| 119 | + const workerMessage: CompileTypesWorkerMessage = { |
| 120 | + tsconfigPath: 'tsconfig.json', |
| 121 | + exposedModules: ['moduleA'], |
| 122 | + outFile: 'dist/types.d.ts', |
| 123 | + dirGlobalTypes: 'src/@types', |
| 124 | + federationConfig: {} as FederationConfig, |
| 125 | + }; |
| 126 | + |
| 127 | + const error = new Error('Compilation error'); |
| 128 | + mockCompileTypes.mockImplementation(() => { |
| 129 | + throw error; |
| 130 | + }); |
| 131 | + |
| 132 | + messageHandler(workerMessage); |
| 133 | + |
| 134 | + expect(mockParentPort?.postMessage).toHaveBeenCalledWith({ |
| 135 | + status: 'error', |
| 136 | + error, |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + test('logs performance metrics', () => { |
| 141 | + const workerMessage: CompileTypesWorkerMessage = { |
| 142 | + tsconfigPath: 'tsconfig.json', |
| 143 | + exposedModules: ['moduleA'], |
| 144 | + outFile: 'dist/types.d.ts', |
| 145 | + dirGlobalTypes: 'src/@types', |
| 146 | + federationConfig: {} as FederationConfig, |
| 147 | + }; |
| 148 | + |
| 149 | + mockCompileTypes.mockReturnValue({ isSuccess: true, typeDefinitions: 'type definitions' }); |
| 150 | + |
| 151 | + vi.spyOn(performance, 'now') |
| 152 | + .mockReturnValueOnce(0) |
| 153 | + .mockReturnValueOnce(2000) |
| 154 | + .mockReturnValueOnce(2000) |
| 155 | + .mockReturnValueOnce(3000); |
| 156 | + |
| 157 | + messageHandler(workerMessage); |
| 158 | + |
| 159 | + expect(workerLogger.log).toHaveBeenCalledWith('Types compilation completed in 2.00 seconds'); |
| 160 | + expect(workerLogger.log).toHaveBeenCalledWith('Typings file rewritten in 1.00 seconds'); |
| 161 | + expect(workerLogger.info).toHaveBeenCalledWith( |
| 162 | + 'Types compilation and modification completed in 2.00 + 1.00 seconds', |
| 163 | + ); |
| 164 | + }); |
| 165 | +}); |
0 commit comments