feat(plugins): expose suite to plugin setup (#13807)

This commit is contained in:
Pavel Feldman 2022-04-28 07:16:17 -08:00 committed by GitHub
parent b9f47558b5
commit e756528ad2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 25 additions and 26 deletions

View File

@ -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

View File

@ -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';

View File

@ -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;

View File

@ -19,6 +19,5 @@ import type { InlineConfig } from 'vite';
export default function(options?: {
config?: InlineConfig,
include?: string
port?: number,
}): TestPlugin;

View File

@ -19,6 +19,5 @@ import type { InlineConfig } from 'vite';
export default function(options?: {
config?: InlineConfig,
include?: string
port?: number,
}): TestPlugin;

View File

@ -19,6 +19,5 @@ import type { InlineConfig } from 'vite';
export default function(options?: {
config?: InlineConfig,
include?: string,
port?: number,
}): TestPlugin;

View File

@ -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<string>();
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<string, ComponentInfo> = 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<string[]>((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');

View File

@ -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<void>) | undefined> {
private async _performGlobalSetup(config: FullConfigInternal, rootSuite: Suite): Promise<(() => Promise<void>) | undefined> {
const result: FullResult = { status: 'passed' };
const pluginTeardowns: (() => Promise<void>)[] = [];
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);
}

View File

@ -2842,6 +2842,7 @@ export type PlaywrightTestProject<TestArgs = {}, WorkerArgs = {}> = Project<Play
export type PlaywrightTestConfig<TestArgs = {}, WorkerArgs = {}> = Config<PlaywrightTestOptions & TestArgs, PlaywrightWorkerOptions & WorkerArgs>;
import type * as expectType from '@playwright/test/types/expect-types';
import type { Suite } from '@playwright/test/types/testReporter';
type AsymmetricMatcher = Record<string, any>;
@ -3643,7 +3644,10 @@ export interface TestPlugin {
*/
configure?(config: TestConfig, configDir: string): Promise<void>;
setup?(): Promise<void>;
/**
* @param suite
*/
setup?(suite: Suite): Promise<void>;
teardown?(): Promise<void>;
}

View File

@ -19109,6 +19109,7 @@ export type PlaywrightTestProject<TestArgs = {}, WorkerArgs = {}> = Project<Play
export type PlaywrightTestConfig<TestArgs = {}, WorkerArgs = {}> = Config<PlaywrightTestOptions & TestArgs, PlaywrightWorkerOptions & WorkerArgs>;
import type * as expectType from '@playwright/test/types/expect-types';
import type { Suite } from '@playwright/test/types/testReporter';
type AsymmetricMatcher = Record<string, any>;
@ -20096,7 +20097,10 @@ export interface TestPlugin {
*/
configure?(config: TestConfig, configDir: string): Promise<void>;
setup?(): Promise<void>;
/**
* @param suite
*/
setup?(suite: Suite): Promise<void>;
teardown?(): Promise<void>;
}

View File

@ -246,6 +246,7 @@ export type PlaywrightTestProject<TestArgs = {}, WorkerArgs = {}> = Project<Play
export type PlaywrightTestConfig<TestArgs = {}, WorkerArgs = {}> = Config<PlaywrightTestOptions & TestArgs, PlaywrightWorkerOptions & WorkerArgs>;
import type * as expectType from '@playwright/test/types/expect-types';
import type { Suite } from '@playwright/test/types/testReporter';
type AsymmetricMatcher = Record<string, any>;