|
| 1 | +import type { Plugin, ResolvedConfig, ViteDevServer } from 'vite' |
| 2 | +import process from 'node:process' |
| 3 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 4 | +import { createDevToolsContext } from '../context' |
| 5 | +import '@vitejs/devtools-kit' |
| 6 | + |
| 7 | +function createConfig(plugins: Plugin[], command: 'serve' | 'build' = 'serve'): ResolvedConfig { |
| 8 | + return { |
| 9 | + root: process.cwd(), |
| 10 | + command, |
| 11 | + plugins, |
| 12 | + } as unknown as ResolvedConfig |
| 13 | +} |
| 14 | + |
| 15 | +function createViteServer(): ViteDevServer { |
| 16 | + return { |
| 17 | + middlewares: { |
| 18 | + use: vi.fn(), |
| 19 | + }, |
| 20 | + } as unknown as ViteDevServer |
| 21 | +} |
| 22 | + |
| 23 | +/** A plugin whose dock names its client script by npm specifier, as the kit docs show. */ |
| 24 | +function createBareSpecifierPlugin(): Plugin { |
| 25 | + return { |
| 26 | + name: 'test-bare-specifier-dock', |
| 27 | + devtools: { |
| 28 | + setup(ctx) { |
| 29 | + ctx.docks.register({ |
| 30 | + id: 'test-bare-specifier', |
| 31 | + title: 'Test', |
| 32 | + icon: 'ph:bug-duotone', |
| 33 | + type: 'action', |
| 34 | + action: { importFrom: 'my-plugin/devtools-action' }, |
| 35 | + }) |
| 36 | + }, |
| 37 | + }, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +function spyOnDF8111() { |
| 42 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) |
| 43 | + return () => warn.mock.calls.filter(args => args.some(arg => String(arg).includes('DF8111'))).length |
| 44 | +} |
| 45 | + |
| 46 | +describe('createDevToolsContext client module resolution', () => { |
| 47 | + afterEach(() => { |
| 48 | + vi.restoreAllMocks() |
| 49 | + }) |
| 50 | + |
| 51 | + it('declares the Vite `/@id/` resolver before plugin setup registers bare-specifier docks', async () => { |
| 52 | + const countDF8111 = spyOnDF8111() |
| 53 | + |
| 54 | + const ctx = await createDevToolsContext( |
| 55 | + createConfig([createBareSpecifierPlugin()]), |
| 56 | + createViteServer(), |
| 57 | + ) |
| 58 | + |
| 59 | + expect(countDF8111()).toBe(0) |
| 60 | + expect(ctx.staticConfig.dock?.clientModuleResolution).toBe('/@id/{specifier}') |
| 61 | + }) |
| 62 | + |
| 63 | + it('keeps warning DF8111 without a dev server (standalone / build), where bare specifiers stay unresolvable', async () => { |
| 64 | + const countDF8111 = spyOnDF8111() |
| 65 | + |
| 66 | + const ctx = await createDevToolsContext( |
| 67 | + createConfig([createBareSpecifierPlugin()], 'build'), |
| 68 | + ) |
| 69 | + |
| 70 | + expect(ctx.staticConfig.dock?.clientModuleResolution).toBeUndefined() |
| 71 | + expect(countDF8111()).toBe(1) |
| 72 | + }) |
| 73 | +}) |
0 commit comments