test(config): 更新 Playwright 配置以适应 VSCode 环境

- 添加代码判断是否在 VSCode 中运行,并设置相应的 baseURL
- 修改 webServer 配置,仅在 VSCode 环境中启动本地服务器
- 优化配置文件结构,提高可读性和维护性
This commit is contained in:
严浩
2025-08-15 16:33:27 +08:00
parent b4c2711da6
commit ee58de3146

View File

@@ -1,6 +1,11 @@
import { defineConfig, devices } from '@playwright/test'; import { defineConfig, devices } from '@playwright/test';
import process from 'node:process'; import process from 'node:process';
const runningInVSCode = process.env.TERM_PROGRAM === 'vscode';
const baseURL = runningInVSCode
? 'http://localhost:4173'
: process.env.BASE_URL || 'https://vue-ts-example.oo1.dev';
/** /**
* Read environment variables from file. * Read environment variables from file.
* https://github.com/motdotla/dotenv * https://github.com/motdotla/dotenv
@@ -66,7 +71,7 @@ export default defineConfig({
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */ /* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
actionTimeout: 0, actionTimeout: 0,
/* Base URL to use in actions like `await page.goto('/')`. */ /* Base URL to use in actions like `await page.goto('/')`. */
baseURL: process.env.BASE_URL || 'https://vue-ts-example.oo1.dev', baseURL,
/* Only on CI systems run the tests headless */ /* Only on CI systems run the tests headless */
headless: !!process.env.CI || process.env.HEADLESS === 'true', headless: !!process.env.CI || process.env.HEADLESS === 'true',
@@ -76,16 +81,13 @@ export default defineConfig({
}, },
// /* Run your local dev server before starting the tests */ // /* Run your local dev server before starting the tests */
// webServer: { webServer: runningInVSCode
// /** ? {
// * Use the dev server by default for faster feedback loop. command: 'npm run build-only; npm run preview',
// * Use the preview server on CI for more realistic testing. port: 4173,
// * Playwright will re-use the local server if there is already a dev-server running. reuseExistingServer: true,
// */ }
// command: process.env.CI ? 'npm run preview' : 'npm run dev', : undefined,
// port: process.env.CI ? 4173 : 5173,
// reuseExistingServer: !process.env.CI,
// },
/* Folder for test artifacts such as screenshots, videos, traces, etc. */ /* Folder for test artifacts such as screenshots, videos, traces, etc. */
// outputDir: 'test-results/', // outputDir: 'test-results/',