From b1a6fff973a63e3e54cc285430f812b96b1b511d Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Fri, 9 Mar 2018 22:58:40 +0100 Subject: [PATCH] Use mocha for testing --- lighthouse-extension/package.json | 2 +- lighthouse-extension/test/extension.js | 123 +++++++++++++------------ 2 files changed, 67 insertions(+), 58 deletions(-) diff --git a/lighthouse-extension/package.json b/lighthouse-extension/package.json index 0edce914e292..e773b101c15f 100644 --- a/lighthouse-extension/package.json +++ b/lighthouse-extension/package.json @@ -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", diff --git a/lighthouse-extension/test/extension.js b/lighthouse-extension/test/extension.js index 10fc09d933e1..2af910204963 100644 --- a/lighthouse-extension/test/extension.js +++ b/lighthouse-extension/test/extension.js @@ -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; @@ -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 });