parent
111876d526
commit
fc8126ca4e
|
|
@ -16,7 +16,6 @@
|
||||||
|
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import type http from 'http';
|
import type http from 'http';
|
||||||
import type { AddressInfo } from 'net';
|
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { assert, calculateSha1, getPlaywrightVersion, isURLAvailable } from 'playwright-core/lib/utils';
|
import { assert, calculateSha1, getPlaywrightVersion, isURLAvailable } from 'playwright-core/lib/utils';
|
||||||
import { debug } from 'playwright-core/lib/utilsBundle';
|
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 { source as injectedSource } from './generated/indexSource';
|
||||||
import type { ImportInfo } from './tsxTransform';
|
import type { ImportInfo } from './tsxTransform';
|
||||||
import type { ComponentRegistry } from './viteUtils';
|
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';
|
import { resolveHook } from 'playwright/lib/transform/transform';
|
||||||
|
|
||||||
const log = debug('pw:vite');
|
const log = debug('pw:vite');
|
||||||
|
|
@ -57,12 +56,10 @@ export function createPlugin(): TestRunnerPlugin {
|
||||||
const { preview } = await import('vite');
|
const { preview } = await import('vite');
|
||||||
const previewServer = await preview(viteConfig);
|
const previewServer = await preview(viteConfig);
|
||||||
stoppableServer = stoppable(previewServer.httpServer as http.Server, 0);
|
stoppableServer = stoppable(previewServer.httpServer as http.Server, 0);
|
||||||
const isAddressInfo = (x: any): x is AddressInfo => x?.address;
|
|
||||||
const address = previewServer.httpServer.address();
|
const baseUrl = getTestBaseUrl(config, previewServer, viteConfig);
|
||||||
if (isAddressInfo(address)) {
|
if (baseUrl)
|
||||||
const protocol = viteConfig.preview.https ? 'https:' : 'http:';
|
process.env.PLAYWRIGHT_TEST_BASE_URL = baseUrl;
|
||||||
process.env.PLAYWRIGHT_TEST_BASE_URL = `${protocol}//${viteConfig.preview.host}:${address.port}`;
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
end: async () => {
|
end: async () => {
|
||||||
|
|
|
||||||
|
|
@ -15,12 +15,13 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
import type { AddressInfo } from 'net';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { debug } from 'playwright-core/lib/utilsBundle';
|
import { debug } from 'playwright-core/lib/utilsBundle';
|
||||||
import { getUserData } from 'playwright/lib/transform/compilationCache';
|
import { getUserData } from 'playwright/lib/transform/compilationCache';
|
||||||
import type { PlaywrightTestConfig as BasePlaywrightTestConfig } from 'playwright/types/test';
|
import type { PlaywrightTestConfig as BasePlaywrightTestConfig } from 'playwright/types/test';
|
||||||
import type { FullConfig } from 'playwright/types/testReporter';
|
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 type { ImportInfo } from './tsxTransform';
|
||||||
import { resolveHook } from 'playwright/lib/transform/transform';
|
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 importReactRE = /(^|\n|;)import\s+(\*\s+as\s+)?React(,|\s+)/;
|
||||||
const compiledReactRE = /(const|var)\s+React\s*=/;
|
const compiledReactRE = /(const|var)\s+React\s*=/;
|
||||||
|
|
||||||
export function transformIndexFile(id: string, content: string, templateDir: string, registerSource: string, importInfos: Map<string, ImportInfo>): TransformResult | null {
|
export function transformIndexFile(id: string, content: string, templateDir: string, registerSource: string, importInfos: Map<string, ImportInfo>): TransformResult | null {
|
||||||
// Vite React plugin will do this for .jsx files, but not .js files.
|
// 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)) {
|
if (id.endsWith('.js') && content.includes('React.createElement') && !content.match(importReactRE) && !content.match(compiledReactRE)) {
|
||||||
const code = `import React from 'react';\n${content}`;
|
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<Plugin> } {
|
export function frameworkConfig(config: FullConfig): { registerSourceFile: string, frameworkPluginFactory?: () => Promise<Plugin> } {
|
||||||
return (config as any)['@playwright/experimental-ct-core'];
|
return (config as any)['@playwright/experimental-ct-core'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getTestBaseUrl(config: FullConfig, previewServer: PreviewServer, viteConfig: Record<string, any>): 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);
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue