前端测试基建: vitest.config + jsdom + test script, 3 个死 spec 复活

现状: vitest 已装, 3 个 spec 已存在 (listings composables, 14 用例), 但跑不起来——package.json 无 test script, 无 vitest.config, @/ 别名在测试环境不通 (Cannot find package '@/...')。

修复:
- 新增 vitest.config.ts: jsdom 环境 (提供 window/document/navigator, 让 monitor.ts 等浏览器模块可加载) + @ 别名 (与 vite.config.ts 同口径) + globals + v8 coverage 配置
- package.json 加 test / test:watch / test:coverage script
- 装 jsdom devDependency
- check.sh 前端门禁加 npm run test 步骤 (受 --skip-tests 控制, 与后端测试一致)

验证: npm run test 3 spec 全过 (14 用例); check.sh --skip-backend 全绿 (check + test + build)。

之前 "有测试实际跑不起来" 的债务清掉。后续 composable/api 层测试可直接 npm run test。
This commit is contained in:
yml
2026-06-14 18:13:06 +08:00
parent 8fef037be3
commit 965832c3cb
4 changed files with 563 additions and 3 deletions
+7 -2
View File
@@ -7,7 +7,7 @@
#
# 包含:
# 后端:go vet + go build + go test
# 前端:npm run check (prettier + eslint + typecheck) + npm run build
# 前端:npm run check (prettier + eslint + typecheck) + npm run test + npm run build
#
# 任一步骤失败即退出非零,阻止部署。
set -euo pipefail
@@ -35,7 +35,7 @@ show_help() {
cat << EOF
用法: ./scripts/check.sh [选项]
全量代码门禁:后端 go vet/build/test + 前端 check/build。
全量代码门禁:后端 go vet/build/test + 前端 check/test/build。
选项:
--skip-backend 跳过后端检查
@@ -116,6 +116,11 @@ if [[ "${SKIP_FRONTEND}" == "0" ]]; then
# npm run check = prettier --check + eslint --max-warnings=0 + vue-tsc
run_step "npm run check (prettier + eslint + typecheck)" npm run check || FAILED=1
if [[ "${SKIP_TESTS}" == "0" ]]; then
run_step "npm run test (vitest)" npm run test || FAILED=1
else
log_warn "跳过前端测试 (--skip-tests)"
fi
run_step "npm run build (vite 生产构建)" npm run build || FAILED=1
else
log_warn "跳过前端检查 (--skip-frontend)"