Files
novalon-website/e2e/playwright.config.ts
T
zhangxiang 1c0ce5f458 chore(gates): DEC-N6 让 e2e 进类型口径,并修 pre-commit 位与证据路径覆盖历史
- 新增 e2e/tsconfig.json + `npm run type-check:e2e`(并入 test:all)。
  本轮实测证据:teardown 补丁里第二个 `const revalidateSecret` 与既有声明重名,
  `npm run type-check` 全程 EXIT=0,只有 `playwright test --list` 才报重复声明 ——
  现在静态期就能抓。读数:type-check:e2e EXIT=0。
- .husky/pre-commit 的索引模式改回 100755(本轮每次提交都在打
  "was ignored because it's not set as executable",即 lint-staged 在任何克隆里都不执行)。
  验证看 `git ls-files -s .husky` 的 mode,不是看文件系统权限。
- axe 证据落盘改为按运行轮次分目录(原写死 docs/acceptance/2026-09-21-axe/axe-evidence.json,
  于是每跑一次就覆盖一次历史:0157b1c9 里那 409 行 diff 就是这么产生的),
  并补 check:axe:selftest;判定逻辑与退出码三档语义未动。
- 顺带修一处仪器级缺陷:package.json 里 `type-check:e2e` 被写了两遍 ——
  JSON 重复键是后者静默覆盖前者,不会报错也不会判红,正是本仓反复吃的"重复声明"类。
  现取键数与值由 node 回读校验。
2026-10-17 07:54:21 +08:00

