在 CI/CD 流程中,如何配置 Svelte 项目的自动化测试?
解读
面试官问的是“如何配置”,而不是“用什么工具”。他想知道你能否把测试脚本、环境、流水线、缓存、报告、质量门禁等要素串成一条可落地的国内研发管线。回答时要体现:
- 国内主流代码托管(Gitee、GitLab CN、阿里云 Codeup、腾讯云 CODING)的 YAML 语法差异;
- 国内镜像源加速(npmmirror、淘宝源、自建 Nexus);
- 前端特有的“编译-测试-产物”三段式;
- Svelte 编译时无虚拟 DOM,测试必须跑在真实浏览器或带 DOM 的 JSDOM 环境;
- 把单元、组件、端到端三层测试都纳入流水线,并给出失败策略与报告归档。
知识点
- Svelte 组件编译后生成原生 DOM 操作代码,测试需引入 @testing-library/svelte + jest-environment-jsdom;
- Vitest 与 Jest 在国内使用频率相当,Vitest 配置更轻量,且与 Vite 生态一致;
- Playwright 比 Cypress 在容器内并行更快,且官方提供国内镜像 mcr.microsoft.com/playwright;
- 国内 CI 平台默认单核 2G,需开 pnpm --shamefully-hoist + 缓存 node_modules 与 ~/.cache/ms-playwright;
- 质量门禁:单元覆盖率 ≥ 80%,组件快照 diff ≤ 1%,端到端用例 100% 通过;
- 产物上传:把静态文件推到阿里云 OSS / 腾讯云 COS,并通过 CDN 刷新缓存;
- 安全合规:流水线密钥统一放在平台“凭据管理”,禁止写死 ak/sk;
- 失败回滚:端到端失败即阻断,单元测试失败可发版但打警告标签。
答案
以 GitLab CI(国内私有化部署版)为例,给出可直接落地的 .gitlab-ci.yml:
# 变量区
variables:
NPM_CONFIG_REGISTRY: "https://registry.npmmirror.com"
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: "1"
VITEST_COVERAGE_THRESHOLD: "80"
# 缓存区
cache:
key: $CI_COMMIT_REF_SLUG
paths:
- node_modules/
- ~/.cache/ms-playwright/
# 阶段定义
stages:
- install
- lint
- test:unit
- test:component
- test:e2e
- build
- deploy
# 安装依赖
install:
stage: install
image: node:18-alpine
script:
- corepack enable pnpm
- pnpm config set registry $NPM_CONFIG_REGISTRY
- pnpm install --frozen-lockfile --shamefully-hoist
- npx playwright install chromium
artifacts:
expire_in: 1 day
paths:
- node_modules/
- ~/.cache/ms-playwright/
# 静态检查
lint:
stage: lint
image: node:18-alpine
needs: [install]
script:
- pnpm lint
- pnpm check # svelte-check
# 单元测试
test:unit:
stage: test:unit
image: node:18-alpine
needs: [install]
script:
- pnpm test:unit -- --coverage
coverage: '/All files[^|]*\|[^|]*\s+([\d\.]+)/'
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: coverage/cobertura-coverage.xml
expire_in: 3 days
# 组件测试
test:component:
stage: test:component
image: node:18-alpine
needs: [install]
script:
- pnpm test:component
artifacts:
reports:
junit: junit-component.xml
expire_in: 3 days
# 端到端测试
test:e2e:
stage: test:e2e
image: mcr.microsoft.com/playwright:v1.44.0-focal
needs: [install]
script:
- pnpm build
- pnpm test:e2e
artifacts:
reports:
junit: junit-e2e.xml
expire_in: 7 days
when: always
paths:
- test-results/
# 构建
build:
stage: build
image: node:18-alpine
needs: [lint, test:unit, test:component]
script:
- pnpm build
artifacts:
expire_in: 7 days
paths:
- dist/
# 部署
deploy:
stage: deploy
image: alpine:latest
needs: [build, test:e2e]
only:
- main
before_script:
- apk add --no-cache aws-cli
script:
- aws configure set aws_access_key_id $OSS_AK
- aws configure set aws_secret_access_key $OSS_SK
- aws s3 sync dist/ s3://your-bucket/web/ --endpoint-url=https://oss-cn-hangzhou.aliyuncs.com --delete
- curl https://cdn.aliyun.com/refresh --data-urlencode "objectPath=https://your-domain.com/*"
关键点解释:
- 使用 npmmirror 加速,CI 容器 30 秒内完成依赖安装;
- Playwright 浏览器缓存目录单独缓存,避免每次 400 MB 下载;
- 单元测试用 Vitest + @vitest/coverage-c8,覆盖率低于 80% 直接失败;
- 端到端失败即阻断部署,保证线上主分支永远可回滚;
- 产物上传到 OSS 后刷新 CDN,国内用户 30 秒内生效;
- 所有密钥通过 GitLab CI/CD Variables 注入,满足国内等保要求。
拓展思考
- 如果公司用的是 Gitee Go,可把 YAML 换成 gitee-go.yml,但缓存语法改为 gitee-cache,且需要把 playwright 镜像推送到阿里云 ACR 内网地址,防止公网拉取超时;
- 对于微前端场景,Svelte 子应用可在流水线里增加“快照比对”步骤:用 playwright 截图后,把图片转成 base64 推送到内部 Harbor 镜像仓库的附属存储,再通过自研的视觉回归服务比对,diff 超过 0.2% 即告警;
- 若需支持多环境(dev/sit/uat/prod),可在 CI 阶段只跑单元与组件测试,端到端测试下沉到 CD 阶段的“预发布”环境,通过 Kubernetes Job 拉起一次性 Pod,测试完成即销毁,节省集群资源;
- 国内金融客户要求“源码不可出境”,可在私有化 GitLab 里集成 SonarQube 国产替代(如鸿渐、思客),把测试覆盖率、漏洞、代码规范一次性聚合到质量门禁,再出具有效测评报告,满足监管审计。