SvelteKit中基础Playwright配置无法正常运行问题求助
解决方案:Playwright测试超时+未执行问题
核心问题分析
- WebServer超时:Playwright配置的待监听端口与Vite预览端口不匹配,导致Playwright持续等待不存在的服务端口,最终触发超时。
- 测试未被识别:虽文件名符合默认规则,但需确认路径匹配或显式指定测试文件匹配规则。
具体修复步骤
1. 统一WebServer端口配置
你的playwright.config.js中webServer.port设为4173,但vite.config.js里preview.port是3000,两者不一致是超时的直接原因。二选一修改即可:
- 方案一:修改
playwright.config.js的端口:
const config = { webServer: { command: 'npm run build && npm run preview', port: 3000 // 与Vite预览端口对齐 }, testDir: 'uat', timeout: 6000 }; export default config;
- 方案二:修改
vite.config.js的预览端口:
const config = { // ...其他配置 preview: { port: 4173 // 与Playwright监听端口对齐 }, // ...其他配置 } export default config;
2. 确保测试文件被识别
若仍出现Running 0 tests,可在playwright.config.js中显式指定测试文件匹配规则,避免路径或命名的隐性问题:
const config = { // ...其他配置 testMatch: '**/*.test.js' // 明确匹配uat目录下的test.js文件 };
3. 验证服务启动可用性
手动执行npm run build && npm run preview,在浏览器访问对应端口(如3000),确认页面能正常加载。如果build或preview过程本身报错,需先解决这些问题(比如检查socketPlugin是否影响预览服务启动)。
4. 调整WebServer启动超时时间
若项目build或预览启动较慢,可增加webServer的超时时间:
webServer: { command: 'npm run build && npm run preview', port: 3000, timeout: 120000 // 延长至120秒,根据实际情况调整 }
内容的提问来源于stack exchange,提问作者Tadas V.
相关产品推荐
相关产品推荐

