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

allow access to chrome coverage from test-script #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

schrotie
Copy link

Hey, I love mocha-headless-chrome, thanks!

The main reason I use it is because I use modern tech which requires Chrome if I don't want to transpile and polyfill (and the polyfills seem pretty hairy to get running in jsdom). I'm hell bent on keeping my dev process clean, i.e. no transpilation or polyfills during dev if I can help it. Istanbul requires tranpilation. However, I found a way to get Chromes built-in coverage information and use that in an assert in my tests. Just had to add a tiny little bit of hack to you stuff, I hope you consider it :-) My test.html then contains this script:

mocha.setup('bdd');
chai.should();

describeCoverageTest();
((window.puppeteerStartCoverage && puppeteerStartCoverage()) || Promise.resolve())
.then(listTests)
.then(importTests)
.then(() => {
	mocha.checkLeaks();
	mocha.run();
});

function listTests() {return fetch('/test').then(res => res.text());}

function importTests(text) {return Promise.all(
	text.match(/(\w+)\.mjs/g).map(file => import(`./${file}`))
);}

function describeCoverageTest() {
	if(!window.puppeteerStartCoverage) return;
	after(done => {
		puppeteerStopCoverage()
		.then(coverage => {
			const sq = /shadowQuery\.mjs/;
			coverage = coverage.js.filter(el => sq.test(el.url))[0];
			let totalBytes = coverage.text.length, usedBytes = 0;
			for(let range of coverage.ranges) usedBytes += range.end - range.start - 1;
			coverage = usedBytes / totalBytes;
			try{coverage.should.be.above(0.99);}
			catch(e) {done(); throw new Error(`Insufficient coverage: ${coverage}`);}
			done();
		});
	});
}

@schrotie
Copy link
Author

Or maybe just hand the whole puppeteer.page down to the test script?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant