Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(ct): enhance error messages when no index.html template is provided (#36556)

Adam Gastineau4c25573c3bc7
diff --git a/packages/playwright-ct-core/src/devServer.ts b/packages/playwright-ct-core/src/devServer.ts
index 6e3b555a07..c715f585a1 100644
--- a/packages/playwright-ct-core/src/devServer.ts
+++ b/packages/playwright-ct-core/src/devServer.ts
@@ -18,6 +18,7 @@ import fs from 'fs';
import path from 'path';
import { Watcher } from 'playwright/lib/fsWatcher';
+import { colors } from 'playwright-core/lib/utilsBundle';
import { source as injectedSource } from './generated/indexSource';
import { createConfig, frameworkConfig, populateComponentsFromTests, resolveDirs, transformIndexFile } from './viteUtils';
@@ -35,7 +36,7 @@ export async function runDevServer(config: FullConfig): Promise<() => Promise<vo
const dirs = await resolveDirs(configDir, config);
if (!dirs) {
// eslint-disable-next-line no-console
- console.log(`Template file playwright/index.html is missing.`);
+ console.log(colors.red(`Component testing template file playwright/index.html is missing and there is no existing Vite server. Component tests will fail.\n`));
return async () => {};
}
const registerSource = injectedSource + '\n' + await fs.promises.readFile(registerSourceFile, 'utf-8');
diff --git a/packages/playwright-ct-core/src/mount.ts b/packages/playwright-ct-core/src/mount.ts
index 9899a1b414..94c7de8d3d 100644
--- a/packages/playwright-ct-core/src/mount.ts
+++ b/packages/playwright-ct-core/src/mount.ts
@@ -50,6 +50,8 @@ export const fixtures: Fixtures<TestFixtures, WorkerFixtures, BaseTestFixtures>
page: async ({ page }, use, info) => {
if (!((info as any)._configInternal as FullConfigInternal).defineConfigWasUsed)
throw new Error('Component testing requires the use of the defineConfig() in your playwright-ct.config.{ts,js}: https://aka.ms/playwright/ct-define-config');
+ if (!process.env.PLAYWRIGHT_TEST_BASE_URL)
+ throw new Error('Component testing could not determine the base URL of your component under test. Ensure you have supplied a template playwright/index.html or have set the PLAYWRIGHT_TEST_BASE_URL environment variable.');
await (page as PageImpl)._wrapApiCall(async () => {
await page.exposeFunction('__ctDispatchFunction', (ordinal: number, args: any[]) => {
boundCallbacksForMount[ordinal](...args);
diff --git a/packages/playwright-ct-core/src/vitePlugin.ts b/packages/playwright-ct-core/src/vitePlugin.ts
index d31af197d1..f932bfd500 100644
--- a/packages/playwright-ct-core/src/vitePlugin.ts
+++ b/packages/playwright-ct-core/src/vitePlugin.ts
@@ -23,7 +23,7 @@ import { removeDirAndLogToConsole } from 'playwright/lib/util';
import { stoppable } from 'playwright/lib/utilsBundle';
import { isURLAvailable } from 'playwright-core/lib/utils';
import { assert, calculateSha1, getPlaywrightVersion } from 'playwright-core/lib/utils';
-import { debug } from 'playwright-core/lib/utilsBundle';
+import { colors, debug } from 'playwright-core/lib/utilsBundle';
import { runDevServer } from './devServer';
import { source as injectedSource } from './generated/indexSource';
@@ -127,7 +127,7 @@ export async function buildBundle(config: FullConfig, configDir: string): Promis
const dirs = await resolveDirs(configDir, config);
if (!dirs) {
// eslint-disable-next-line no-console
- console.log(`Template file playwright/index.html is missing.`);
+ console.log(colors.red(`Component testing template file playwright/index.html is missing and there is no existing Vite server. Component tests will fail.\n`));
return null;
}
diff --git a/tests/playwright-test/playwright.ct-react.spec.ts b/tests/playwright-test/playwright.ct-react.spec.ts
index ed3fa47caf..a5a545347d 100644
--- a/tests/playwright-test/playwright.ct-react.spec.ts
+++ b/tests/playwright-test/playwright.ct-react.spec.ts
@@ -596,3 +596,30 @@ test('should allow import from shared file', async ({ runInlineTest }) => {
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
});
+
+test('should throw test error when template index.html is not provided', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'playwright.config.ts': `
+ import { defineConfig } from '@playwright/experimental-ct-react';
+ export default defineConfig({});
+ `,
+ 'src/component.jsx': `
+ export const Component = () => <></>;
+ `,
+
+ 'src/component.test.jsx': `
+ import { test, expect } from '@playwright/experimental-ct-react';
+ import { Component } from './component';
+
+ test('pass component', async ({ page, mount }) => {
+ const component = await mount(<Component />);
+ await expect(page).toHaveURL('http://127.0.0.1:8080/');
+ });
+ `,
+ }, { workers: 1 });
+
+ expect(result.exitCode).toBe(1);
+ expect(result.passed).toBe(0);
+ expect(result.output).toContain('Component testing template file playwright/index.html is missing and there is no existing Vite server. Component tests will fail.');
+ expect(result.results[0].error.message).toBe('Error: Component testing could not determine the base URL of your component under test. Ensure you have supplied a template playwright/index.html or have set the PLAYWRIGHT_TEST_BASE_URL environment variable.');
+});