Skip to content

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

chore: expose find related files over test server (#29604)

Pavel Feldmanac0787d0c8b3
diff --git a/packages/playwright/src/program.ts b/packages/playwright/src/program.ts
index cfe2d471ff..cbe161b59f 100644
--- a/packages/playwright/src/program.ts
+++ b/packages/playwright/src/program.ts
@@ -34,7 +34,7 @@ import { program } from 'playwright-core/lib/cli/program';
export { program } from 'playwright-core/lib/cli/program';
import type { ReporterDescription } from '../types/test';
import { prepareErrorStack } from './reporters/base';
-import { affectedTestFiles, cacheDir } from './transform/compilationCache';
+import { cacheDir } from './transform/compilationCache';
import { runTestServer } from './runner/testServer';
function addTestCommand(program: Command) {
@@ -102,17 +102,7 @@ function addFindRelatedTestFilesCommand(program: Command) {
command.description('Returns the list of related tests to the given files');
command.option('-c, --config <file>', `Configuration file, or a test directory with optional "playwright.config.{m,c}?{js,ts}"`);
command.action(async (files, options) => {
- await withRunnerAndMutedWrite(options.config, async (runner, config, configDir) => {
- const result = await runner.loadAllTests();
- if (result.status !== 'passed' || !result.suite)
- return { errors: result.errors };
-
- const resolvedFiles = (files as string[]).map(file => path.resolve(process.cwd(), file));
- const override = (config as any)['@playwright/test']?.['cli']?.['find-related-test-files'];
- if (override)
- return await override(resolvedFiles, config, configDir, result.suite);
- return { testFiles: affectedTestFiles(resolvedFiles) };
- });
+ await withRunnerAndMutedWrite(options.config, runner => runner.findRelatedTestFiles('in-process', files));
});
}
diff --git a/packages/playwright/src/runner/runner.ts b/packages/playwright/src/runner/runner.ts
index e9df35fd4e..51e902377a 100644
--- a/packages/playwright/src/runner/runner.ts
+++ b/packages/playwright/src/runner/runner.ts
@@ -30,6 +30,7 @@ import { InternalReporter } from '../reporters/internalReporter';
import { Multiplexer } from '../reporters/multiplexer';
import type { Suite } from '../common/test';
import { wrapReporterAsV2 } from '../reporters/reporterV2';
+import { affectedTestFiles } from '../transform/compilationCache';
type ProjectConfigWithFiles = {
name: string;
@@ -44,6 +45,11 @@ type ConfigListFilesReport = {
error?: TestError;
};
+export type FindRelatedTestFilesReport = {
+ testFiles: string[];
+ errors?: TestError[];
+};
+
export class Runner {
private _config: FullConfigInternal;
@@ -144,4 +150,16 @@ export class Runner {
webServerPluginsForConfig(config).forEach(p => config.plugins.push({ factory: p }));
return await runUIMode(config, options);
}
+
+ async findRelatedTestFiles(mode: 'in-process' | 'out-of-process', files: string[]): Promise<FindRelatedTestFilesReport> {
+ const result = await this.loadAllTests(mode);
+ if (result.status !== 'passed' || !result.suite)
+ return { errors: result.errors, testFiles: [] };
+
+ const resolvedFiles = (files as string[]).map(file => path.resolve(process.cwd(), file));
+ const override = (this._config.config as any)['@playwright/test']?.['cli']?.['find-related-test-files'];
+ if (override)
+ return await override(resolvedFiles, this._config.config, this._config.configDir, result.suite);
+ return { testFiles: affectedTestFiles(resolvedFiles) };
+ }
}
diff --git a/packages/playwright/src/runner/testServer.ts b/packages/playwright/src/runner/testServer.ts
index 4abe2b122b..805a73ceb8 100644
--- a/packages/playwright/src/runner/testServer.ts
+++ b/packages/playwright/src/runner/testServer.ts
@@ -27,6 +27,8 @@ import { Multiplexer } from '../reporters/multiplexer';
import { createReporters } from './reporters';
import { TestRun, createTaskRunnerForList, createTaskRunnerForTestServer } from './tasks';
import type { ConfigCLIOverrides } from '../common/ipc';
+import { Runner } from './runner';
+import type { FindRelatedTestFilesReport } from './runner';
type PlaywrightTestOptions = {
headed?: boolean,
@@ -117,6 +119,12 @@ class Dispatcher {
await this._runTests(params.reporter, params.locations, params.options);
}
+ async findRelatedTestFiles(params: { files: string[] }): Promise<FindRelatedTestFilesReport> {
+ const config = await this._loadConfig({});
+ const runner = new Runner(config);
+ return runner.findRelatedTestFiles('out-of-process', params.files);
+ }
+
async stop() {
await this._stopTests();
}