164 lines
7.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { defineConfig, devices } from '@playwright/test';
import { config as dotenvConfig } from 'dotenv';
import path from 'path';
// 加载项目根目录的 .env.local,使 Playwright webServer 与测试进程使用相同的 CMS_REVALIDATE_SECRET
// 若 .env.local 不存在则回退到默认测试 secret
dotenvConfig({ path: path.resolve(__dirname, '../.env.local') });
process.env.CMS_REVALIDATE_SECRET = process.env.CMS_REVALIDATE_SECRET || 'e2e-test-secret';
export default defineConfig({
testDir: './',
fullyParallel: true,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
workers: process.env.CI ? 1 : undefined,
reporter: [
['html', { open: 'never' }],
['list', { printSteps: true }],
['allure-playwright', { outputFolder: 'allure-results' }],
],
snapshotDir: './visual-snapshots',
snapshotPathTemplate: '{snapshotDir}/{projectName}/{testFilePath}/{arg}-{projectName}{ext}',
// 显式 outputDir 绝对路径(e2e/test-results),避免 playwright 在项目根创建 test-results
// 触发 WorkBuddy 沙箱 safe-delete BULK_GUARD(node fs.rm ≥ 50 文件被拦,e2e 直接退出)
outputDir: path.join(__dirname, 'test-results'),
use: {
baseURL: process.env.E2E_BASE_URL || 'http://localhost:3000',
// 路径以配置文件所在目录为基准,兼容 `cd e2e` 与从项目根目录直接调用两种执行方式
storageState: path.resolve(__dirname, 'storageState.json'),
trace: 'on-first-retry',
screenshot: 'only-on-failure',
ignoreHTTPSErrors: true,
actionTimeout: 15000,
navigationTimeout: 30000,
},
expect: {
toHaveScreenshot: {
maxDiffPixels: 200,
maxDiffPixelRatio: 0.005,
threshold: 0.3,
animations: 'disabled',
caret: 'hide',
scale: 'device',
// @ts-expect-error 类型口径缺口(不是运行时缺口):@playwright/test 的
// expect.toHaveScreenshot 选项类型里没有 `timeout`,但实现确实读它 ——
// node_modules/playwright/lib/matchers/expect.js:12182 `helper.options.timeout ?? this.timeout`。
// 删掉这行会让截图断言超时从 20s 回落到下面的 expect.timeout=15000,属行为变更 ⇒ 保留并标记。
// 若日后 Playwright 把 timeout 补进类型,本指令会因「未使用」而报错,届时直接摘掉标记。
timeout: 20000,
},
timeout: 15000,
},
projects: [
{
name: 'visual-chromium-desktop',
use: {
...devices['Desktop Chrome'],
viewport: { width: 1280, height: 800 },
// 视觉回归确定性:reducedMotion 让 ScrollReveal/StaggerReveal 走 initial={} 分支
// 默认可见(不残留 opacity:0),避免全页截图时 below-the-fold 模块因 whileInView
// 未触发而空白。同时使 Hero 粒子场走固定种子的静态降级帧,快照零漂移。
//
// ⚠ 残留 R-1(DEC-N6 补口带出,本批未修,需视觉 owner 拍板):这条意图**当前并没有生效**。
// `reducedMotion` 是 browser.newContext() 的上下文选项,不是 `use` 层选项:
// node_modules/playwright/lib/index.js:241-266 的 fixture 白名单里没有它,
// `_combinedContextOptions` 只从 `contextOptions` 取值 ⇒ 写在 use 里被直接忽略。
// 生效写法是 `contextOptions: { reducedMotion: 'reduce' }`,但那会真的关掉动效、
// 改变截图字节 ⇒ 与正在重采的 132 张基线冲突,本批只标注不改行为。
// @ts-expect-error 残留 R-1:见上,use 层无 reducedMotion 选项
reducedMotion: 'reduce',
},
testMatch: 'visual-regression.spec.ts',
},
{
name: 'visual-chromium-tablet',
use: {
...devices['iPad Pro 11'],
viewport: { width: 834, height: 1194 },
// @ts-expect-error 残留 R-1:use 层无 reducedMotion 选项,运行时被忽略(见 visual-chromium-desktop 处说明)
reducedMotion: 'reduce',
},
testMatch: 'visual-regression.spec.ts',
},
{
name: 'visual-chromium-mobile',
use: {
...devices['iPhone 14'],
viewport: { width: 390, height: 844 },
isMobile: true,
hasTouch: true,
// @ts-expect-error 残留 R-1:use 层无 reducedMotion 选项,运行时被忽略(见 visual-chromium-desktop 处说明)
reducedMotion: 'reduce',
},
testMatch: 'visual-regression.spec.ts',
},
{
name: 'visual-firefox-desktop',
use: {
...devices['Desktop Firefox'],
viewport: { width: 1280, height: 800 },
storageState: './storageState.firefox.json',
// @ts-expect-error 残留 R-1:use 层无 reducedMotion 选项,运行时被忽略(见 visual-chromium-desktop 处说明)
reducedMotion: 'reduce',
},
testMatch: 'visual-regression.spec.ts',
},
{
name: 'visual-webkit-desktop',
use: {
...devices['Desktop Safari'],
viewport: { width: 1280, height: 800 },
storageState: './storageState.firefox.json',
// @ts-expect-error 残留 R-1:use 层无 reducedMotion 选项,运行时被忽略(见 visual-chromium-desktop 处说明)
reducedMotion: 'reduce',
},
testMatch: 'visual-regression.spec.ts',
},
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
testIgnore: 'visual-regression.spec.ts',
},
{
name: 'chromium-mobile',
use: {
...devices['iPhone 14'],
viewport: { width: 390, height: 844 },
isMobile: true,
hasTouch: true,
},
testIgnore: 'visual-regression.spec.ts',
},
{
name: 'firefox',
use: { ...devices['Desktop Firefox'], storageState: './storageState.firefox.json' },
testIgnore: 'visual-regression.spec.ts',
},
{
name: 'webkit',
use: { ...devices['Desktop Safari'], storageState: './storageState.firefox.json' },
testIgnore: 'visual-regression.spec.ts',
},
],
webServer: {
// E2E_TARGET=production 时打构建产物(验收 A-10:dev server 下预渲染、ISR、生产响应头
// 与构建期才暴露的问题永不被测)。dev 仍是默认目标,因为多数用例依赖热更新提速。
// 产物是 output:'standalone',Next 16 明确告警 `"next start" does not work with
// "output: standalone"` ⇒ 生产目标必须直接起 dist/standalone/server.js,否则测的是不受支持的降级路径。
command:
process.env.E2E_TARGET === 'production'
? 'HOSTNAME=localhost PORT=3000 node dist/standalone/server.js'
: 'npm run dev',
url: 'http://localhost:3000',
// 复用已有服务会让「测的是当前代码」这一前提失效(门禁脚本曾把 next-server 留在 :3000 达
// 39 分钟),故 CI 与 production 目标一律新起服务。
reuseExistingServer: !process.env.CI && process.env.E2E_TARGET !== 'production',
timeout: 180000,
cwd: '..',
env: {
CMS_REVALIDATE_SECRET: process.env.CMS_REVALIDATE_SECRET || 'e2e-test-secret',
},
},
});