Skip to content

Commit

Permalink
Improved coverage handling
Browse files Browse the repository at this point in the history
  • Loading branch information
shannonmoeller committed Jan 19, 2018
1 parent 6ca6e3c commit 80012aa
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 35 deletions.
4 changes: 2 additions & 2 deletions bin/run-headless
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ function main() {
});
}

main();

process.on('unhandledRejection', err => {
throw err;
});

main();
61 changes: 37 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const fs = require('fs');
const path = require('path');
const puppeteer = require('puppeteer');

const CLOSE_GLOBAL = '__close__';
const COVERAGE_GLOBAL = '__coverage__';

const defaultHtml = `
<!doctype html>
<html lang="en">
Expand Down Expand Up @@ -40,37 +39,41 @@ function onError(err) {
throw err;
}

async function run({html, script, url}) {
async function writeCoverage(page) {
const coverage = await page.waitForFunction('window.__coverage__');
const output = await coverage.jsonValue();

// Filter out irrelevant coverage output.
// https://github.com/artberri/rollup-plugin-istanbul/issues/9
Object.keys(output).forEach(key => {
if (!key.includes(path.sep)) {
delete output[key];
}
});

fs.writeFileSync(
path.join(process.cwd(), '.nyc_output', `${Date.now()}.json`),
JSON.stringify(output),
'utf8'
);
}

async function runHeadless({html, script, url}) {
html = String(html || defaultHtml);
script = String(script || '');

if (script && !script.includes(CLOSE_GLOBAL)) {
script += `;window.${CLOSE_GLOBAL}();`;
}

const browser = await puppeteer.launch();
const page = await browser.newPage();

page.on('console', onConsole);
page.on('error', onError);
page.on('pageerror', onError);

const done = new Promise(async resolve => {
async function close() {
if (global[COVERAGE_GLOBAL]) {
Object.assign(
global[COVERAGE_GLOBAL],
await page.waitForFunction(`window.${COVERAGE_GLOBAL}`)
);
}

await browser.close();

resolve();
}

await page.exposeFunction(CLOSE_GLOBAL, close);
const closed = new Promise(async resolve => {
await page.exposeFunction('__close__', resolve);
});

const done = new Promise(async resolve => {
if (url) {
await page.goto(url, {waitUntil: 'networkidle0'});
} else {
Expand All @@ -80,6 +83,16 @@ async function run({html, script, url}) {
if (script) {
await page.addScriptTag({content: script});
}

await closed;

if (script && script.includes('__coverage__')) {
await writeCoverage(page);
}

await browser.close();

resolve();
});

done.browser = browser;
Expand All @@ -88,4 +101,4 @@ async function run({html, script, url}) {
return done;
}

module.exports = run;
module.exports = runHeadless;
11 changes: 10 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "run-headless",
"version": "1.0.0-9",
"version": "1.0.0-10",
"description": "The easiest way of running code in a modern headless browser environment.",
"scripts": {
"test": "run-s test-*",
"test-node": "nyc tape test/index.js | tap-diff",
"test": "nyc run-s test-*",
"test-node": "tape test/index.js | tap-diff",
"test-browser": "browserify test/index.js | bin/run-headless | tap-diff",
"test-url": "bin/run-headless --url 'https://google.com' --script 'console.log(document.title)'",
"test-url": "bin/run-headless --url 'https://google.com' --script 'console.log(document.title);window.__close__()'",
"report": "nyc report -r text-lcov | coveralls",
"pretest": "xo --fix",
"precommit": "npm run test && git add .",
Expand All @@ -31,6 +31,7 @@
"puppeteer": "^1.0.0"
},
"devDependencies": {
"blue-tape": "^1.0.0",
"browserify": "^15.2.0",
"coveralls": "^3.0.0",
"husky": "^0.14.3",
Expand Down
12 changes: 8 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const tape = require('tape');
const test = require('blue-tape');
const close = require('../close');
const foo = require('./foo.js');
const bar = require('./bar.js');

tape('should sum numbers', t => {
test('should sum numbers', async t => {
t.equal(foo + bar, 3);
t.comment('hello from test');
t.end();
});

tape.onFinish(close);
test('should sum numbers again', async t => {
t.equal(foo + bar, 3);
t.comment('hello from test');
});

test.onFinish(close);

0 comments on commit 80012aa

Please sign in to comment.