@gsc-basic/vitest-config 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +6 -0
- package/README.md +137 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.js +14 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
Copyright (c) 2026 Lenovo SSG BASD. All rights reserved.
|
|
2
|
+
@license Proprietary
|
|
3
|
+
|
|
4
|
+
This software is the confidential and proprietary property of Lenovo SSG BASD.
|
|
5
|
+
You shall not disclose such Confidential Information and shall use it only in
|
|
6
|
+
accordance with the terms of the license agreement.
|
package/README.md
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# @gsc-basic/vitest-config
|
|
2
|
+
|
|
3
|
+
面向第三方项目的通用 Vitest 配置工具包。
|
|
4
|
+
|
|
5
|
+
## 设计目标
|
|
6
|
+
|
|
7
|
+
- 通过预设覆盖常见场景(`node`、`web`、`vue`)。
|
|
8
|
+
- 保持原生 Vitest 命令使用习惯(`vitest`、`vitest run`、`vitest --ui`)。
|
|
9
|
+
|
|
10
|
+
## 安装
|
|
11
|
+
|
|
12
|
+
在使用方项目中安装:
|
|
13
|
+
|
|
14
|
+
- `@gsc-basic/vitest-config`
|
|
15
|
+
- `vite`
|
|
16
|
+
- `vitest`
|
|
17
|
+
|
|
18
|
+
如需 Vue 组件测试,请在使用方项目中显式安装 `@vue/test-utils`。
|
|
19
|
+
|
|
20
|
+
如需 UI 模式(`vitest --ui`),可直接使用,无需在使用方项目中额外安装 `@vitest/ui`。
|
|
21
|
+
|
|
22
|
+
## API
|
|
23
|
+
|
|
24
|
+
### createVitestConfig(options)
|
|
25
|
+
|
|
26
|
+
返回一个普通对象配置,适合在导出前继续加工或调试。
|
|
27
|
+
|
|
28
|
+
### defineVitestConfig(options, overrides)
|
|
29
|
+
|
|
30
|
+
基于默认值生成配置,并通过 `mergeConfig` 合并 `overrides`。
|
|
31
|
+
|
|
32
|
+
### mergeVitestConfig(baseConfig, overrideConfig)
|
|
33
|
+
|
|
34
|
+
对 Vitest `mergeConfig` 的轻量封装。
|
|
35
|
+
|
|
36
|
+
## 配置项设计
|
|
37
|
+
|
|
38
|
+
配置按职责拆分:
|
|
39
|
+
|
|
40
|
+
- `preset`: `node` | `web` | `vue`
|
|
41
|
+
- `root`: 根目录(用于路径解析)
|
|
42
|
+
- `plugins`: 框架插件开关(`vue`、`vueJsx`)
|
|
43
|
+
- `resolve`: 直接透传 Vitest/Vite 官方 `resolve` 配置
|
|
44
|
+
- `css`: 透传 Vite `css` 配置
|
|
45
|
+
- `test`: Vitest 运行时配置(`environment`、`setupFiles`、`coverage`、`depsInline`、`ui` 等)
|
|
46
|
+
|
|
47
|
+
### 预设说明
|
|
48
|
+
|
|
49
|
+
- `node`
|
|
50
|
+
- environment: `node`
|
|
51
|
+
- css transform: 关闭
|
|
52
|
+
- vue 插件: 关闭
|
|
53
|
+
- `web`
|
|
54
|
+
- environment: `happy-dom`
|
|
55
|
+
- css transform: 开启
|
|
56
|
+
- vue 插件: 关闭
|
|
57
|
+
- `vue`
|
|
58
|
+
- environment: `happy-dom`
|
|
59
|
+
- css transform: 开启
|
|
60
|
+
- vue 插件: 开启
|
|
61
|
+
|
|
62
|
+
## 使用示例
|
|
63
|
+
|
|
64
|
+
### Node 项目最小配置
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
import { defineVitestConfig } from '@gsc-basic/vitest-config';
|
|
68
|
+
|
|
69
|
+
export default defineVitestConfig({
|
|
70
|
+
preset: 'node',
|
|
71
|
+
test: {
|
|
72
|
+
setupFiles: ['./test/setup.ts'],
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Web 项目
|
|
78
|
+
|
|
79
|
+
```js
|
|
80
|
+
import { defineVitestConfig } from '@gsc-basic/vitest-config';
|
|
81
|
+
|
|
82
|
+
export default defineVitestConfig({
|
|
83
|
+
preset: 'web',
|
|
84
|
+
test: {
|
|
85
|
+
coverage: true,
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Vue 项目
|
|
91
|
+
|
|
92
|
+
```js
|
|
93
|
+
import path from 'node:path';
|
|
94
|
+
import { defineVitestConfig } from '@gsc-basic/vitest-config';
|
|
95
|
+
|
|
96
|
+
export default defineVitestConfig({
|
|
97
|
+
preset: 'vue',
|
|
98
|
+
resolve: {
|
|
99
|
+
alias: {
|
|
100
|
+
'@': path.resolve(process.cwd(), 'src'),
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
test: {
|
|
104
|
+
setupFiles: ['./test/setup.ts'],
|
|
105
|
+
coverage: {
|
|
106
|
+
include: ['src/**/*.{ts,tsx,js,jsx,vue}'],
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## 最短迁移清单(从业务仓库迁移到本包)
|
|
113
|
+
|
|
114
|
+
1. 安装依赖:在业务仓库新增 `@gsc-basic/vitest-config`,并确保保留 `vite`、`vitest`。
|
|
115
|
+
2. 新建或改造 `vitest.config.mjs`:改为 `import { defineVitestConfig } from '@gsc-basic/vitest-config'`。
|
|
116
|
+
3. 选择预设:
|
|
117
|
+
- 纯函数/Node 工具库选 `node`
|
|
118
|
+
- 浏览器逻辑选 `web`
|
|
119
|
+
- Vue 组件项目选 `vue`
|
|
120
|
+
4. 回填项目特有项:把原配置中的 `setupFiles`、coverage include、alias、css 预处理配置迁入新配置对象。
|
|
121
|
+
5. 清理重复配置:从业务仓库旧的 `vite` 配置链中移除 `test` 相关段,避免双配置。
|
|
122
|
+
6. 验证命令:依次执行 `vitest`、`vitest run`、`vitest --ui`(如使用 UI)。
|
|
123
|
+
7. 最后收敛依赖:
|
|
124
|
+
- 若项目测试代码直接 `import` 了 `@vue/test-utils`,继续保留在业务仓库。
|
|
125
|
+
- 若项目使用 `vitest --ui`,无需在业务仓库重复声明 `@vitest/ui`。
|
|
126
|
+
|
|
127
|
+
## 命令使用
|
|
128
|
+
|
|
129
|
+
本包不替代 Vitest CLI,请在使用方项目保留原生命令:
|
|
130
|
+
|
|
131
|
+
- `vitest`
|
|
132
|
+
- `vitest run`
|
|
133
|
+
- `vitest --ui`
|
|
134
|
+
|
|
135
|
+
## 注意事项
|
|
136
|
+
|
|
137
|
+
- 测试代码中直接使用到的库,请在使用方项目显式声明(如 `@vue/test-utils`)。
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
|
|
2
|
+
import { UserConfig } from "vite";
|
|
3
|
+
|
|
4
|
+
//#region src/typing.d.ts
|
|
5
|
+
type GscVitestPreset = 'node' | 'web' | 'vue';
|
|
6
|
+
interface GscVitestCoverageOptions {
|
|
7
|
+
provider?: 'v8';
|
|
8
|
+
reporter?: string[];
|
|
9
|
+
include?: string[];
|
|
10
|
+
exclude?: string[];
|
|
11
|
+
all?: boolean;
|
|
12
|
+
}
|
|
13
|
+
interface GscVitestPluginOptions {
|
|
14
|
+
vue?: boolean;
|
|
15
|
+
vueJsx?: boolean;
|
|
16
|
+
}
|
|
17
|
+
interface GscVitestTestOptions {
|
|
18
|
+
environment?: 'node' | 'happy-dom' | 'jsdom' | 'edge-runtime';
|
|
19
|
+
includeSource?: string[];
|
|
20
|
+
exclude?: string[];
|
|
21
|
+
setupFiles?: string[];
|
|
22
|
+
css?: boolean;
|
|
23
|
+
globals?: boolean;
|
|
24
|
+
passWithNoTests?: boolean;
|
|
25
|
+
depsInline?: Array<string | RegExp>;
|
|
26
|
+
depsExternal?: Array<string | RegExp>;
|
|
27
|
+
coverage?: boolean | GscVitestCoverageOptions;
|
|
28
|
+
ui?: boolean;
|
|
29
|
+
}
|
|
30
|
+
interface GscVitestOptions {
|
|
31
|
+
preset?: GscVitestPreset;
|
|
32
|
+
root?: string;
|
|
33
|
+
resolve?: UserConfig['resolve'];
|
|
34
|
+
plugins?: GscVitestPluginOptions;
|
|
35
|
+
css?: UserConfig['css'];
|
|
36
|
+
test?: GscVitestTestOptions;
|
|
37
|
+
}
|
|
38
|
+
type GscVitestConfig = UserConfig;
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/index.d.ts
|
|
41
|
+
declare function createVitestConfig(options?: GscVitestOptions): GscVitestConfig;
|
|
42
|
+
declare function defineVitestConfig(options?: GscVitestOptions, overrides?: GscVitestConfig): GscVitestConfig;
|
|
43
|
+
declare function mergeVitestConfig(baseConfig: GscVitestConfig, overrideConfig: GscVitestConfig): GscVitestConfig;
|
|
44
|
+
//#endregion
|
|
45
|
+
export { type GscVitestConfig, type GscVitestCoverageOptions, type GscVitestOptions, type GscVitestPluginOptions, type GscVitestPreset, type GscVitestTestOptions, createVitestConfig, defineVitestConfig, mergeVitestConfig };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
Copyright (c) 2026 Lenovo SSG BASD. All rights reserved.
|
|
3
|
+
@license Proprietary
|
|
4
|
+
|
|
5
|
+
This software is the confidential and proprietary property of Lenovo SSG BASD.
|
|
6
|
+
You shall not disclose such Confidential Information and shall use it only in
|
|
7
|
+
accordance with the terms of the license agreement.
|
|
8
|
+
|
|
9
|
+
--- Build Info ---
|
|
10
|
+
Version: 0.0.0
|
|
11
|
+
Build Time: 2026/5/21 09:37:58
|
|
12
|
+
Build Commit: 98b7f75ab1883e7e13a31b2f1dfd4c5da2d75fb5
|
|
13
|
+
*/
|
|
14
|
+
import e from"@vitejs/plugin-vue";import t from"@vitejs/plugin-vue-jsx";import{configDefaults as n,defineConfig as r,mergeConfig as i}from"vitest/config";const a={provider:`v8`,reporter:[`text`,`lcov`]},o={node:{environment:`node`,css:!1,vue:!1,vueJsx:!1},web:{environment:`happy-dom`,css:!0,vue:!1,vueJsx:!1},vue:{environment:`happy-dom`,css:!0,vue:!0,vueJsx:!0}};function s(e){if(e!==!1)return e===!0||e==null?a:{...a,...e}}function c(n,r){let i=[],a=o[r],s=n.plugins?.vue??a.vue,c=n.plugins?.vueJsx??a.vueJsx;return s&&i.push(e()),c&&i.push(t()),i}function l(e={}){let t=e.preset||`node`,r=o[t];return{plugins:c(e,t),root:e.root,resolve:e.resolve,...e.css?{css:e.css}:{},test:{environment:e.test?.environment||r.environment,includeSource:e.test?.includeSource,exclude:[...n.exclude,...e.test?.exclude||[]],setupFiles:e.test?.setupFiles||[],css:e.test?.css??r.css,globals:e.test?.globals,passWithNoTests:e.test?.passWithNoTests,ui:e.test?.ui,server:{deps:{inline:e.test?.depsInline,external:e.test?.depsExternal}},coverage:s(e.test?.coverage)}}}function u(e={},t={}){return r(i(l(e),t))}function d(e,t){return i(e,t)}export{l as createVitestConfig,u as defineVitestConfig,d as mergeVitestConfig};
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gsc-basic/vitest-config",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "Vitest Config for GSC Basic Service",
|
|
6
|
+
"author": "Lenovo SSG BASD <-GSC_Scaffolding_Support@lenovo.com>",
|
|
7
|
+
"license": "LicenseRef-Proprietary",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://gitlab.xpaas.lenovo.com/tsd-basic/gsc-frontend-config.git"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"GSC Basic",
|
|
14
|
+
"Vitest"
|
|
15
|
+
],
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public",
|
|
18
|
+
"registry": "https://registry.npmjs.org/"
|
|
19
|
+
},
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"import": "./dist/index.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"main": "./dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"files": [
|
|
29
|
+
"./dist"
|
|
30
|
+
],
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"vite": "^8.0.12",
|
|
33
|
+
"vitest": "^4.1.7"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@vitejs/plugin-vue": "^6.0.7",
|
|
37
|
+
"@vitejs/plugin-vue-jsx": "^5.1.4",
|
|
38
|
+
"@vitest/coverage-v8": "^4.1.7",
|
|
39
|
+
"@vitest/ui": "^4.1.7",
|
|
40
|
+
"happy-dom": "^20.6.1"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsdown",
|
|
44
|
+
"outdated": "pnpm outdated",
|
|
45
|
+
"clean": "rimraf ./dist"
|
|
46
|
+
}
|
|
47
|
+
}
|