|
| 1 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' |
| 2 | +import { tmpdir } from 'node:os' |
| 3 | +import path from 'node:path' |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +vi.mock('vite', async () => { |
| 7 | + const actual = await vi.importActual<typeof import('vite')>('vite') |
| 8 | + return { |
| 9 | + ...actual, |
| 10 | + createServerModuleRunner: vi.fn(() => ({ |
| 11 | + clearCache: vi.fn(), |
| 12 | + import: vi.fn(), |
| 13 | + })), |
| 14 | + } |
| 15 | +}) |
| 16 | + |
| 17 | +describe('createFileSystemRouterPlugin', () => { |
| 18 | + const previousIsVxrnCli = process.env.IS_VXRN_CLI |
| 19 | + let previousVxrnPluginConfig: unknown |
| 20 | + let tempRoot: string | undefined |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + previousVxrnPluginConfig = (globalThis as any).__vxrnPluginConfig__ |
| 24 | + }) |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + if (tempRoot) { |
| 28 | + rmSync(tempRoot, { recursive: true, force: true }) |
| 29 | + tempRoot = undefined |
| 30 | + } |
| 31 | + |
| 32 | + if (previousIsVxrnCli === undefined) { |
| 33 | + delete process.env.IS_VXRN_CLI |
| 34 | + } else { |
| 35 | + process.env.IS_VXRN_CLI = previousIsVxrnCli |
| 36 | + } |
| 37 | + |
| 38 | + if (previousVxrnPluginConfig === undefined) { |
| 39 | + delete (globalThis as any).__vxrnPluginConfig__ |
| 40 | + } else { |
| 41 | + ;(globalThis as any).__vxrnPluginConfig__ = previousVxrnPluginConfig |
| 42 | + } |
| 43 | + vi.restoreAllMocks() |
| 44 | + }) |
| 45 | + |
| 46 | + it('keeps route watcher rebuild errors handled', async () => { |
| 47 | + process.env.IS_VXRN_CLI = '1' |
| 48 | + tempRoot = mkdtempSync(path.join(tmpdir(), 'one-router-watch-')) |
| 49 | + const appDir = path.join(tempRoot, 'app') |
| 50 | + writeFileSync(path.join(tempRoot, 'package.json'), '{}\n') |
| 51 | + mkdirSync(appDir) |
| 52 | + writeFileSync( |
| 53 | + path.join(appDir, 'index.tsx'), |
| 54 | + 'export default function Index() { return null }\n' |
| 55 | + ) |
| 56 | + |
| 57 | + ;(globalThis as any).__vxrnPluginConfig__ = { |
| 58 | + web: { |
| 59 | + defaultRenderMode: 'ssg', |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + const { createFileSystemRouterPlugin } = await import('./fileSystemRouterPlugin') |
| 64 | + const plugin = createFileSystemRouterPlugin({ |
| 65 | + router: { |
| 66 | + root: appDir, |
| 67 | + }, |
| 68 | + }) |
| 69 | + |
| 70 | + let watcherListener: |
| 71 | + | ((event: string, changedPath: string) => void | Promise<void>) |
| 72 | + | undefined |
| 73 | + const server = { |
| 74 | + environments: { |
| 75 | + ssr: {}, |
| 76 | + }, |
| 77 | + watcher: { |
| 78 | + addListener: vi.fn((event: string, listener: typeof watcherListener) => { |
| 79 | + if (event === 'all') { |
| 80 | + watcherListener = listener |
| 81 | + } |
| 82 | + }), |
| 83 | + on: vi.fn(), |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + ;(plugin as any).configureServer(server) |
| 88 | + expect(watcherListener).toBeDefined() |
| 89 | + |
| 90 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) |
| 91 | + delete (globalThis as any).__vxrnPluginConfig__ |
| 92 | + |
| 93 | + if (!watcherListener) { |
| 94 | + throw new Error('Expected route watcher listener to be registered') |
| 95 | + } |
| 96 | + |
| 97 | + await expect( |
| 98 | + Promise.resolve(watcherListener('add', path.join(appDir, 'new-route.tsx'))) |
| 99 | + ).resolves.toBeUndefined() |
| 100 | + |
| 101 | + expect(warn).toHaveBeenCalledWith( |
| 102 | + expect.stringContaining('[one] Failed to rebuild routes'), |
| 103 | + expect.any(Error) |
| 104 | + ) |
| 105 | + }) |
| 106 | +}) |
0 commit comments