From fc8126ca4eb2755c7dbf0e6a5bddf7c0b698e763 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Ma=CC=8Artensson?= Date: Thu, 27 Jun 2024 16:33:39 +0200 Subject: [PATCH] fix(ct): allow custom baseURL in config References: #31440 --- packages/playwright-ct-core/src/vitePlugin.ts | 13 ++++------- packages/playwright-ct-core/src/viteUtils.ts | 23 +++++++++++++++++-- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/packages/playwright-ct-core/src/vitePlugin.ts b/packages/playwright-ct-core/src/vitePlugin.ts index 075e4670f3..c41bf9d143 100644 --- a/packages/playwright-ct-core/src/vitePlugin.ts +++ b/packages/playwright-ct-core/src/vitePlugin.ts @@ -16,7 +16,6 @@ import fs from 'fs'; import type http from 'http'; -import type { AddressInfo } from 'net'; import path from 'path'; import { assert, calculateSha1, getPlaywrightVersion, isURLAvailable } from 'playwright-core/lib/utils'; import { debug } from 'playwright-core/lib/utilsBundle'; @@ -29,7 +28,7 @@ import type { TestRunnerPlugin } from '../../playwright/src/plugins'; import { source as injectedSource } from './generated/indexSource'; import type { ImportInfo } from './tsxTransform'; import type { ComponentRegistry } from './viteUtils'; -import { createConfig, frameworkConfig, hasJSComponents, populateComponentsFromTests, resolveDirs, resolveEndpoint, transformIndexFile } from './viteUtils'; +import { createConfig, frameworkConfig, getTestBaseUrl, hasJSComponents, populateComponentsFromTests, resolveDirs, resolveEndpoint, transformIndexFile } from './viteUtils'; import { resolveHook } from 'playwright/lib/transform/transform'; const log = debug('pw:vite'); @@ -57,12 +56,10 @@ export function createPlugin(): TestRunnerPlugin { const { preview } = await import('vite'); const previewServer = await preview(viteConfig); stoppableServer = stoppable(previewServer.httpServer as http.Server, 0); - const isAddressInfo = (x: any): x is AddressInfo => x?.address; - const address = previewServer.httpServer.address(); - if (isAddressInfo(address)) { - const protocol = viteConfig.preview.https ? 'https:' : 'http:'; - process.env.PLAYWRIGHT_TEST_BASE_URL = `${protocol}//${viteConfig.preview.host}:${address.port}`; - } + + const baseUrl = getTestBaseUrl(config, previewServer, viteConfig); + if (baseUrl) + process.env.PLAYWRIGHT_TEST_BASE_URL = baseUrl; }, end: async () => { diff --git a/packages/playwright-ct-core/src/viteUtils.ts b/packages/playwright-ct-core/src/viteUtils.ts index a9f7020999..53c191e05e 100644 --- a/packages/playwright-ct-core/src/viteUtils.ts +++ b/packages/playwright-ct-core/src/viteUtils.ts @@ -15,12 +15,13 @@ */ import fs from 'fs'; +import type { AddressInfo } from 'net'; import path from 'path'; import { debug } from 'playwright-core/lib/utilsBundle'; import { getUserData } from 'playwright/lib/transform/compilationCache'; import type { PlaywrightTestConfig as BasePlaywrightTestConfig } from 'playwright/types/test'; import type { FullConfig } from 'playwright/types/testReporter'; -import type { InlineConfig, Plugin, TransformResult, UserConfig } from 'vite'; +import type { InlineConfig, Plugin, PreviewServer, TransformResult, UserConfig } from 'vite'; import type { ImportInfo } from './tsxTransform'; import { resolveHook } from 'playwright/lib/transform/transform'; @@ -164,7 +165,7 @@ export function hasJSComponents(components: ImportInfo[]): boolean { const importReactRE = /(^|\n|;)import\s+(\*\s+as\s+)?React(,|\s+)/; const compiledReactRE = /(const|var)\s+React\s*=/; -export function transformIndexFile(id: string, content: string, templateDir: string, registerSource: string, importInfos: Map): TransformResult | null { +export function transformIndexFile(id: string, content: string, templateDir: string, registerSource: string, importInfos: Map): TransformResult | null { // Vite React plugin will do this for .jsx files, but not .js files. if (id.endsWith('.js') && content.includes('React.createElement') && !content.match(importReactRE) && !content.match(compiledReactRE)) { const code = `import React from 'react';\n${content}`; @@ -197,3 +198,21 @@ export function transformIndexFile(id: string, content: string, templateDir: str export function frameworkConfig(config: FullConfig): { registerSourceFile: string, frameworkPluginFactory?: () => Promise } { return (config as any)['@playwright/experimental-ct-core']; } + +export function getTestBaseUrl(config: FullConfig, previewServer: PreviewServer, viteConfig: Record): string | undefined { + const use = config.projects[0].use as CtConfig; + if (use.baseURL) + return use.baseURL; + + const address = previewServer.httpServer.address(); + if (isAddressInfo(address)) { + const protocol = viteConfig.preview.https ? 'https:' : 'http:'; + return `${protocol}//${viteConfig.preview.host}:${address.port}`; + } + + return undefined; +} + +function isAddressInfo(maybeAddressInfo: undefined | unknown | AddressInfo): maybeAddressInfo is AddressInfo { + return Boolean(maybeAddressInfo) && Boolean((maybeAddressInfo as AddressInfo).address); +} \ No newline at end of file