Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: close browser after run and throw errors #211

Merged
merged 1 commit into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
39 changes: 31 additions & 8 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@ import url from 'url';

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

// TODO(ergunsh): There was an issue with Eslint saying enums are not used
// After fixing it, update this to be an enum
const Status = {
Success: 1,
Error: 0,
} as const;

async function getStatus(
asyncFn: () => Promise<unknown>
): Promise<typeof Status['Success'] | typeof Status['Error']> {
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 +62,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