Skip to content

Commit 8412cce

Browse files
Merge pull request #1274 from gemini-testing/TESTPLANE-1032.async_events
feat: make CLI and NEW_BROWSER events async
2 parents 3d3ab7e + 5d50304 commit 8412cce

9 files changed

Lines changed: 24 additions & 20 deletions

File tree

src/cli/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export const run = async (opts: TestplaneRunOpts = {}): Promise<void> => {
145145
registerCmd(program, testplane);
146146
}
147147

148-
testplane.extendCli(program);
148+
await testplane.extendCli(program);
149149

150150
program.parse(process.argv);
151151
};

src/testplane.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ export class Testplane extends BaseTestplane {
101101
this.testsTracker = null;
102102
}
103103

104-
extendCli(parser: Command): void {
105-
this.emit(MasterEvents.CLI, parser);
104+
async extendCli(parser: Command): Promise<void> {
105+
await this.emitAndWait(MasterEvents.CLI, parser);
106106
}
107107

108108
addFileToRemove(path: string): void {

src/types/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ export type MasterEventHandler<T extends BaseTestplane> = {
256256
(event: Events["DOM_SNAPSHOTS"], callback: (context: TestContext, data: SnapshotsData) => void): T;
257257
(event: Events["TEST_DEPENDENCIES"], callback: (context: TestDepsContext, data: TestDepsData) => void): T;
258258

259-
(event: Events["CLI"], callback: (commander: commander.CommanderStatic) => void): T;
259+
(event: Events["CLI"], callback: (commander: commander.CommanderStatic) => void | Promise<void>): T;
260260
(event: Events["BEGIN"], callback: () => void): T;
261261
(event: Events["END"], callback: () => void): T;
262262
(event: Events["BEFORE_FILE_READ"], callback: (data: BeforeFileReadData) => void): T;
@@ -268,7 +268,6 @@ export type MasterEventHandler<T extends BaseTestplane> = {
268268

269269
(event: Events["UPDATE_REFERENCE"], callback: (data: { state: string; refImg: RefImageInfo }) => void): T;
270270
(event: Events["ADD_FILE_TO_REMOVE"], callback: (path: string) => void): T;
271-
(event: Events["NEW_BROWSER"], callback: SyncSessionEventCallback): T;
272271
};
273272

274273
export type WorkerEventHandler<T extends BaseTestplane> = {
@@ -278,7 +277,7 @@ export type WorkerEventHandler<T extends BaseTestplane> = {
278277
(event: Events["AFTER_TESTS_READ"], callback: (collection: TestCollection) => void): T;
279278

280279
(event: Events["UPDATE_REFERENCE"], callback: (data: { state: string; refImg: RefImageInfo }) => void): T;
281-
(event: Events["NEW_BROWSER"], callback: SyncSessionEventCallback): T;
280+
(event: Events["NEW_BROWSER"], callback: AsyncSessionEventCallback): T;
282281
};
283282

284283
export type CookieSameSite = "Strict" | "Lax" | "None";

src/worker/runner/browser-pool.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ module.exports = class BrowserPool {
2727
try {
2828
await browser.init({ sessionId, sessionCaps, sessionOpts }, this._calibrator);
2929

30-
this._emitter.emit(WorkerEvents.NEW_BROWSER, browser.publicAPI, { browserId: browser.id, browserVersion });
30+
await this._emitter.emitAndWait(WorkerEvents.NEW_BROWSER, browser.publicAPI, {
31+
browserId: browser.id,
32+
browserVersion,
33+
});
3134

3235
return browser;
3336
} catch (error) {

test/src/cli/commands/config/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ describe("cli/commands/config", () => {
1111
let consoleInfoStub: SinonStub;
1212
let jsonStringifyStub: SinonSpy;
1313

14-
const config_ = async (options: string[] = [], cli: { run: VoidFunction } = testplaneCli): Promise<void> => {
14+
const config_ = async (options: string[] = [], cli: { run: () => Promise<void> } = testplaneCli): Promise<void> => {
1515
process.argv = ["foo/bar/node", "foo/bar/script", "config", ...options];
16-
cli.run();
16+
await cli.run();
1717

1818
await (Command.prototype.action as SinonStub).lastCall.returnValue;
1919
};

test/src/cli/commands/list-browsers/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ describe("cli/commands/list-browsers", () => {
1414
let loggerErrorStub: SinonStub;
1515
let consoleInfoStub: SinonStub;
1616

17-
const listBrowsers_ = async (options: string[] = [], cli: { run: VoidFunction } = testplaneCli): Promise<void> => {
17+
const listBrowsers_ = async (
18+
options: string[] = [],
19+
cli: { run: () => Promise<void> } = testplaneCli,
20+
): Promise<void> => {
1821
process.argv = ["foo/bar/node", "foo/bar/script", "list-browsers", ...options];
19-
cli.run();
22+
await cli.run();
2023

2124
await (Command.prototype.action as SinonStub).lastCall.returnValue;
2225
};

test/src/cli/commands/list-tests/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ describe("cli/commands/list-tests", () => {
1313
const sandbox = sinon.createSandbox();
1414
let testplaneCli: typeof testplaneCliOriginal;
1515

16-
const listTests_ = async (argv: string = "", cli: { run: VoidFunction } = testplaneCli): Promise<void> => {
16+
const listTests_ = async (argv: string = "", cli: { run: () => Promise<void> } = testplaneCli): Promise<void> => {
1717
process.argv = ["foo/bar/node", "foo/bar/script", "list-tests", ...argv.split(" ")].filter(Boolean);
18-
cli.run();
18+
await cli.run();
1919

2020
await (Command.prototype.action as SinonStub).lastCall.returnValue;
2121
};

test/src/testplane.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,14 @@ describe("testplane", () => {
137137
});
138138

139139
describe("extendCli", () => {
140-
it("should emit CLI event with passed parser", () => {
140+
it("should emit CLI event with passed parser", async () => {
141141
const testplane = mkTestplane_();
142142
const onCli = sinon.spy().named("onCli");
143143
const parser = { foo: "bar" };
144144

145145
testplane.on(RunnerEvents.CLI, onCli);
146146

147-
testplane.extendCli(parser);
147+
await testplane.extendCli(parser);
148148

149149
assert.calledOnceWith(onCli, parser);
150150
});

test/src/worker/runner/browser-pool.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
"use strict";
22

3-
const EventEmitter = require("events").EventEmitter;
43
const _ = require("lodash");
54
const { ExistingBrowser } = require("src/browser/existing-browser");
65
const BrowserPool = require("src/worker/runner/browser-pool");
76
const { Calibrator } = require("src/browser/calibrator");
8-
const { WorkerEvents: RunnerEvents } = require("src/events");
7+
const { WorkerEvents: RunnerEvents, AsyncEmitter } = require("src/events");
98
const ipc = require("src/utils/ipc");
109

1110
describe("worker/browser-pool", () => {
@@ -20,7 +19,7 @@ describe("worker/browser-pool", () => {
2019
const createPool = opts => {
2120
opts = _.defaults(opts || {}, {
2221
config: stubConfig(),
23-
emitter: new EventEmitter(),
22+
emitter: new AsyncEmitter(),
2423
});
2524

2625
return BrowserPool.create(opts.config, opts.emitter);
@@ -48,7 +47,7 @@ describe("worker/browser-pool", () => {
4847
describe("getBrowser", () => {
4948
it("should create browser with correct args", async () => {
5049
const config = stubConfig();
51-
const emitter = new EventEmitter();
50+
const emitter = new AsyncEmitter();
5251
const browserPool = createPool({ config, emitter });
5352
ExistingBrowser.create.returns(stubBrowser({ browserId: "bro-id" }));
5453

@@ -81,7 +80,7 @@ describe("worker/browser-pool", () => {
8180
});
8281

8382
it('should emit "NEW_BROWSER" event on creating of a browser', async () => {
84-
const emitter = new EventEmitter();
83+
const emitter = new AsyncEmitter();
8584
const onNewBrowser = sandbox.spy().named("onNewBrowser");
8685
const browserPool = createPool({ emitter });
8786

0 commit comments

Comments
 (0)