|
| 1 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +afterEach(() => { |
| 4 | + vi.resetModules() |
| 5 | + vi.doUnmock('node:url') |
| 6 | +}) |
| 7 | + |
| 8 | +describe('@comark/vue vite plugin', () => { |
| 9 | + it('normalizes injected runtime import paths to forward slashes on Windows', async () => { |
| 10 | + vi.doMock('node:url', async () => { |
| 11 | + const actual = await vi.importActual<typeof import('node:url')>('node:url') |
| 12 | + return { |
| 13 | + ...actual, |
| 14 | + fileURLToPath: () => String.raw`D:\Code\gqt\packages\comark-vue\src\utils`, |
| 15 | + } |
| 16 | + }) |
| 17 | + |
| 18 | + const { default: comark } = await import('../src/vite.ts') |
| 19 | + const plugin = comark() |
| 20 | + const vueOptions: Record<string, any> = {} |
| 21 | + |
| 22 | + plugin.configResolved?.({ |
| 23 | + root: '/repo', |
| 24 | + plugins: [ |
| 25 | + { |
| 26 | + name: 'vite:vue', |
| 27 | + api: { |
| 28 | + options: vueOptions, |
| 29 | + }, |
| 30 | + }, |
| 31 | + ], |
| 32 | + } as any) |
| 33 | + |
| 34 | + const transform = vueOptions.template.compilerOptions.nodeTransforms[0] |
| 35 | + |
| 36 | + function transformSlotOutlet(node: Record<string, any>) { |
| 37 | + node.codegenNode = { callee: 'renderSlot' } |
| 38 | + } |
| 39 | + |
| 40 | + const node = { |
| 41 | + tag: 'slot', |
| 42 | + props: [{ name: 'unwrap' }], |
| 43 | + } |
| 44 | + |
| 45 | + const context = { |
| 46 | + ssr: false, |
| 47 | + nodeTransforms: [transformSlotOutlet], |
| 48 | + imports: [], |
| 49 | + } |
| 50 | + |
| 51 | + const run = transform(node, context) |
| 52 | + run?.() |
| 53 | + |
| 54 | + expect(node.codegenNode.callee).toBe('_renderComarkSlot') |
| 55 | + expect(context.imports).toHaveLength(1) |
| 56 | + expect(context.imports[0]).toMatchObject({ |
| 57 | + exp: '{ renderSlot as _renderComarkSlot }', |
| 58 | + path: 'D:/Code/gqt/packages/comark-vue/src/utils/slot', |
| 59 | + }) |
| 60 | + }) |
| 61 | +}) |
0 commit comments