|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +vi.mock('../../utils/command.ts', () => ({ |
| 4 | + runCommandSilently: vi.fn(), |
| 5 | +})); |
| 6 | + |
| 7 | +import { runCommandSilently } from '../../utils/command.ts'; |
| 8 | +import { checkRolldownCompatibility, ROLLDOWN_COMPAT_RESULT_PREFIX } from '../compat-runner.ts'; |
| 9 | +import { createMigrationReport } from '../report.ts'; |
| 10 | + |
| 11 | +const mockRunCommandSilently = vi.mocked(runCommandSilently); |
| 12 | + |
| 13 | +describe('checkRolldownCompatibility', () => { |
| 14 | + beforeEach(() => { |
| 15 | + mockRunCommandSilently.mockReset(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('merges warnings returned by the isolated config worker', async () => { |
| 19 | + mockRunCommandSilently.mockResolvedValue({ |
| 20 | + exitCode: 0, |
| 21 | + stdout: Buffer.from( |
| 22 | + `project config output\n${ROLLDOWN_COMPAT_RESULT_PREFIX}${JSON.stringify({ warnings: ['manualChunks warning'] })}\n`, |
| 23 | + ), |
| 24 | + stderr: Buffer.alloc(0), |
| 25 | + }); |
| 26 | + const report = createMigrationReport(); |
| 27 | + |
| 28 | + await checkRolldownCompatibility('/project', report); |
| 29 | + |
| 30 | + expect(report.warnings).toEqual(['manualChunks warning']); |
| 31 | + expect(mockRunCommandSilently).toHaveBeenCalledWith({ |
| 32 | + command: process.execPath, |
| 33 | + args: [expect.stringMatching(/compat-worker\.js$/), '/project'], |
| 34 | + cwd: '/project', |
| 35 | + envs: process.env, |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + it('skips compatibility checking when project config crashes the worker', async () => { |
| 40 | + mockRunCommandSilently.mockResolvedValue({ |
| 41 | + exitCode: 7, |
| 42 | + stdout: Buffer.from( |
| 43 | + `${ROLLDOWN_COMPAT_RESULT_PREFIX}${JSON.stringify({ warnings: ['incomplete result'] })}\n`, |
| 44 | + ), |
| 45 | + stderr: Buffer.from('project config crashed'), |
| 46 | + }); |
| 47 | + const report = createMigrationReport(); |
| 48 | + |
| 49 | + await expect(checkRolldownCompatibility('/project', report)).resolves.toBeUndefined(); |
| 50 | + expect(report.warnings).toEqual([]); |
| 51 | + }); |
| 52 | + |
| 53 | + it('skips compatibility checking when the worker cannot start', async () => { |
| 54 | + mockRunCommandSilently.mockRejectedValue(new Error('spawn failed')); |
| 55 | + const report = createMigrationReport(); |
| 56 | + |
| 57 | + await expect(checkRolldownCompatibility('/project', report)).resolves.toBeUndefined(); |
| 58 | + expect(report.warnings).toEqual([]); |
| 59 | + }); |
| 60 | +}); |
0 commit comments