52 lines
1.8 KiB
TypeScript
52 lines
1.8 KiB
TypeScript
import { readFileSync } from 'node:fs'
|
|
import { fileURLToPath } from 'node:url'
|
|
import vue from '@vitejs/plugin-vue'
|
|
import { defineConfig } from 'vitest/config'
|
|
|
|
/**
|
|
* 单测(L1 纯函数 + L2 组件)配置。
|
|
*
|
|
* 刻意**不复用** vite.config.ts:那份配置带 lib 构建与 dts 插件,与测试无关;
|
|
* 这里只保留组件编译必需的 vue 插件、SCSS 与命名空间常量(与构建同一事实源)。
|
|
*
|
|
* 输出:默认 reporter 给人看,json reporter 写到 test-results/unit.json 给工具/AI 读。
|
|
*/
|
|
|
|
const configScssPath = fileURLToPath(new URL('../tokens/src/scss/_config.scss', import.meta.url))
|
|
const configScss = readFileSync(configScssPath, 'utf-8')
|
|
|
|
function readScssDefault(name: string, fallback: string): string {
|
|
const match = configScss.match(
|
|
new RegExp(`\\$${name}\\s*:\\s*['"]([^'"]+)['"]\\s*!default`),
|
|
)
|
|
return match ? match[1] : fallback
|
|
}
|
|
|
|
export default defineConfig({
|
|
plugins: [vue()],
|
|
resolve: {
|
|
alias: [
|
|
// 测试里大量用字符串模板挂载组件,需要带编译器的完整版 Vue
|
|
{ find: /^vue$/, replacement: 'vue/dist/vue.esm-bundler.js' },
|
|
],
|
|
},
|
|
define: {
|
|
__G3_NAMESPACE__: JSON.stringify(readScssDefault('namespace', 'g3')),
|
|
__G3_STATE_PREFIX__: JSON.stringify(readScssDefault('state-prefix', 'is')),
|
|
},
|
|
css: {
|
|
preprocessorOptions: {
|
|
scss: { api: 'modern-compiler' },
|
|
},
|
|
},
|
|
test: {
|
|
environment: 'happy-dom',
|
|
setupFiles: [fileURLToPath(new URL('./tests/setup.ts', import.meta.url))],
|
|
include: ['src/**/*.test.ts', 'tests/**/*.test.ts'],
|
|
reporters: ['default', 'json'],
|
|
outputFile: { json: 'test-results/unit.json' },
|
|
// 组件测试会 Teleport 到 body 并触发 ResizeObserver / rAF,统一在 setup 里补齐
|
|
restoreMocks: true,
|
|
},
|
|
})
|