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

[WIP] Add hooks necessary for Chrome coverage #1750

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

Conversation

wagenet
Copy link
Contributor

@wagenet wagenet commented Nov 20, 2023

This adds hooks necessary for gathering coverage data directly from Chrome. It's a bit rough right now and untested but it does work. I'd love to see if we can actually get this to full first-class support.

To make use of this, I have this additional code in my own app:

/* eslint-disable no-undef */
const chromeRemoteInterface = require('chrome-remote-interface');
const fs = require('node:fs');

const { mergeProcessCovs } = require('@bcoe/v8-coverage');

function buildContext({ coverageDir }) {
	const remoteInterfaces = {};

	if (!fs.existsSync(coverageDir)) {
		fs.mkdirSync(coverageDir);
	}

	return {
		remoteInterfaces,

		config: {
			// this here will be the browser runner
			async browser_on_dev_tools_ready() {
				const match = this.devToolsUrl.match(/ws:\/\/(.+):(\d+)\//);
				if (match) {
					const [_, host, port] = match;
					const remoteInterface = await chromeRemoteInterface({ host, port });
					remoteInterfaces[this.launcherId] = remoteInterface;

					const { Profiler } = remoteInterface;
					await Profiler.enable();
					await Profiler.startPreciseCoverage({ callCount: true, detailed: true });
				}
			},

			// this here will be the browser runner
			async browser_on_before_finish() {
				const remoteInterface = remoteInterfaces[this.launcherId];
				if (remoteInterface) {
					// Clear it out so we don't do it twice
					remoteInterfaces[this.launcherId] = null;

					const { Profiler } = remoteInterface;

					const results = await Profiler.takePreciseCoverage();

					const coveragePath = `${coverageDir}/coverage-${this.launcherId}.json`;
					fs.writeFileSync(coveragePath, JSON.stringify(results, null, 2));

					remoteInterface.close();
				}
			},

			// this here will be the internal reporter
			after_reporter_finish() {
				let coverage;

				const pastCoveragePath = `${coverageDir}/past-coverage.json`;
				if (fs.existsSync(pastCoveragePath)) {
					const coverageData = fs.readFileSync(pastCoveragePath);
					coverage = JSON.parse(coverageData);
				}

				for (const id of Object.keys(remoteInterfaces)) {
					const coverageData = fs.readFileSync(`${coverageDir}/coverage-${id}.json`);
					const coverageJson = JSON.parse(coverageData);
					if (coverage) {
						coverage = mergeProcessCovs([coverage, coverageJson]);
					} else {
						coverage = coverageJson;
					}
				}

				const jsonString = JSON.stringify(coverage, null, 2);

				console.log(`Merged ${Object.keys(remoteInterfaces).length} files.`);
				console.log(`Total size: ${jsonString.length}`);
				console.log(`Writing coverage to ${coverageDir}/coverage.json.`);
				fs.writeFileSync(`${coverageDir}/coverage.json`, jsonString, 'utf8');
			},
		},
	};
}

module.exports = { buildContext };

Then in my testem.js:

const { buildContext } = require('testem-helpers');

const coverageContext = buildContext({ coverageDir: path.join(__dirname, 'coverage') });

module.exports = {
	// other config
	..coverageContext.config,
};

@NullVoxPopuli
Copy link

would love to see this landed <3 🎉
looks interesting 👀

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.

None yet

2 participants