chore: reuse test server wire for reporter events (#29640)

This commit is contained in:
Pavel Feldman 2024-02-23 15:51:20 -08:00 committed by GitHub
parent 8d9c67ac5d
commit 6b5e273b6d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 12 deletions

View file

@ -316,8 +316,8 @@ export function loadGlobalHook(config: FullConfigInternal, file: string): Promis
return requireOrImportDefaultFunction(path.resolve(config.config.rootDir, file), false); return requireOrImportDefaultFunction(path.resolve(config.config.rootDir, file), false);
} }
export function loadReporter(config: FullConfigInternal, file: string): Promise<new (arg?: any) => Reporter> { export function loadReporter(config: FullConfigInternal | undefined, file: string): Promise<new (arg?: any) => Reporter> {
return requireOrImportDefaultFunction(path.resolve(config.config.rootDir, file), true); return requireOrImportDefaultFunction(config ? path.resolve(config.config.rootDir, file) : file, true);
} }
function sourceMapSources(file: string, cache: Map<string, string[]>): string[] { function sourceMapSources(file: string, cache: Map<string, string[]>): string[] {

View file

@ -32,6 +32,8 @@ import type { FullConfigInternal } from '../common/config';
import type { TestServerInterface } from './testServerInterface'; import type { TestServerInterface } from './testServerInterface';
import { serializeError } from '../util'; import { serializeError } from '../util';
import { prepareErrorStack } from '../reporters/base'; import { prepareErrorStack } from '../reporters/base';
import { loadReporter } from './loadUtils';
import { wrapReporterAsV2 } from '../reporters/reporterV2';
export async function runTestServer() { export async function runTestServer() {
if (restartWithExperimentalTsEsm(undefined, true)) if (restartWithExperimentalTsEsm(undefined, true))
@ -117,10 +119,10 @@ class Dispatcher implements TestServerInterface {
reporter: string; reporter: string;
env: NodeJS.ProcessEnv; env: NodeJS.ProcessEnv;
}) { }) {
this._syncEnv(params.env);
const config = await this._loadConfig(params.configFile); const config = await this._loadConfig(params.configFile);
config.cliArgs = params.locations || []; config.cliArgs = params.locations || [];
const reporter = new InternalReporter(new Multiplexer(await createReporters(config, 'list', [[params.reporter]]))); const wireReporter = await this._createReporter(params.reporter);
const reporter = new InternalReporter(new Multiplexer([wireReporter]));
const taskRunner = createTaskRunnerForList(config, reporter, 'out-of-process', { failOnLoadErrors: true }); const taskRunner = createTaskRunnerForList(config, reporter, 'out-of-process', { failOnLoadErrors: true });
const testRun = new TestRun(config, reporter); const testRun = new TestRun(config, reporter);
reporter.onConfigure(config.config); reporter.onConfigure(config.config);
@ -148,11 +150,9 @@ class Dispatcher implements TestServerInterface {
reuseContext?: boolean; reuseContext?: boolean;
connectWsEndpoint?: string; connectWsEndpoint?: string;
}) { }) {
this._syncEnv(params.env);
await this._stopTests(); await this._stopTests();
const overrides: ConfigCLIOverrides = { const overrides: ConfigCLIOverrides = {
additionalReporters: [[params.reporter]],
repeatEach: 1, repeatEach: 1,
retries: 0, retries: 0,
preserveOutputDir: true, preserveOutputDir: true,
@ -171,7 +171,9 @@ class Dispatcher implements TestServerInterface {
config.cliGrep = params.grep; config.cliGrep = params.grep;
config.cliProjectFilter = params.projects?.length ? params.projects : undefined; config.cliProjectFilter = params.projects?.length ? params.projects : undefined;
const reporter = new InternalReporter(new Multiplexer(await createReporters(config, 'run'))); const wireReporter = await this._createReporter(params.reporter);
const configReporters = await createReporters(config, 'run');
const reporter = new InternalReporter(new Multiplexer([...configReporters, wireReporter]));
const taskRunner = createTaskRunnerForTestServer(config, reporter); const taskRunner = createTaskRunnerForTestServer(config, reporter);
const testRun = new TestRun(config, reporter); const testRun = new TestRun(config, reporter);
reporter.onConfigure(config.config); reporter.onConfigure(config.config);
@ -218,9 +220,10 @@ class Dispatcher implements TestServerInterface {
return loadConfig({ resolvedConfigFile: configFile, configDir: path.dirname(configFile) }, overrides); return loadConfig({ resolvedConfigFile: configFile, configDir: path.dirname(configFile) }, overrides);
} }
private _syncEnv(env: NodeJS.ProcessEnv) { private async _createReporter(file: string) {
for (const name in env) const reporterConstructor = await loadReporter(undefined, file);
process.env[name] = env[name]; const instance = new reporterConstructor((message: any) => this._dispatchEvent('report', message));
return wrapReporterAsV2(instance);
} }
} }

View file

@ -34,14 +34,12 @@ export interface TestServerInterface {
configFile: string; configFile: string;
locations: string[]; locations: string[];
reporter: string; reporter: string;
env: NodeJS.ProcessEnv;
}): Promise<void>; }): Promise<void>;
test(params: { test(params: {
configFile: string; configFile: string;
locations: string[]; locations: string[];
reporter: string; reporter: string;
env: NodeJS.ProcessEnv;
headed?: boolean; headed?: boolean;
oneWorker?: boolean; oneWorker?: boolean;
trace?: 'on' | 'off'; trace?: 'on' | 'off';
@ -64,5 +62,6 @@ export interface TestServerInterface {
} }
export interface TestServerEvents { export interface TestServerEvents {
on(event: 'report', listener: (params: any) => void): void;
on(event: 'stdio', listener: (params: { type: 'stdout' | 'stderr', text?: string, buffer?: string }) => void): void; on(event: 'stdio', listener: (params: { type: 'stdout' | 'stderr', text?: string, buffer?: string }) => void): void;
} }