Skip to content

Commit

Permalink
Use mocha for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Mar 9, 2018
1 parent dc0ac20 commit b1a6fff
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 58 deletions.
2 changes: 1 addition & 1 deletion lighthouse-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"watch": "gulp watch",
"build": "gulp build:production",
"test:e2e": "node test/extension.js"
"test:e2e": "../node_modules/.bin/_mocha test/extension.js"
},
"devDependencies": {
"brfs": "^1.4.3",
Expand Down
123 changes: 66 additions & 57 deletions lighthouse-extension/test/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ const assertAuditElements = async ({category, expected, selector}) => {
assert.equal(expected, elementCount);
};

const assertReport = async () => {
const categories = await extensionPage.$$(`#${lighthouseCategories.join(',#')}`);
assert.equal(categories.length, lighthouseCategories.length);

await lighthouseCategories.forEach(async (category) => {
let selector = '.lh-audit';
if (category === 'performance') {
selector = '.lh-audit,.lh-timeline-metric,.lh-perf-hint,.lh-filmstrip';
}

await assertAuditElements({
category,
expected: config.categories[category].audits.length,
selector,
});
});
};

describe('Lighthouse chrome extension', () => {
const manifestLocation = path.join(lighthouseExtensionPath, 'manifest.json');
let originalManifest;
Expand All @@ -48,68 +66,59 @@ describe('Lighthouse chrome extension', () => {
});

it('completes an end-to-end run', async () => {
const browser = await puppeteer.launch({
headless: false,
executablePath: process.env.CHROME_PATH,
args: [
`--disable-extensions-except=${lighthouseExtensionPath}`,
`--load-extension=${lighthouseExtensionPath}`,
],
});
let browser;
try {
browser = await puppeteer.launch({
headless: false,
executablePath: process.env.CHROME_PATH,
args: [
`--disable-extensions-except=${lighthouseExtensionPath}`,
`--load-extension=${lighthouseExtensionPath}`,
],
});

const page = await browser.newPage();
await page.goto('https://www.paulirish.com', {waitUntil: 'networkidle2'});
const targets = await browser.targets();
const extensionTarget = targets.find(({_targetInfo}) => {
return _targetInfo.title === 'Lighthouse' &&
_targetInfo.type === 'background_page';
});

if (!extensionTarget) {
return await browser.close();
}

const page = await browser.newPage();
await page.goto('https://www.paulirish.com', {waitUntil: 'networkidle2'});
const targets = await browser.targets();
const extensionTarget = targets.find(({_targetInfo}) => {
return _targetInfo.title === 'Lighthouse' &&
_targetInfo.type === 'background_page';
});
const client = await extensionTarget.createCDPSession();
const lighthouseResult = await client.send(
'Runtime.evaluate',
{
expression: `runLighthouseInExtension({
restoreCleanState: true,
}, ${JSON.stringify(lighthouseCategories)})`,
awaitPromise: true,
returnByValue: true,
}
);

if (extensionTarget) {
try {
const client = await extensionTarget.createCDPSession();
const lighthouseResult = await client.send(
'Runtime.evaluate',
{
expression: `runLighthouseInExtension({
restoreCleanState: true,
}, ${JSON.stringify(lighthouseCategories)})`,
awaitPromise: true,
returnByValue: true,
}
);

if (lighthouseResult.exceptionDetails) {
if (lighthouseResult.exceptionDetails.exception) {
throw new Error(lighthouseResult.exceptionDetails.exception.description);
}

throw new Error(lighthouseResult.exceptionDetails.text);
if (lighthouseResult.exceptionDetails) {
if (lighthouseResult.exceptionDetails.exception) {
throw new Error(lighthouseResult.exceptionDetails.exception.description);
}

extensionPage = (await browser.pages())
.find(page => page.url().includes('blob:chrome-extension://'));

const categories = await extensionPage.$$(`#${lighthouseCategories.join(',#')}`);
assert.equal(categories.length, lighthouseCategories.length);

await lighthouseCategories.forEach(async (category) => {
let selector = '.lh-audit';
if (category === 'performance') {
selector = '.lh-audit,.lh-timeline-metric,.lh-perf-hint,.lh-filmstrip';
}

await assertAuditElements({
category,
expected: config.categories[category].audits.length,
selector,
});
});
} catch (err) {
assert.ok(false, err.message);
throw new Error(lighthouseResult.exceptionDetails.text);
}

extensionPage = (await browser.pages())
.find(page => page.url().includes('blob:chrome-extension://'));

await assertReport();
} catch (err) {
assert.ok(false, err.message);
}

await browser.close();
}):
if (browser) {
await browser.close();
}
}).timeout(90000); // 90s
});

0 comments on commit b1a6fff

Please sign in to comment.