From 2c27bd3b07c600802847040f00159e9aa9fd13cf Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Fri, 27 Jan 2023 14:36:41 -0800 Subject: [PATCH] chore: remove plugin registration sink (#20460) --- packages/playwright-ct-react/index.js | 8 +-- packages/playwright-ct-solid/index.js | 8 +-- packages/playwright-ct-svelte/index.js | 8 +-- packages/playwright-ct-vue/index.js | 8 +-- packages/playwright-ct-vue2/index.js | 8 +-- packages/playwright-test/src/cli.ts | 2 +- .../src/common/configLoader.ts | 2 + .../playwright-test/src/common/testLoader.ts | 13 ++-- packages/playwright-test/src/common/types.ts | 3 + packages/playwright-test/src/index.ts | 1 - .../playwright-test/src/loader/loaderMain.ts | 2 +- packages/playwright-test/src/plugins/index.ts | 20 +----- .../playwright-test/src/runner/loadUtils.ts | 6 +- .../src/runner/projectUtils.ts | 12 ++-- packages/playwright-test/src/runner/runner.ts | 22 +++---- packages/playwright-test/src/runner/tasks.ts | 45 +++++++++----- .../playwright-test/src/worker/workerMain.ts | 2 +- tests/playwright-test/global-setup.spec.ts | 10 +-- tests/playwright-test/reporter-html.spec.ts | 13 ++-- tests/playwright-test/runner.spec.ts | 20 +++--- tests/playwright-test/web-server.spec.ts | 61 +++++++++++-------- 21 files changed, 138 insertions(+), 136 deletions(-) diff --git a/packages/playwright-ct-react/index.js b/packages/playwright-ct-react/index.js index 8a74f9855b..b40cd0a184 100644 --- a/packages/playwright-ct-react/index.js +++ b/packages/playwright-ct-react/index.js @@ -14,18 +14,18 @@ * limitations under the License. */ -const { test: baseTest, expect, devices, _addRunnerPlugin, defineConfig } = require('@playwright/test'); +const { test: baseTest, expect, devices, defineConfig: originalDefineConfig } = require('@playwright/test'); const { fixtures } = require('@playwright/test/lib/mount'); const path = require('path'); -_addRunnerPlugin(() => { +const plugin = () => { // Only fetch upon request to avoid resolution in workers. const { createPlugin } = require('@playwright/test/lib/plugins/vitePlugin'); return createPlugin( path.join(__dirname, 'registerSource.mjs'), () => import('@vitejs/plugin-react').then(plugin => plugin.default())); -}); - +}; +const defineConfig = config => originalDefineConfig({ ...config, _plugins: [plugin] }); const test = baseTest.extend(fixtures); module.exports = { test, expect, devices, defineConfig }; diff --git a/packages/playwright-ct-solid/index.js b/packages/playwright-ct-solid/index.js index 9fe4c233cf..3e149431ab 100644 --- a/packages/playwright-ct-solid/index.js +++ b/packages/playwright-ct-solid/index.js @@ -14,18 +14,18 @@ * limitations under the License. */ -const { test: baseTest, expect, devices, _addRunnerPlugin, defineConfig } = require('@playwright/test'); +const { test: baseTest, expect, devices, defineConfig: originalDefineConfig } = require('@playwright/test'); const { fixtures } = require('@playwright/test/lib/mount'); const path = require('path'); -_addRunnerPlugin(() => { +const plugin = () => { // Only fetch upon request to avoid resolution in workers. const { createPlugin } = require('@playwright/test/lib/plugins/vitePlugin'); return createPlugin( path.join(__dirname, 'registerSource.mjs'), () => import('vite-plugin-solid').then(plugin => plugin.default())); -}); - +}; +const defineConfig = config => originalDefineConfig({ ...config, _plugins: [plugin] }); const test = baseTest.extend(fixtures); module.exports = { test, expect, devices, defineConfig }; diff --git a/packages/playwright-ct-svelte/index.js b/packages/playwright-ct-svelte/index.js index 7b03217a3c..95410584b9 100644 --- a/packages/playwright-ct-svelte/index.js +++ b/packages/playwright-ct-svelte/index.js @@ -14,18 +14,18 @@ * limitations under the License. */ -const { test: baseTest, expect, devices, _addRunnerPlugin, defineConfig } = require('@playwright/test'); +const { test: baseTest, expect, devices, defineConfig: originalDefineConfig } = require('@playwright/test'); const { fixtures } = require('@playwright/test/lib/mount'); const path = require('path'); -_addRunnerPlugin(() => { +const plugin = () => { // Only fetch upon request to avoid resolution in workers. const { createPlugin } = require('@playwright/test/lib/plugins/vitePlugin'); return createPlugin( path.join(__dirname, 'registerSource.mjs'), () => import('@sveltejs/vite-plugin-svelte').then(plugin => plugin.svelte())); -}); - +}; +const defineConfig = config => originalDefineConfig({ ...config, _plugins: [plugin] }); const test = baseTest.extend(fixtures); module.exports = { test, expect, devices, defineConfig }; diff --git a/packages/playwright-ct-vue/index.js b/packages/playwright-ct-vue/index.js index 3ae6b1a13d..9d7d84d07a 100644 --- a/packages/playwright-ct-vue/index.js +++ b/packages/playwright-ct-vue/index.js @@ -14,18 +14,18 @@ * limitations under the License. */ -const { test: baseTest, expect, devices, _addRunnerPlugin, defineConfig } = require('@playwright/test'); +const { test: baseTest, expect, devices, defineConfig: originalDefineConfig } = require('@playwright/test'); const { fixtures } = require('@playwright/test/lib/mount'); const path = require('path'); -_addRunnerPlugin(() => { +const plugin = () => { // Only fetch upon request to avoid resolution in workers. const { createPlugin } = require('@playwright/test/lib/plugins/vitePlugin'); return createPlugin( path.join(__dirname, 'registerSource.mjs'), () => import('@vitejs/plugin-vue').then(plugin => plugin.default())); -}); - +} +const defineConfig = config => originalDefineConfig({ ...config, _plugins: [plugin] }); const test = baseTest.extend(fixtures); module.exports = { test, expect, devices, defineConfig }; diff --git a/packages/playwright-ct-vue2/index.js b/packages/playwright-ct-vue2/index.js index 5e71a9f7e7..e9bd4dbcfe 100644 --- a/packages/playwright-ct-vue2/index.js +++ b/packages/playwright-ct-vue2/index.js @@ -14,18 +14,18 @@ * limitations under the License. */ -const { test: baseTest, expect, devices, _addRunnerPlugin, defineConfig } = require('@playwright/test'); +const { test: baseTest, expect, devices, defineConfig: originalDefineConfig } = require('@playwright/test'); const { fixtures } = require('@playwright/test/lib/mount'); const path = require('path'); -_addRunnerPlugin(() => { +const plugin = () => { // Only fetch upon request to avoid resolution in workers. const { createPlugin } = require('@playwright/test/lib/plugins/vitePlugin'); return createPlugin( path.join(__dirname, 'registerSource.mjs'), () => import('@vitejs/plugin-vue2').then(plugin => plugin.default())); -}); - +}; +const defineConfig = config => originalDefineConfig({ ...config, _plugins: [plugin] }); const test = baseTest.extend(fixtures); module.exports = { test, expect, devices, defineConfig }; diff --git a/packages/playwright-test/src/cli.ts b/packages/playwright-test/src/cli.ts index f07f6f3592..807dbab8b6 100644 --- a/packages/playwright-test/src/cli.ts +++ b/packages/playwright-test/src/cli.ts @@ -151,11 +151,11 @@ async function runTests(args: string[], opts: { [key: string]: any }) { return; const configLoader = new ConfigLoader(overrides); - const runner = new Runner(configLoader.fullConfig()); if (resolvedConfigFile) await configLoader.loadConfigFile(resolvedConfigFile); else await configLoader.loadEmptyConfig(configFileOrDirectory); + const runner = new Runner(configLoader.fullConfig()); const testFileFilters: TestFileFilter[] = args.map(arg => { const match = /^(.*?):(\d+):?(\d+)?$/.exec(arg); diff --git a/packages/playwright-test/src/common/configLoader.ts b/packages/playwright-test/src/common/configLoader.ts index aeaea2f1ac..ae5dcc24e3 100644 --- a/packages/playwright-test/src/common/configLoader.ts +++ b/packages/playwright-test/src/common/configLoader.ts @@ -121,6 +121,7 @@ export class ConfigLoader { this._fullConfig.shard = takeFirst(config.shard, baseFullConfig.shard); this._fullConfig._ignoreSnapshots = takeFirst(config.ignoreSnapshots, baseFullConfig._ignoreSnapshots); this._fullConfig.updateSnapshots = takeFirst(config.updateSnapshots, baseFullConfig.updateSnapshots); + this._fullConfig._pluginRegistrations = (config as any)._plugins || []; const workers = takeFirst(config.workers, '50%'); if (typeof workers === 'string') { @@ -435,6 +436,7 @@ export const baseFullConfig: FullConfigInternal = { _storeDir: '', _maxConcurrentTestGroups: 0, _ignoreSnapshots: false, + _pluginRegistrations: [], }; function resolveReporters(reporters: Config['reporter'], rootDir: string): ReporterDescription[]|undefined { diff --git a/packages/playwright-test/src/common/testLoader.ts b/packages/playwright-test/src/common/testLoader.ts index 9324e037a5..8c7d8af3c1 100644 --- a/packages/playwright-test/src/common/testLoader.ts +++ b/packages/playwright-test/src/common/testLoader.ts @@ -16,7 +16,6 @@ import path from 'path'; import type { TestError } from '../../reporter'; -import type { FullConfigInternal } from './types'; import type { LoadError } from './fixtures'; import { setCurrentlyLoadingFileSuite } from './globals'; import { PoolBuilder } from './poolBuilder'; @@ -31,16 +30,16 @@ export const defaultTimeout = 30000; const cachedFileSuites = new Map(); export class TestLoader { - private _fullConfig: FullConfigInternal; + private _rootDir: string; - constructor(fullConfig: FullConfigInternal) { - this._fullConfig = fullConfig; + constructor(rootDir: string) { + this._rootDir = rootDir; } async loadTestFile(file: string, environment: 'loader' | 'worker', loadErrors: TestError[]): Promise { if (cachedFileSuites.has(file)) return cachedFileSuites.get(file)!; - const suite = new Suite(path.relative(this._fullConfig.rootDir, file) || path.basename(file), 'file'); + const suite = new Suite(path.relative(this._rootDir, file) || path.basename(file), 'file'); suite._requireFile = file; suite.location = { file, line: 0, column: 0 }; @@ -80,8 +79,8 @@ export class TestLoader { } } -export async function loadTestFilesInProcess(config: FullConfigInternal, testFiles: string[], loadErrors: LoadError[]): Promise { - const testLoader = new TestLoader(config); +export async function loadTestFilesInProcess(rootDir: string, testFiles: string[], loadErrors: LoadError[]): Promise { + const testLoader = new TestLoader(rootDir); const rootSuite = new Suite('', 'root'); for (const file of testFiles) { const fileSuite = await testLoader.loadTestFile(file, 'loader', loadErrors); diff --git a/packages/playwright-test/src/common/types.ts b/packages/playwright-test/src/common/types.ts index cc539db0ed..14a6a6a736 100644 --- a/packages/playwright-test/src/common/types.ts +++ b/packages/playwright-test/src/common/types.ts @@ -16,6 +16,7 @@ import type { Fixtures, TestInfoError, Project } from '../../types/test'; import type { Location } from '../../types/testReporter'; +import type { TestRunnerPluginRegistration } from '../plugins'; import type { ConfigCLIOverrides } from './ipc'; import type { FullConfig as FullConfigPublic, FullProject as FullProjectPublic } from './types'; export * from '../../types/test'; @@ -57,6 +58,8 @@ export interface FullConfigInternal extends FullConfigPublic { // Overrides the public field. projects: FullProjectInternal[]; + + _pluginRegistrations: TestRunnerPluginRegistration[]; } /** diff --git a/packages/playwright-test/src/index.ts b/packages/playwright-test/src/index.ts index 9a4697c47f..1b5c533a99 100644 --- a/packages/playwright-test/src/index.ts +++ b/packages/playwright-test/src/index.ts @@ -24,7 +24,6 @@ import type { TestInfoImpl } from './common/testInfo'; import { rootTestType } from './common/testType'; import { type ContextReuseMode } from './common/types'; export { expect } from './matchers/expect'; -export { addRunnerPlugin as _addRunnerPlugin } from './plugins'; export const _baseTest: TestType<{}, {}> = rootTestType.test; addStackIgnoreFilter((frame: StackFrame) => frame.file.startsWith(path.dirname(require.resolve('../package.json')))); diff --git a/packages/playwright-test/src/loader/loaderMain.ts b/packages/playwright-test/src/loader/loaderMain.ts index 07792377f9..ca8bbe5dfb 100644 --- a/packages/playwright-test/src/loader/loaderMain.ts +++ b/packages/playwright-test/src/loader/loaderMain.ts @@ -39,7 +39,7 @@ export class LoaderMain extends ProcessRunner { async loadTestFiles(params: { files: string[] }) { const loadErrors: LoadError[] = []; const config = await this._config(); - const rootSuite = await loadTestFilesInProcess(config, params.files, loadErrors); + const rootSuite = await loadTestFilesInProcess(config.rootDir, params.files, loadErrors); return { rootSuite: rootSuite._deepSerialize(), loadErrors }; } } diff --git a/packages/playwright-test/src/plugins/index.ts b/packages/playwright-test/src/plugins/index.ts index 072f00cbb7..32d86af9b7 100644 --- a/packages/playwright-test/src/plugins/index.ts +++ b/packages/playwright-test/src/plugins/index.ts @@ -15,7 +15,6 @@ */ import type { Suite, Reporter } from '../../types/testReporter'; -import type { Runner } from '../runner/runner'; import type { FullConfig } from '../common/types'; export interface TestRunnerPlugin { @@ -25,22 +24,7 @@ export interface TestRunnerPlugin { teardown?(): Promise; } +export type TestRunnerPluginRegistration = TestRunnerPlugin | (() => TestRunnerPlugin | Promise); + export { webServer } from './webServerPlugin'; export { gitCommitInfo } from './gitCommitInfoPlugin'; - -let runnerInstanceToAddPluginsTo: Runner | undefined; -const deferredPlugins: TestRunnerPlugin[] = []; - -export const setRunnerToAddPluginsTo = (runner: Runner) => { - runnerInstanceToAddPluginsTo = runner; - for (const plugin of deferredPlugins) - runnerInstanceToAddPluginsTo.addPlugin(plugin); -}; - -export const addRunnerPlugin = (plugin: TestRunnerPlugin | (() => TestRunnerPlugin)) => { - plugin = typeof plugin === 'function' ? plugin() : plugin; - if (runnerInstanceToAddPluginsTo) - runnerInstanceToAddPluginsTo.addPlugin(plugin); - else - deferredPlugins.push(plugin); -}; diff --git a/packages/playwright-test/src/runner/loadUtils.ts b/packages/playwright-test/src/runner/loadUtils.ts index cf190d002f..10e80d48db 100644 --- a/packages/playwright-test/src/runner/loadUtils.ts +++ b/packages/playwright-test/src/runner/loadUtils.ts @@ -26,7 +26,7 @@ import type { FullConfigInternal } from '../common/types'; import { errorWithFile } from '../util'; import type { Matcher, TestFileFilter } from '../util'; import { createFileMatcher } from '../util'; -import { collectFilesForProjects, collectProjects } from './projectUtils'; +import { collectFilesForProjects, filterProjects } from './projectUtils'; import { requireOrImport } from '../common/transform'; import { serializeConfig } from '../common/ipc'; @@ -39,7 +39,7 @@ type LoadOptions = { }; export async function loadAllTests(config: FullConfigInternal, reporter: Multiplexer, options: LoadOptions, errors: TestError[]): Promise { - const projects = collectProjects(config, options.projectFilter); + const projects = filterProjects(config.projects, options.projectFilter); const filesByProject = await collectFilesForProjects(projects, options.testFileFilters); const allTestFiles = new Set(); for (const files of filesByProject.values()) @@ -80,7 +80,7 @@ async function loadTests(config: FullConfigInternal, reporter: Multiplexer, test } const loadErrors: LoadError[] = []; try { - return await loadTestFilesInProcess(config, [...testFiles], loadErrors); + return await loadTestFilesInProcess(config.rootDir, [...testFiles], loadErrors); } finally { errors.push(...loadErrors); } diff --git a/packages/playwright-test/src/runner/projectUtils.ts b/packages/playwright-test/src/runner/projectUtils.ts index c24fd52b77..21c2a0b9f1 100644 --- a/packages/playwright-test/src/runner/projectUtils.ts +++ b/packages/playwright-test/src/runner/projectUtils.ts @@ -18,16 +18,16 @@ import fs from 'fs'; import path from 'path'; import { minimatch } from 'playwright-core/lib/utilsBundle'; import { promisify } from 'util'; -import type { FullConfigInternal, FullProjectInternal } from '../common/types'; +import type { FullProjectInternal } from '../common/types'; import type { TestFileFilter } from '../util'; import { createFileMatcher, createFileMatcherFromFilters } from '../util'; const readFileAsync = promisify(fs.readFile); const readDirAsync = promisify(fs.readdir); -export function collectProjects(config: FullConfigInternal, projectNames?: string[]): FullProjectInternal[] { +export function filterProjects(projects: FullProjectInternal[], projectNames?: string[]): FullProjectInternal[] { if (!projectNames) - return [...config.projects]; + return [...projects]; const projectsToFind = new Set(); const unknownProjects = new Map(); projectNames.forEach(n => { @@ -35,19 +35,19 @@ export function collectProjects(config: FullConfigInternal, projectNames?: strin projectsToFind.add(name); unknownProjects.set(name, n); }); - const projects = config.projects.filter(project => { + const result = projects.filter(project => { const name = project.name.toLocaleLowerCase(); unknownProjects.delete(name); return projectsToFind.has(name); }); if (unknownProjects.size) { - const names = config.projects.map(p => p.name).filter(name => !!name); + const names = projects.map(p => p.name).filter(name => !!name); if (!names.length) throw new Error(`No named projects are specified in the configuration file`); const unknownProjectNames = Array.from(unknownProjects.values()).map(n => `"${n}"`).join(', '); throw new Error(`Project(s) ${unknownProjectNames} not found. Available named projects: ${names.map(name => `"${name}"`).join(', ')}`); } - return projects; + return result; } export async function collectFilesForProjects(projects: FullProjectInternal[], commandLineFileFilters: TestFileFilter[]): Promise> { diff --git a/packages/playwright-test/src/runner/runner.ts b/packages/playwright-test/src/runner/runner.ts index 73f5ce091a..48566e0eb6 100644 --- a/packages/playwright-test/src/runner/runner.ts +++ b/packages/playwright-test/src/runner/runner.ts @@ -17,13 +17,11 @@ import { monotonicTime } from 'playwright-core/lib/utils'; import type { FullResult } from '../../types/testReporter'; -import type { TestRunnerPlugin } from '../plugins'; -import { setRunnerToAddPluginsTo } from '../plugins'; import { dockerPlugin } from '../plugins/dockerPlugin'; import { webServerPluginsForConfig } from '../plugins/webServerPlugin'; -import { collectFilesForProjects, collectProjects } from './projectUtils'; +import { collectFilesForProjects, filterProjects } from './projectUtils'; import { createReporter } from './reporters'; -import { createTaskRunner } from './tasks'; +import { createTaskRunner, createTaskRunnerForList } from './tasks'; import type { TaskRunnerState } from './tasks'; import type { FullConfigInternal } from '../common/types'; import type { Matcher, TestFileFilter } from '../util'; @@ -38,19 +36,13 @@ export type RunOptions = { export class Runner { private _config: FullConfigInternal; - private _plugins: TestRunnerPlugin[] = []; constructor(config: FullConfigInternal) { this._config = config; - setRunnerToAddPluginsTo(this); - } - - addPlugin(plugin: TestRunnerPlugin) { - this._plugins.push(plugin); } async listTestFiles(projectNames: string[] | undefined): Promise { - const projects = collectProjects(this._config, projectNames); + const projects = filterProjects(this._config.projects, projectNames); const filesByProject = await collectFilesForProjects(projects, []); const report: any = { projects: [] @@ -69,17 +61,19 @@ export class Runner { const deadline = config.globalTimeout ? monotonicTime() + config.globalTimeout : 0; // Legacy webServer support. - this._plugins.push(...webServerPluginsForConfig(config)); + config._pluginRegistrations.push(...webServerPluginsForConfig(config)); // Docker support. - this._plugins.push(dockerPlugin); + config._pluginRegistrations.push(dockerPlugin); const reporter = await createReporter(config, options.listOnly); - const taskRunner = createTaskRunner(config, reporter, this._plugins, options); + const taskRunner = options.listOnly ? createTaskRunnerForList(config, reporter) + : createTaskRunner(config, reporter); const context: TaskRunnerState = { config, options, reporter, + plugins: [], }; reporter.onConfigure(config); diff --git a/packages/playwright-test/src/runner/tasks.ts b/packages/playwright-test/src/runner/tasks.ts index 3df8be66a4..613ae328aa 100644 --- a/packages/playwright-test/src/runner/tasks.ts +++ b/packages/playwright-test/src/runner/tasks.ts @@ -19,7 +19,7 @@ import path from 'path'; import { promisify } from 'util'; import { colors, rimraf } from 'playwright-core/lib/utilsBundle'; import { Dispatcher } from './dispatcher'; -import type { TestRunnerPlugin } from '../plugins'; +import type { TestRunnerPlugin, TestRunnerPluginRegistration } from '../plugins'; import type { Multiplexer } from '../reporters/multiplexer'; import type { TestGroup } from '../runner/testGroups'; import { createTestGroups, filterForShard } from '../runner/testGroups'; @@ -45,43 +45,56 @@ export type TaskRunnerState = { options: TaskRunnerOptions; reporter: Multiplexer; config: FullConfigInternal; + plugins: TestRunnerPlugin[]; rootSuite?: Suite; testGroups?: TestGroup[]; dispatcher?: Dispatcher; }; -export function createTaskRunner(config: FullConfigInternal, reporter: Multiplexer, plugins: TestRunnerPlugin[], options: TaskRunnerOptions): TaskRunner { +export function createTaskRunner(config: FullConfigInternal, reporter: Multiplexer): TaskRunner { const taskRunner = new TaskRunner(reporter, config.globalTimeout); - for (const plugin of plugins) + for (const plugin of config._pluginRegistrations) taskRunner.addTask('plugin setup', createPluginSetupTask(plugin)); if (config.globalSetup || config.globalTeardown) taskRunner.addTask('global setup', createGlobalSetupTask()); taskRunner.addTask('load tests', createLoadTask()); - if (!options.listOnly) { - taskRunner.addTask('prepare to run', createRemoveOutputDirsTask()); - taskRunner.addTask('plugin begin', async ({ rootSuite }) => { - for (const plugin of plugins) - await plugin.begin?.(rootSuite!); - }); - } + taskRunner.addTask('prepare to run', createRemoveOutputDirsTask()); + taskRunner.addTask('plugin begin', async ({ rootSuite, plugins }) => { + for (const plugin of plugins) + await plugin.begin?.(rootSuite!); + }); taskRunner.addTask('report begin', async ({ reporter, rootSuite }) => { reporter.onBegin?.(config, rootSuite!); return () => reporter.onEnd(); }); - if (!options.listOnly) { - taskRunner.addTask('setup workers', createSetupWorkersTask()); - taskRunner.addTask('test suite', async ({ dispatcher }) => dispatcher!.run()); - } + taskRunner.addTask('setup workers', createSetupWorkersTask()); + taskRunner.addTask('test suite', async ({ dispatcher }) => dispatcher!.run()); return taskRunner; } -export function createPluginSetupTask(plugin: TestRunnerPlugin): Task { - return async ({ config, reporter }) => { +export function createTaskRunnerForList(config: FullConfigInternal, reporter: Multiplexer): TaskRunner { + const taskRunner = new TaskRunner(reporter, config.globalTimeout); + taskRunner.addTask('load tests', createLoadTask()); + taskRunner.addTask('report begin', async ({ reporter, rootSuite }) => { + reporter.onBegin?.(config, rootSuite!); + return () => reporter.onEnd(); + }); + return taskRunner; +} + +export function createPluginSetupTask(pluginRegistration: TestRunnerPluginRegistration): Task { + return async ({ config, reporter, plugins }) => { + let plugin: TestRunnerPlugin; + if (typeof pluginRegistration === 'function') + plugin = await pluginRegistration(); + else + plugin = pluginRegistration; + plugins.push(plugin); await plugin.setup?.(config, config._configDir, reporter); return () => plugin.teardown?.(); }; diff --git a/packages/playwright-test/src/worker/workerMain.ts b/packages/playwright-test/src/worker/workerMain.ts index 3ef25eca9d..14debba78f 100644 --- a/packages/playwright-test/src/worker/workerMain.ts +++ b/packages/playwright-test/src/worker/workerMain.ts @@ -195,7 +195,7 @@ export class WorkerMain extends ProcessRunner { const configLoader = await ConfigLoader.deserialize(this._params.config); this._config = configLoader.fullConfig(); - this._testLoader = new TestLoader(this._config); + this._testLoader = new TestLoader(this._config.rootDir); this._project = this._config.projects.find(p => p._id === this._params.projectId)!; this._poolBuilder = PoolBuilder.createForWorker(this._project); } diff --git a/tests/playwright-test/global-setup.spec.ts b/tests/playwright-test/global-setup.spec.ts index 8ff7737251..558dac7b36 100644 --- a/tests/playwright-test/global-setup.spec.ts +++ b/tests/playwright-test/global-setup.spec.ts @@ -325,13 +325,14 @@ test('globalSetup auth should compile', async ({ runTSC }) => { test('teardown order', async ({ runInlineTest }) => { const result = await runInlineTest({ 'playwright.config.ts': ` + const _plugins = []; for (let i = 1; i < 4; ++i) { - pwt._addRunnerPlugin(() => ({ + _plugins.push(() => ({ setup: () => console.log('\\n%%setup ' + i), teardown: () => console.log('\\n%%teardown ' + i), })); } - export default {}; + export default { _plugins }; `, 'a.test.js': ` pwt.test('test', () => {}); @@ -352,8 +353,9 @@ test('teardown order', async ({ runInlineTest }) => { test('teardown after error', async ({ runInlineTest }) => { const result = await runInlineTest({ 'playwright.config.ts': ` + const _plugins = []; for (let i = 1; i < 4; ++i) { - pwt._addRunnerPlugin(() => ({ + _plugins.push(() => ({ setup: () => console.log('\\n%%setup ' + i), teardown: () => { console.log('\\n%%teardown ' + i); @@ -361,7 +363,7 @@ test('teardown after error', async ({ runInlineTest }) => { }, })); } - export default {}; + export default { _plugins }; `, 'a.test.js': ` pwt.test('test', () => {}); diff --git a/tests/playwright-test/reporter-html.spec.ts b/tests/playwright-test/reporter-html.spec.ts index ab68166437..d5417333eb 100644 --- a/tests/playwright-test/reporter-html.spec.ts +++ b/tests/playwright-test/reporter-html.spec.ts @@ -778,9 +778,8 @@ test.describe('gitCommitInfo plugin', () => { 'uncommitted.txt': `uncommitted file`, 'playwright.config.ts': ` import { gitCommitInfo } from '@playwright/test/lib/plugins'; - const { test, _addRunnerPlugin } = pwt; - _addRunnerPlugin(gitCommitInfo()); - export default {}; + const { test } = pwt; + export default { _plugins: [gitCommitInfo()] }; `, 'example.spec.ts': ` const { test } = pwt; @@ -810,8 +809,8 @@ test.describe('gitCommitInfo plugin', () => { 'uncommitted.txt': `uncommitted file`, 'playwright.config.ts': ` import { gitCommitInfo } from '@playwright/test/lib/plugins'; - const { test, _addRunnerPlugin } = pwt; - _addRunnerPlugin(gitCommitInfo({ + const { test } = pwt; + const plugin = gitCommitInfo({ info: { 'revision.id': '1234567890', 'revision.subject': 'a better subject', @@ -819,8 +818,8 @@ test.describe('gitCommitInfo plugin', () => { 'revision.author': 'William', 'revision.email': 'shakespeare@example.local', }, - })); - export default {}; + }); + export default { _plugins: [plugin] }; `, 'example.spec.ts': ` import { gitCommitInfo } from '@playwright/test/lib/plugins'; diff --git a/tests/playwright-test/runner.spec.ts b/tests/playwright-test/runner.spec.ts index da338dc37e..ead6ff1ce4 100644 --- a/tests/playwright-test/runner.spec.ts +++ b/tests/playwright-test/runner.spec.ts @@ -363,10 +363,8 @@ test('sigint should stop plugins', async ({ runInlineTest }) => { const result = await runInlineTest({ 'playwright.config.ts': ` - module.exports = { - }; - - require('@playwright/test')._addRunnerPlugin(() => ({ + const _plugins = []; + _plugins.push(() => ({ setup: async () => { console.log('Plugin1 setup'); console.log('%%SEND-SIGINT%%'); @@ -377,7 +375,7 @@ test('sigint should stop plugins', async ({ runInlineTest }) => { } })); - require('@playwright/test')._addRunnerPlugin(() => ({ + _plugins.push(() => ({ setup: async () => { console.log('Plugin2 setup'); }, @@ -385,6 +383,9 @@ test('sigint should stop plugins', async ({ runInlineTest }) => { console.log('Plugin2 teardown'); } })); + module.exports = { + _plugins + }; `, 'a.spec.js': ` const { test } = pwt; @@ -405,10 +406,8 @@ test('sigint should stop plugins 2', async ({ runInlineTest }) => { const result = await runInlineTest({ 'playwright.config.ts': ` - module.exports = { - }; - - require('@playwright/test')._addRunnerPlugin(() => ({ + const _plugins = []; + _plugins.push(() => ({ setup: async () => { console.log('Plugin1 setup'); }, @@ -417,7 +416,7 @@ test('sigint should stop plugins 2', async ({ runInlineTest }) => { } })); - require('@playwright/test')._addRunnerPlugin(() => ({ + _plugins.push(() => ({ setup: async () => { console.log('Plugin2 setup'); console.log('%%SEND-SIGINT%%'); @@ -427,6 +426,7 @@ test('sigint should stop plugins 2', async ({ runInlineTest }) => { console.log('Plugin2 teardown'); } })); + module.exports = { _plugins }; `, 'a.spec.js': ` const { test } = pwt; diff --git a/tests/playwright-test/web-server.spec.ts b/tests/playwright-test/web-server.spec.ts index f0b5ef9087..b955b8b7bb 100644 --- a/tests/playwright-test/web-server.spec.ts +++ b/tests/playwright-test/web-server.spec.ts @@ -536,17 +536,22 @@ test.describe('baseURL with plugins', () => { const port = workerIndex * 2 + 10500; const result = await runInlineTest({ 'test.spec.ts': ` - import { webServer } from '@playwright/test/lib/plugins'; - const { test, _addRunnerPlugin } = pwt; - _addRunnerPlugin(webServer({ - command: 'node ${JSON.stringify(SIMPLE_SERVER_PATH)} ${port}', - url: 'http://localhost:${port}/port', - })); - test('connect to the server', async ({baseURL, page}) => { - expect(baseURL).toBeUndefined(); - }); + const { test } = pwt; + test('connect to the server', async ({baseURL, page}) => { + expect(baseURL).toBeUndefined(); + }); + `, + 'playwright.config.ts': ` + import { webServer } from '@playwright/test/lib/plugins'; + module.exports = { + _plugins: [ + webServer({ + command: 'node ${JSON.stringify(SIMPLE_SERVER_PATH)} ${port}', + url: 'http://localhost:${port}/port', + }) + ] + }; `, - 'playwright.config.ts': `module.exports = {};`, }, undefined, { DEBUG: 'pw:webserver' }); expect(result.exitCode).toBe(0); expect(result.passed).toBe(1); @@ -556,24 +561,26 @@ test.describe('baseURL with plugins', () => { const port = workerIndex * 2 + 10500; const result = await runInlineTest({ 'test.spec.ts': ` - import { webServer } from '@playwright/test/lib/plugins'; - const { test, _addRunnerPlugin } = pwt; - _addRunnerPlugin(webServer({ - command: 'node ${JSON.stringify(SIMPLE_SERVER_PATH)} ${port + 1}', - url: 'http://localhost:${port + 1}/port' - })); - test('connect to the server', async ({baseURL, page}) => { - expect(baseURL).toBe('http://localhost:${port}'); - }); - `, + const { test } = pwt; + test('connect to the server', async ({baseURL, page}) => { + expect(baseURL).toBe('http://localhost:${port}'); + }); + `, 'playwright.config.ts': ` - module.exports = { - webServer: { - command: 'node ${JSON.stringify(SIMPLE_SERVER_PATH)} ${port}', - port: ${port}, - } - }; - `, + import { webServer } from '@playwright/test/lib/plugins'; + module.exports = { + webServer: { + command: 'node ${JSON.stringify(SIMPLE_SERVER_PATH)} ${port}', + port: ${port}, + }, + _plugins: [ + webServer({ + command: 'node ${JSON.stringify(SIMPLE_SERVER_PATH)} ${port + 1}', + url: 'http://localhost:${port + 1}/port' + }) + ] + }; + `, }, undefined, { DEBUG: 'pw:webserver' }); expect(result.exitCode).toBe(0); expect(result.passed).toBe(1);