|
| 1 | +import type { Plugin } from '@voidzero-dev/vite-plus-core'; |
1 | 2 | import { describe, expect, it } from 'vitest'; |
2 | 3 |
|
3 | | -import { rewriteVitePlusTestSpecifier } from '../define-config.ts'; |
| 4 | +import { defineConfig, rewriteVitePlusTestSpecifier } from '../define-config.ts'; |
| 5 | + |
| 6 | +const REWRITE_PLUGIN_NAME = 'vite-plus:vitest-specifier-rewrite'; |
| 7 | + |
| 8 | +function pluginName(p: unknown): string | undefined { |
| 9 | + if (p && typeof p === 'object' && 'name' in p && typeof (p as { name: unknown }).name === 'string') { |
| 10 | + return (p as { name: string }).name; |
| 11 | + } |
| 12 | + return undefined; |
| 13 | +} |
4 | 14 |
|
5 | 15 | describe('rewriteVitePlusTestSpecifier', () => { |
6 | 16 | it('is a no-op when source does not mention vite-plus/test', () => { |
@@ -66,3 +76,82 @@ describe('rewriteVitePlusTestSpecifier', () => { |
66 | 76 | expect(rewriteVitePlusTestSpecifier(input)).toBe(expected); |
67 | 77 | }); |
68 | 78 | }); |
| 79 | + |
| 80 | +describe('defineConfig project plugin injection', () => { |
| 81 | + it('injects rewrite plugin at the root plugins array', () => { |
| 82 | + const existing: Plugin = { name: 'user-existing-root-plugin' }; |
| 83 | + const result = defineConfig({ plugins: [existing] }) as { plugins: unknown[] }; |
| 84 | + |
| 85 | + expect(pluginName(result.plugins[0])).toBe(REWRITE_PLUGIN_NAME); |
| 86 | + expect(pluginName(result.plugins[1])).toBe('user-existing-root-plugin'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('injects rewrite plugin into an inline-object project entry, preserving existing plugins', () => { |
| 90 | + const existing: Plugin = { name: 'user-unit-project-plugin' }; |
| 91 | + const result = defineConfig({ |
| 92 | + test: { |
| 93 | + projects: [ |
| 94 | + { |
| 95 | + plugins: [existing], |
| 96 | + test: { name: 'unit', include: ['test/unit/**/*.spec.ts'], environment: 'node' }, |
| 97 | + }, |
| 98 | + ], |
| 99 | + }, |
| 100 | + }) as { test: { projects: unknown[] } }; |
| 101 | + |
| 102 | + const project = result.test.projects[0] as { plugins: unknown[]; test: { name: string } }; |
| 103 | + expect(project.test.name).toBe('unit'); |
| 104 | + expect(pluginName(project.plugins[0])).toBe(REWRITE_PLUGIN_NAME); |
| 105 | + expect(pluginName(project.plugins[1])).toBe('user-unit-project-plugin'); |
| 106 | + // Sanity: the existing plugin reference is preserved (clone shallow-copies the array). |
| 107 | + expect(project.plugins[1]).toBe(existing); |
| 108 | + }); |
| 109 | + |
| 110 | + it('injects rewrite plugin into the return value of a function-shaped project entry', () => { |
| 111 | + const existing: Plugin = { name: 'user-fn-project-plugin' }; |
| 112 | + const projectFn = () => ({ |
| 113 | + plugins: [existing], |
| 114 | + test: { name: 'nuxt', environment: 'happy-dom' as const }, |
| 115 | + }); |
| 116 | + const result = defineConfig({ |
| 117 | + test: { projects: [projectFn] }, |
| 118 | + }) as { test: { projects: unknown[] } }; |
| 119 | + |
| 120 | + const wrapped = result.test.projects[0]; |
| 121 | + expect(typeof wrapped).toBe('function'); |
| 122 | + |
| 123 | + // Vitest passes a `ConfigEnv` to the function; we don't depend on its |
| 124 | + // shape here, the wrapper just forwards it. |
| 125 | + const fakeEnv = { mode: 'test', command: 'serve' as const }; |
| 126 | + const resolved = (wrapped as (env: typeof fakeEnv) => { plugins: unknown[] })(fakeEnv); |
| 127 | + expect(pluginName(resolved.plugins[0])).toBe(REWRITE_PLUGIN_NAME); |
| 128 | + expect(pluginName(resolved.plugins[1])).toBe('user-fn-project-plugin'); |
| 129 | + }); |
| 130 | + |
| 131 | + it('passes string-glob project entries through unchanged', () => { |
| 132 | + const result = defineConfig({ |
| 133 | + test: { |
| 134 | + projects: ['./packages/*', './apps/*'], |
| 135 | + }, |
| 136 | + }) as { test: { projects: unknown[] } }; |
| 137 | + |
| 138 | + expect(result.test.projects).toEqual(['./packages/*', './apps/*']); |
| 139 | + }); |
| 140 | + |
| 141 | + it('handles projects with no existing plugins array', () => { |
| 142 | + const result = defineConfig({ |
| 143 | + test: { |
| 144 | + projects: [ |
| 145 | + { |
| 146 | + test: { name: 'no-plugins', environment: 'node' }, |
| 147 | + }, |
| 148 | + ], |
| 149 | + }, |
| 150 | + }) as { test: { projects: unknown[] } }; |
| 151 | + |
| 152 | + const project = result.test.projects[0] as { plugins: unknown[]; test: { name: string } }; |
| 153 | + expect(project.test.name).toBe('no-plugins'); |
| 154 | + expect(project.plugins).toHaveLength(1); |
| 155 | + expect(pluginName(project.plugins[0])).toBe(REWRITE_PLUGIN_NAME); |
| 156 | + }); |
| 157 | +}); |
0 commit comments