Skip to content

Commit

Permalink
fix: close browser after run and throw errors
Browse files Browse the repository at this point in the history
* We need to close the browser so that process
exits at the end of the runs
* We need to throw the error so that process
exits with exit code 1 when the replay
was not successful
  • Loading branch information
ergunsh committed Jun 29, 2022
1 parent b026345 commit 0596787
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
12 changes: 8 additions & 4 deletions src/CLIUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { join, isAbsolute, extname } from 'path';
import { pathToFileURL } from 'url';
import { cwd } from 'process';
import { PuppeteerRunnerOwningBrowserExtension } from '../lib/main.js';
import { Browser } from 'puppeteer';

export function getJSONFilesFromFolder(path: string): string[] {
return readdirSync(path)
Expand Down Expand Up @@ -80,8 +81,10 @@ export async function runFiles(
log: false,
headless: true,
}
): Promise<boolean> {
): Promise<void> {
let Extension = PuppeteerRunnerOwningBrowserExtension;
let browser: Browser | undefined;

if (opts.extension) {
const module = await import(
pathToFileURL(
Expand All @@ -99,7 +102,7 @@ export async function runFiles(
const object = JSON.parse(content);
const recording = parse(object);
const { default: puppeteer } = await import('puppeteer');
const browser = await puppeteer.launch({
browser = await puppeteer.launch({
headless: opts.headless,
});
const page = await browser.newPage();
Expand All @@ -109,8 +112,9 @@ export async function runFiles(
opts.log && console.log(`Finished running ${file}`);
} catch (err) {
opts.log && console.error(`Error running ${file}`, err);
return false;
throw err;
} finally {
await browser?.close();
}
}
return true;
}
35 changes: 27 additions & 8 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ import url from 'url';

const __dirname = path.dirname(url.fileURLToPath(import.meta.url));

enum Status {
Success,
Error,
}

async function getStatus(asyncFn: () => Promise<unknown>): Promise<Status> {
let error = undefined;
try {
await asyncFn();
} catch (err) {
error = err;
}

return error ? Status.Error : Status.Success;
}

describe('cli', () => {
describe('getHeadlessEnvVar', () => {
it('extracts the headless parameter from process.argv', () => {
Expand All @@ -42,33 +58,36 @@ describe('cli', () => {

describe('runFiles', () => {
it('is able to run successfully', async () => {
assert.isTrue(
await runFiles([path.join(__dirname, 'resources', 'replay.json')])
const result = await getStatus(() =>
runFiles([path.join(__dirname, 'resources', 'replay.json')])
);
assert.strictEqual(result, Status.Success);
});

it('is not able to run', async () => {
assert.isFalse(
await runFiles([path.join(__dirname, 'resources', 'replay-fail.json')])
const result = await getStatus(() =>
runFiles([path.join(__dirname, 'resources', 'replay-fail.json')])
);
assert.strictEqual(result, Status.Error);
});

it('is able to run able to run folder of recordings', async () => {
const recordings = getJSONFilesFromFolder(
path.join(__dirname, 'resources')
);

assert.isFalse(await runFiles([...recordings]));
const result = await getStatus(() => runFiles([...recordings]));
assert.strictEqual(result, Status.Error);
});

it('is able to run successfully with an extension', async () => {
assert.isTrue(
await runFiles([path.join(__dirname, 'resources', 'replay.json')], {
const result = await getStatus(() =>
runFiles([path.join(__dirname, 'resources', 'replay.json')], {
extension: path.join('examples', 'cli-extension', 'extension.js'),
headless: true,
log: false,
})
);
assert.strictEqual(result, Status.Success);
});
});

Expand Down

0 comments on commit 0596787

Please sign in to comment.