Skip to content

Commit

Permalink
chore: prepare to reuse test server from ui mode (6)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Mar 19, 2024
1 parent e4c8ce4 commit 5001d35
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 55 deletions.
27 changes: 7 additions & 20 deletions packages/playwright/src/isomorphic/teleReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ export class TeleReporterReceiver {
private _reporter: Partial<ReporterV2>;
private _tests = new Map<string, TeleTestCase>();
private _rootDir!: string;
private _listOnly = false;
private _config!: reporterTypes.FullConfig;

constructor(reporter: Partial<ReporterV2>, options: TeleReporterReceiverOptions) {
Expand All @@ -144,11 +143,16 @@ export class TeleReporterReceiver {
this._reporter = reporter;
}

dispatch(mode: 'list' | 'test', message: JsonEvent): Promise<void> | void {
reset() {
this._rootSuite.suites = [];
this._rootSuite.tests = [];
this._tests.clear();
}

dispatch(message: JsonEvent): Promise<void> | void {
const { method, params } = message;
if (method === 'onConfigure') {
this._onConfigure(params.config);
this._listOnly = mode === 'list';
return;
}
if (method === 'onProject') {
Expand Down Expand Up @@ -205,23 +209,6 @@ export class TeleReporterReceiver {
// Always update project in watch mode.
projectSuite._project = this._parseProject(project);
this._mergeSuitesInto(project.suites, projectSuite);

// Remove deleted tests when listing. Empty suites will be auto-filtered
// in the UI layer.
if (this._listOnly) {
const testIds = new Set<string>();
const collectIds = (suite: JsonSuite) => {
suite.tests.map(t => t.testId).forEach(testId => testIds.add(testId));
suite.suites.forEach(collectIds);
};
project.suites.forEach(collectIds);

const filterTests = (suite: TeleSuite) => {
suite.tests = suite.tests.filter(t => testIds.has(t.id));
suite.suites.forEach(filterTests);
};
filterTests(projectSuite);
}
}

private _onBegin() {
Expand Down
20 changes: 8 additions & 12 deletions packages/playwright/src/isomorphic/testServerConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ import * as events from './events';

export class TestServerConnection implements TestServerInterface, TestServerInterfaceEvents {
readonly onClose: events.Event<void>;
readonly onListReport: events.Event<any>;
readonly onTestReport: events.Event<any>;
readonly onReport: events.Event<any>;
readonly onStdio: events.Event<{ type: 'stderr' | 'stdout'; text?: string | undefined; buffer?: string | undefined; }>;
readonly onListChanged: events.Event<void>;
readonly onTestFilesChanged: events.Event<{ testFiles: string[] }>;
readonly onLoadTraceRequested: events.Event<{ traceUrl: string }>;

private _onCloseEmitter = new events.EventEmitter<void>();
private _onListReportEmitter = new events.EventEmitter<any>();
private _onTestReportEmitter = new events.EventEmitter<any>();
private _onReportEmitter = new events.EventEmitter<any>();
private _onStdioEmitter = new events.EventEmitter<{ type: 'stderr' | 'stdout'; text?: string | undefined; buffer?: string | undefined; }>();
private _onListChangedEmitter = new events.EventEmitter<void>();
private _onTestFilesChangedEmitter = new events.EventEmitter<{ testFiles: string[] }>();
Expand All @@ -42,8 +40,7 @@ export class TestServerConnection implements TestServerInterface, TestServerInte

constructor(wsURL: string) {
this.onClose = this._onCloseEmitter.event;
this.onListReport = this._onListReportEmitter.event;
this.onTestReport = this._onTestReportEmitter.event;
this.onReport = this._onReportEmitter.event;
this.onStdio = this._onStdioEmitter.event;
this.onListChanged = this._onListChangedEmitter.event;
this.onTestFilesChanged = this._onTestFilesChangedEmitter.event;
Expand Down Expand Up @@ -94,10 +91,8 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
}

private _dispatchEvent(method: string, params?: any) {
if (method === 'listReport')
this._onListReportEmitter.fire(params);
else if (method === 'testReport')
this._onTestReportEmitter.fire(params);
if (method === 'report')
this._onReportEmitter.fire(params);
else if (method === 'stdio')
this._onStdioEmitter.fire(params);
else if (method === 'listChanged')
Expand Down Expand Up @@ -142,9 +137,10 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
return await this._sendMessage('listFiles');
}

async listTests(params: { reporter?: string | undefined; fileNames?: string[] | undefined; }): Promise<void> {
await this._sendMessage('listTests', params);
async listTests(params: { reporter?: string | undefined; fileNames?: string[] | undefined; }): Promise<{ report: any[] }> {
return await this._sendMessage('listTests', params);
}

async runTests(params: { reporter?: string | undefined; locations?: string[] | undefined; grep?: string | undefined; testIds?: string[] | undefined; headed?: boolean | undefined; oneWorker?: boolean | undefined; trace?: 'off' | 'on' | undefined; projects?: string[] | undefined; reuseContext?: boolean | undefined; connectWsEndpoint?: string | undefined; }): Promise<void> {
await this._sendMessage('runTests', params);
}
Expand Down
8 changes: 3 additions & 5 deletions packages/playwright/src/isomorphic/testServerInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export interface TestServerInterface {
listTests(params: {
reporter?: string;
fileNames?: string[];
}): Promise<void>;
}): Promise<{ report: any[] }>;

runTests(params: {
reporter?: string;
Expand All @@ -76,8 +76,7 @@ export interface TestServerInterface {

export interface TestServerInterfaceEvents {
onClose: Event<void>;
onListReport: Event<any>;
onTestReport: Event<any>;
onReport: Event<any>;
onStdio: Event<{ type: 'stdout' | 'stderr', text?: string, buffer?: string }>;
onListChanged: Event<void>;
onTestFilesChanged: Event<{ testFiles: string[] }>;
Expand All @@ -86,8 +85,7 @@ export interface TestServerInterfaceEvents {

export interface TestServerInterfaceEventEmitters {
dispatchEvent(event: 'close', params: {}): void;
dispatchEvent(event: 'listReport', params: any): void;
dispatchEvent(event: 'testReport', params: any): void;
dispatchEvent(event: 'report', params: any): void;
dispatchEvent(event: 'stdio', params: { type: 'stdout' | 'stderr', text?: string, buffer?: string }): void;
dispatchEvent(event: 'listChanged', params: {}): void;
dispatchEvent(event: 'testFilesChanged', params: { testFiles: string[] }): void;
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright/src/reporters/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export async function createMergedReport(config: FullConfigInternal, dir: string
for (const event of events) {
if (event.method === 'onEnd')
printStatus(`building final report`);
await receiver.dispatch('test', event);
await receiver.dispatch(event);
if (event.method === 'onEnd')
printStatus(`finished building report`);
}
Expand Down
1 change: 0 additions & 1 deletion packages/playwright/src/runner/reporters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ function reporterOptions(config: FullConfigInternal, mode: 'list' | 'test' | 'ui
return {
configDir: config.configDir,
_send: send,
_mode: mode,
};
}

Expand Down
12 changes: 9 additions & 3 deletions packages/playwright/src/runner/testServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,17 @@ class TestServerDispatcher implements TestServerInterface {
}

async listTests(params: { reporter?: string; fileNames: string[]; }) {
this._queue = this._queue.then(() => this._innerListTests(params)).catch(printInternalError);
let report: any[] = [];
this._queue = this._queue.then(async () => {
report = await this._innerListTests(params);
}).catch(printInternalError);
await this._queue;
return { report };
}

private async _innerListTests(params: { reporter?: string; fileNames?: string[]; }) {
const wireReporter = await createReporterForTestServer(this._config, params.reporter || require.resolve('./uiModeReporter'), 'list', e => this._dispatchEvent('listReport', e));
const report: any[] = [];
const wireReporter = await createReporterForTestServer(this._config, params.reporter || require.resolve('./uiModeReporter'), 'list', e => report.push(e));
const reporter = new InternalReporter(wireReporter);
this._config.cliArgs = params.fileNames || [];
this._config.cliListOnly = true;
Expand All @@ -196,6 +201,7 @@ class TestServerDispatcher implements TestServerInterface {
projectOutputs.add(p.project.outputDir);
}
this._globalWatcher.update([...projectDirs], [...projectOutputs], false);
return report;
}

async runTests(params: { reporter?: string; locations?: string[] | undefined; grep?: string | undefined; testIds?: string[] | undefined; headed?: boolean | undefined; oneWorker?: boolean | undefined; trace?: 'off' | 'on' | undefined; projects?: string[] | undefined; reuseContext?: boolean | undefined; connectWsEndpoint?: string | undefined; }) {
Expand All @@ -215,7 +221,7 @@ class TestServerDispatcher implements TestServerInterface {
this._config.testIdMatcher = id => !testIdSet || testIdSet.has(id);

const reporters = await createReporters(this._config, 'ui');
reporters.push(await createReporterForTestServer(this._config, params.reporter || require.resolve('./uiModeReporter'), 'list', e => this._dispatchEvent('testReport', e)));
reporters.push(await createReporterForTestServer(this._config, params.reporter || require.resolve('./uiModeReporter'), 'list', e => this._dispatchEvent('report', e)));
const reporter = new InternalReporter(new Multiplexer(reporters));
const taskRunner = createTaskRunnerForWatch(this._config, reporter);
const testRun = new TestRun(this._config, reporter);
Expand Down
13 changes: 9 additions & 4 deletions packages/trace-viewer/src/ui/teleSuiteUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,16 @@ export class TeleSuiteUpdater {
};
}

dispatch(mode: 'test' | 'list', message: any) {
processListReport(report: any[]) {
this._receiver.reset();
for (const message of report)
this._receiver.dispatch(message);
}

processTestReport(message: any) {
// The order of receiver dispatches matters here, we want to assign `lastRunTestCount`
// before we use it.
if (mode === 'test')
this._lastRunReceiver?.dispatch('test', message)?.catch(() => {});
this._receiver.dispatch(mode, message)?.catch(() => {});
this._lastRunReceiver?.dispatch(message)?.catch(() => {});
this._receiver.dispatch(message)?.catch(() => {});
}
}
16 changes: 7 additions & 9 deletions packages/trace-viewer/src/ui/uiModeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -648,12 +648,14 @@ const refreshRootSuite = async (testServerConnection: TestServerConnection): Pro
},
pathSeparator,
});
return testServerConnection.listTests({});
const { report } = await testServerConnection.listTests({});
teleSuiteUpdater?.processListReport(report);
};

const wireConnectionListeners = (testServerConnection: TestServerConnection) => {
testServerConnection.onListChanged(() => {
testServerConnection.listTests({}).catch(() => {});
testServerConnection.onListChanged(async () => {
const { report } = await testServerConnection.listTests({});
teleSuiteUpdater?.processListReport(report);
});

testServerConnection.onTestFilesChanged(params => {
Expand All @@ -669,12 +671,8 @@ const wireConnectionListeners = (testServerConnection: TestServerConnection) =>
}
});

testServerConnection.onListReport(params => {
teleSuiteUpdater?.dispatch('list', params);
});

testServerConnection.onTestReport(params => {
teleSuiteUpdater?.dispatch('test', params);
testServerConnection.onReport(params => {
teleSuiteUpdater?.processTestReport(params);
});

xtermDataSource.resize = (cols, rows) => {
Expand Down

0 comments on commit 5001d35

Please sign in to comment.