From e756528ad2804a9fd09910ef0ef95b703a48ed68 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Thu, 28 Apr 2022 07:16:17 -0800 Subject: [PATCH] feat(plugins): expose suite to plugin setup (#13807) --- docs/src/test-api/class-testplugin.md | 4 +++- .../bundles/utils/src/utilsBundleImpl.ts | 3 --- packages/playwright-core/src/utilsBundle.ts | 1 - packages/playwright-ct-react/vitePlugin.d.ts | 1 - packages/playwright-ct-svelte/vitePlugin.d.ts | 1 - packages/playwright-ct-vue/vitePlugin.d.ts | 1 - .../playwright-test/src/plugins/vitePlugin.ts | 21 +++++++------------ packages/playwright-test/src/runner.ts | 6 +++--- packages/playwright-test/types/test.d.ts | 6 +++++- tests/config/experimental.d.ts | 6 +++++- utils/generate_types/overrides-test.d.ts | 1 + 11 files changed, 25 insertions(+), 26 deletions(-) diff --git a/docs/src/test-api/class-testplugin.md b/docs/src/test-api/class-testplugin.md index f94de5b1be..aa650cc614 100644 --- a/docs/src/test-api/class-testplugin.md +++ b/docs/src/test-api/class-testplugin.md @@ -10,5 +10,7 @@ ## optional async method: TestPlugin.setup -## optional async method: TestPlugin.teardown +### param: TestPlugin.setup.suite +- `suite` <[Suite]> +## optional async method: TestPlugin.teardown diff --git a/packages/playwright-core/bundles/utils/src/utilsBundleImpl.ts b/packages/playwright-core/bundles/utils/src/utilsBundleImpl.ts index faa62d7f61..1ec3c810a7 100644 --- a/packages/playwright-core/bundles/utils/src/utilsBundleImpl.ts +++ b/packages/playwright-core/bundles/utils/src/utilsBundleImpl.ts @@ -22,9 +22,6 @@ export const debug = debugLibrary; export { getProxyForUrl } from 'proxy-from-env'; -import globLibrary from 'glob'; -export const glob = globLibrary; - export { HttpsProxyAgent } from 'https-proxy-agent'; import jpegLibrary from 'jpeg-js'; diff --git a/packages/playwright-core/src/utilsBundle.ts b/packages/playwright-core/src/utilsBundle.ts index 6406f3ec91..e47b55c3e1 100644 --- a/packages/playwright-core/src/utilsBundle.ts +++ b/packages/playwright-core/src/utilsBundle.ts @@ -17,7 +17,6 @@ export const colors: typeof import('../bundles/utils/node_modules/colors/safe') = require('./utilsBundleImpl').colors; export const debug: typeof import('../bundles/utils/node_modules/@types/debug') = require('./utilsBundleImpl').debug; export const getProxyForUrl: typeof import('../bundles/utils/node_modules/@types/proxy-from-env').getProxyForUrl = require('./utilsBundleImpl').getProxyForUrl; -export const glob: typeof import('../bundles/utils/node_modules/@types/glob') = require('./utilsBundleImpl').glob; export const HttpsProxyAgent: typeof import('../bundles/utils/node_modules/https-proxy-agent').HttpsProxyAgent = require('./utilsBundleImpl').HttpsProxyAgent; export const jpegjs: typeof import('../bundles/utils/node_modules/jpeg-js') = require('./utilsBundleImpl').jpegjs; export const lockfile: typeof import('../bundles/utils/node_modules/@types/proper-lockfile') = require('./utilsBundleImpl').lockfile; diff --git a/packages/playwright-ct-react/vitePlugin.d.ts b/packages/playwright-ct-react/vitePlugin.d.ts index afba441940..759cf63aee 100644 --- a/packages/playwright-ct-react/vitePlugin.d.ts +++ b/packages/playwright-ct-react/vitePlugin.d.ts @@ -19,6 +19,5 @@ import type { InlineConfig } from 'vite'; export default function(options?: { config?: InlineConfig, - include?: string port?: number, }): TestPlugin; diff --git a/packages/playwright-ct-svelte/vitePlugin.d.ts b/packages/playwright-ct-svelte/vitePlugin.d.ts index afba441940..759cf63aee 100644 --- a/packages/playwright-ct-svelte/vitePlugin.d.ts +++ b/packages/playwright-ct-svelte/vitePlugin.d.ts @@ -19,6 +19,5 @@ import type { InlineConfig } from 'vite'; export default function(options?: { config?: InlineConfig, - include?: string port?: number, }): TestPlugin; diff --git a/packages/playwright-ct-vue/vitePlugin.d.ts b/packages/playwright-ct-vue/vitePlugin.d.ts index 3d8f50b042..759cf63aee 100644 --- a/packages/playwright-ct-vue/vitePlugin.d.ts +++ b/packages/playwright-ct-vue/vitePlugin.d.ts @@ -19,6 +19,5 @@ import type { InlineConfig } from 'vite'; export default function(options?: { config?: InlineConfig, - include?: string, port?: number, }): TestPlugin; diff --git a/packages/playwright-test/src/plugins/vitePlugin.ts b/packages/playwright-test/src/plugins/vitePlugin.ts index b9a6d63bad..2a62491615 100644 --- a/packages/playwright-test/src/plugins/vitePlugin.ts +++ b/packages/playwright-test/src/plugins/vitePlugin.ts @@ -17,7 +17,6 @@ import type { PlaywrightTestConfig, TestPlugin } from '@playwright/test'; import fs from 'fs'; import path from 'path'; -import { glob } from 'playwright-core/lib/utilsBundle'; import type { InlineConfig, Plugin, ViteDevServer } from 'vite'; import { parse, traverse, types as t } from '../babelBundle'; import type { ComponentInfo } from '../tsxTransform'; @@ -29,7 +28,6 @@ export function createPlugin( registerFunction: string, frameworkPluginFactory: () => Plugin, options: { - include?: string, port?: number, config?: InlineConfig } = {}): TestPlugin { @@ -45,12 +43,17 @@ export function createPlugin( config.use!.baseURL = url; }, - setup: async () => { + setup: async suite => { viteConfig.root = viteConfig.root || configDir; viteConfig.plugins = viteConfig.plugins || [ frameworkPluginFactory() ]; - viteConfig.plugins.push(vitePlugin(registerFunction, options.include)); + const files = new Set(); + for (const project of suite.suites) { + for (const file of project.suites) + files.add(file.location!.file); + } + viteConfig.plugins.push(vitePlugin(registerFunction, [...files])); viteConfig.configFile = viteConfig.configFile || false; viteConfig.server = viteConfig.server || {}; viteConfig.server.port = port; @@ -67,19 +70,11 @@ export function createPlugin( const imports: Map = new Map(); -function vitePlugin(registerFunction: string, include: string | undefined): Plugin { +function vitePlugin(registerFunction: string, files: string[]): Plugin { return { name: 'playwright:component-index', configResolved: async config => { - const files = await new Promise((f, r) => { - glob(include || config.root + '/**/*.{test,spec}.[tj]s{x,}', {}, function(err, files) { - if (err) - r(err); - else - f(files); - }); - }); for (const file of files) { const text = await fs.promises.readFile(file, 'utf-8'); diff --git a/packages/playwright-test/src/runner.ts b/packages/playwright-test/src/runner.ts index cae4cee162..690a450324 100644 --- a/packages/playwright-test/src/runner.ts +++ b/packages/playwright-test/src/runner.ts @@ -371,7 +371,7 @@ export class Runner { } // 13. Run Global setup. - const globalTearDown = await this._performGlobalSetup(config); + const globalTearDown = await this._performGlobalSetup(config, rootSuite); if (!globalTearDown) return { status: 'failed' }; @@ -410,7 +410,7 @@ export class Runner { return result; } - private async _performGlobalSetup(config: FullConfigInternal): Promise<(() => Promise) | undefined> { + private async _performGlobalSetup(config: FullConfigInternal, rootSuite: Suite): Promise<(() => Promise) | undefined> { const result: FullResult = { status: 'passed' }; const pluginTeardowns: (() => Promise)[] = []; let globalSetupResult: any; @@ -443,7 +443,7 @@ export class Runner { // First run the plugins, if plugin is a web server we want it to run before the // config's global setup. for (const plugin of config._plugins) { - await plugin.setup?.(); + await plugin.setup?.(rootSuite); if (plugin.teardown) pluginTeardowns.unshift(plugin.teardown); } diff --git a/packages/playwright-test/types/test.d.ts b/packages/playwright-test/types/test.d.ts index 0fd85c6254..3088daf89d 100644 --- a/packages/playwright-test/types/test.d.ts +++ b/packages/playwright-test/types/test.d.ts @@ -2842,6 +2842,7 @@ export type PlaywrightTestProject = Project = Config; import type * as expectType from '@playwright/test/types/expect-types'; +import type { Suite } from '@playwright/test/types/testReporter'; type AsymmetricMatcher = Record; @@ -3643,7 +3644,10 @@ export interface TestPlugin { */ configure?(config: TestConfig, configDir: string): Promise; - setup?(): Promise; + /** + * @param suite + */ + setup?(suite: Suite): Promise; teardown?(): Promise; } diff --git a/tests/config/experimental.d.ts b/tests/config/experimental.d.ts index be8bd894aa..0da9312926 100644 --- a/tests/config/experimental.d.ts +++ b/tests/config/experimental.d.ts @@ -19109,6 +19109,7 @@ export type PlaywrightTestProject = Project = Config; import type * as expectType from '@playwright/test/types/expect-types'; +import type { Suite } from '@playwright/test/types/testReporter'; type AsymmetricMatcher = Record; @@ -20096,7 +20097,10 @@ export interface TestPlugin { */ configure?(config: TestConfig, configDir: string): Promise; - setup?(): Promise; + /** + * @param suite + */ + setup?(suite: Suite): Promise; teardown?(): Promise; } diff --git a/utils/generate_types/overrides-test.d.ts b/utils/generate_types/overrides-test.d.ts index 1c1161d3a1..8ef3ab7974 100644 --- a/utils/generate_types/overrides-test.d.ts +++ b/utils/generate_types/overrides-test.d.ts @@ -246,6 +246,7 @@ export type PlaywrightTestProject = Project = Config; import type * as expectType from '@playwright/test/types/expect-types'; +import type { Suite } from '@playwright/test/types/testReporter'; type AsymmetricMatcher = Record;