Skip to content

Commit

Permalink
fix(runner): should allocate and dispose on run
Browse files Browse the repository at this point in the history
  • Loading branch information
PaperStrike committed Oct 29, 2023
1 parent 9c94d70 commit df5834b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 40 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ If you want Node.js API,
```ts
import { Runner } from 'wrightplay/node';

// Or manually calling `runner.dispose()` to release resources
await using runner = new Runner({
const runner = new Runner({
setup: 'test/setup.ts',
tests: 'test/**/*.spec.ts',
});
Expand Down
2 changes: 1 addition & 1 deletion src/cli/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const program = command
const runnerOptionsList = await parseRunnerOptionsFromCLI(testAndEntries, options);
await runnerOptionsList.reduce(async (last, runnerOptions) => {
await last;
await using runner = new Runner(runnerOptions);
const runner = new Runner(runnerOptions);
const exitCode = await runner.runTests();
process.exitCode ||= exitCode;
}, Promise.resolve());
Expand Down
79 changes: 42 additions & 37 deletions src/server/Runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,26 @@ export interface RunnerOptions {

export type BrowserServer = playwright.BrowserServer;

export default class Runner implements AsyncDisposable {
export default class Runner {
readonly cwd: string;

/**
* File to run before the test files.
*/
readonly setupFile: string | undefined;

/**
* Test file patterns.
*/
readonly testPatterns: string | string[];

/**
* Additional entry points to build. The output name must be explicitly specified.
* You can use this option to build workers.
* @see [Entry points | esbuild - API](https://esbuild.github.io/api/#entry-points)
*/
readonly entryPoints: Record<string, string>;

/**
* Monitor test file changes and trigger automatic test reruns.
*/
Expand All @@ -84,20 +101,16 @@ export default class Runner implements AsyncDisposable {
readonly browserType: BrowserTypeName;

/**
* Whether to run browser in headless mode.
* @see BrowserServerOptions.headless
*/
readonly headless: boolean;

/**
* File server for the test files.
* Options used to launch the test browser server. Defaults to the Playwright defaults.
* @see playwright.BrowserType.launchServer
*/
readonly testServer: TestServer;
readonly browserServerOptions: BrowserServerOptions;

/**
* Browser server launch promise.
* Whether to run browser in headless mode.
* @see BrowserServerOptions.headless
*/
readonly browserServerPromise: Promise<BrowserServer>;
readonly headless: boolean;

/**
* Directory to save the coverage output file. Defaults to `NODE_V8_COVERAGE`
Expand All @@ -121,25 +134,19 @@ export default class Runner implements AsyncDisposable {
headless = browserServerOptions.headless ?? !browserServerOptions.devtools,
noCov = browser !== 'chromium',
}: RunnerOptions) {
this.setupFile = setup;
this.testPatterns = tests;
this.entryPoints = entryPoints;
this.watch = watch;
this.browserType = browser;
this.headless = headless;

this.cwd = path.resolve(cwd);

this.testServer = new TestServer({
cwd: this.cwd,
setup,
tests,
entryPoints,
watch,
uuid: this.uuid,
});

this.browserServerPromise = playwright[browser].launchServer({
this.browserServerOptions = {
...browserServerOptions,
headless,
});
};

// Resolve coverage folder. Defaults to NODE_V8_COVERAGE
if (!noCov && process.env.NODE_V8_COVERAGE && browser === 'chromium') {
Expand All @@ -153,11 +160,20 @@ export default class Runner implements AsyncDisposable {
async runTests(): Promise<number> {
await using stack = new AsyncDisposableStack();

const testServer = stack.use(new TestServer({
cwd: this.cwd,
setup: this.setupFile,
tests: this.testPatterns,
entryPoints: this.entryPoints,
watch: this.watch,
uuid: this.uuid,
}));

const [addressInfo, browserServer] = await Promise.all([
this.testServer.launch(),
this.browserServerPromise,
testServer.launch(),
playwright[this.browserType].launchServer(this.browserServerOptions),
]);
stack.defer(() => this.testServer.close());
stack.defer(() => browserServer.close());

const browser = await playwright[this.browserType].connect(browserServer.wsEndpoint());
stack.defer(() => browser.close());
Expand All @@ -176,7 +192,7 @@ export default class Runner implements AsyncDisposable {
const page = await browserContext.newPage();
stack.defer(() => page.close());

const { cwd, browserType, testServer } = this;
const { cwd, browserType } = this;
const { sourceMapPayloads, httpServer } = testServer;
const bLog = stack.use(new BrowserLogger({
browserType,
Expand Down Expand Up @@ -262,15 +278,4 @@ export default class Runner implements AsyncDisposable {

return exitCodePromise;
}

async dispose() {
await Promise.all([
this.browserServerPromise.then((browserServer) => browserServer.close()),
this.testServer[Symbol.asyncDispose](),
]);
}

[Symbol.asyncDispose]() {
return this.dispose();
}
}

0 comments on commit df5834b

Please sign in to comment.