Skip to content

Instantly share code, notes, and snippets.

@jkrems
Created October 7, 2017 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkrems/761d459bd913f2db16a0299659b16a87 to your computer and use it in GitHub Desktop.
Save jkrems/761d459bd913f2db16a0299659b16a87 to your computer and use it in GitHub Desktop.
// V8 6.2 required
'use strict';
const fs = require('fs');
const { Session } = require('inspector');
const { promisify } = require('util');
const session = new Session();
if (session != null) {
session.connect();
}
const run = promisify(session.post.bind(session));
const NATIVES = process.binding('natives');
function formatCoverage({ url, scriptId, functions }) {
const isInternal = /^[\w-]+(?:\/[\w-]+)*\.js$/.test(url);
if (url.includes('internal/fs')) {
const source = isInternal ?
NATIVES[url.replace(/\.js$/, '')] : fs.readFileSync(url, 'utf8');
const markers = [
{
startOffset: 0,
endOffset: source.length,
count: 0,
}
];
functions.forEach(f => {
f.ranges.forEach(range => {
if (range.count > 0) {
console.log('covered:\n', source.slice(range.startOffset, range.endOffset));
}
});
});
}
}
run('Profiler.enable')
.then(() => run('Profiler.startPreciseCoverage', { detailed: true }))
.then(() => {
require('./test/parallel/test-fs-read-file-sync.js');
})
.then(() => run('Profiler.takePreciseCoverage'))
.then((data) => data.result.forEach(formatCoverage))
.then(() => run('Profiler.stopPreciseCoverage'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment