Skip to content

Commit

Permalink
update v8-and-istanbul test
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Dec 21, 2023
1 parent 60cc9b1 commit 8b44888
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 77 deletions.
196 changes: 196 additions & 0 deletions test/test-v8-and-istanbul.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
const { chromium } = require('playwright');
const EC = require('eight-colors');
const CG = require('console-grid');

const CoverageReport = require('../');

// v8 and istanbul reports
const multipleReportsOptions = {
// logging: 'debug',

// https://github.com/istanbuljs/istanbuljs/tree/master/packages/istanbul-reports/lib
reports: [
['v8'],
['v8-json', {
// outputFile: 'json/v8-report.json'
}],
['html', {
subdir: 'istanbul'
}],
['json', {
file: 'my-json-file.json'
}],
'lcovonly'
],

name: 'My V8 and Istanbul Coverage Report',
// v8 sub dir
outputFile: 'v8/index.html',
assetsPath: '../../assets',

// reportPath: 'lcov.info',
reportPath: () => {
return 'my-json-file.json';
},

onEnd: (coverageResults) => {
const summary = coverageResults.summary;
console.log(summary);
CG({
columns: [{
id: 'name'
}, {
id: 'value',
formatter: (v, row, column) => {
if (row.name === 'pct') {
// in red color if coverage less than 90%
if (v < 90) {
v = EC.red(v);
}
return v;
}
return v;
}
}],
rows: Object.keys(summary).map((id) => {
const indicator = summary[id];
return {
name: id,
value: '',
subs: Object.keys(indicator).map((k) => {
return {
name: k,
value: indicator[k]
};
})
};
})
});
},

outputDir: './docs/v8-and-istanbul'
};

const test1 = async (serverUrl) => {

console.log('start v8-and-istanbul test1 ...');
const browser = await chromium.launch({
// headless: false
});
const page = await browser.newPage();

await Promise.all([
page.coverage.startJSCoverage({
resetOnNavigation: false
}),
page.coverage.startCSSCoverage({
resetOnNavigation: false
})
]);

const url = `${serverUrl}/v8/`;

console.log(`goto ${url}`);

await page.goto(url);

await new Promise((resolve) => {
setTimeout(resolve, 500);
});

await page.evaluate(() => {
const { foo } = window['coverage-v8'];
foo();
});

await page.evaluate(() => {
const { bar } = window['coverage-v8'];
bar();
});

const [jsCoverage, cssCoverage] = await Promise.all([
page.coverage.stopJSCoverage(),
page.coverage.stopCSSCoverage()
]);

const coverageList = [... jsCoverage, ... cssCoverage];

// to istanbul
const report = await new CoverageReport(multipleReportsOptions).add(coverageList);

console.log('v8-and-istanbul coverage1 added', report.type);

await browser.close();
};


const test2 = async (serverUrl) => {

console.log('start v8-and-istanbul test2 ...');
const browser = await chromium.launch({
// headless: false
});
const page = await browser.newPage();

await Promise.all([
page.coverage.startJSCoverage({
resetOnNavigation: false
}),
page.coverage.startCSSCoverage({
resetOnNavigation: false
})
]);

const url = `${serverUrl}/v8/`;

console.log(`goto ${url}`);

await page.goto(url);

await new Promise((resolve) => {
setTimeout(resolve, 500);
});

await page.evaluate(() => {
const { start } = window['coverage-v8'];
start();
});

const [jsCoverage, cssCoverage] = await Promise.all([
page.coverage.stopJSCoverage(),
page.coverage.stopCSSCoverage()
]);

const coverageList = [... jsCoverage, ... cssCoverage];

// to istanbul
const report = await new CoverageReport(multipleReportsOptions).add(coverageList);

console.log('v8-and-istanbul coverage2 added', report.type);

await browser.close();
};


const generate = async () => {

console.log('generate v8-and-istanbul coverage reports ...');
// to istanbul
const coverageResults2 = await new CoverageReport(multipleReportsOptions).generate();
console.log('reportPath', EC.magenta(coverageResults2.reportPath));
};


module.exports = async (serverUrl) => {
// clean cache first if debug
if (multipleReportsOptions.logging === 'debug') {
await new CoverageReport(multipleReportsOptions).cleanCache();
}

await Promise.all([
test1(serverUrl),
test2(serverUrl)
]);

await generate();
};
77 changes: 0 additions & 77 deletions test/test-v8.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { chromium } = require('playwright');
const EC = require('eight-colors');
const CG = require('console-grid');

const CoverageReport = require('../');

Expand All @@ -19,73 +18,6 @@ const coverageOptions = {
outputDir: './docs/v8'
};

// v8 and istanbul reports
const multipleReportsOptions = {
// logging: 'debug',

// https://github.com/istanbuljs/istanbuljs/tree/master/packages/istanbul-reports/lib
reports: [
['v8'],
['v8-json', {
// outputFile: 'json/v8-report.json'
}],
['html', {
subdir: 'istanbul'
}],
['json', {
file: 'my-json-file.json'
}],
'lcovonly'
],

name: 'My V8 sub dir Coverage Report',
// v8 sub dir
outputFile: 'v8/index.html',
assetsPath: '../../assets',

// reportPath: 'lcov.info',
reportPath: () => {
return 'my-json-file.json';
},

onEnd: (coverageResults) => {
const summary = coverageResults.summary;
console.log(summary);
CG({
columns: [{
id: 'name'
}, {
id: 'value',
formatter: (v, row, column) => {
if (row.name === 'pct') {
// in red color if coverage less than 90%
if (v < 90) {
v = EC.red(v);
}
return v;
}
return v;
}
}],
rows: Object.keys(summary).map((id) => {
const indicator = summary[id];
return {
name: id,
value: '',
subs: Object.keys(indicator).map((k) => {
return {
name: k,
value: indicator[k]
};
})
};
})
});
},

outputDir: './docs/v8-and-istanbul'
};

const test1 = async (serverUrl) => {

console.log('start v8 test1 ...');
Expand Down Expand Up @@ -133,9 +65,6 @@ const test1 = async (serverUrl) => {
// v8
const report = await new CoverageReport(coverageOptions).add(coverageList);

// to istanbul
await new CoverageReport(multipleReportsOptions).add(coverageList);

console.log('v8 coverage1 added', report.type);

await browser.close();
Expand Down Expand Up @@ -183,9 +112,6 @@ const test2 = async (serverUrl) => {

const report = await new CoverageReport(coverageOptions).add(coverageList);

// to istanbul
await new CoverageReport(multipleReportsOptions).add(coverageList);

console.log('v8 coverage2 added', report.type);

await browser.close();
Expand All @@ -200,9 +126,6 @@ const generate = async () => {
console.log('reportPath', EC.magenta(coverageResults1.reportPath));
console.log('v8 coverage generated', Object.keys(coverageResults1), coverageResults1.summary);

// to istanbul
const coverageResults2 = await new CoverageReport(multipleReportsOptions).generate();
console.log('reportPath', EC.magenta(coverageResults2.reportPath));
};


Expand Down
5 changes: 5 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const KSR = require('koa-static-resolver');
const Koa = require('koa');

const testV8 = require('./test-v8.js');
const testV8AndIstanbul = require('./test-v8-and-istanbul.js');
const testV8EsBuild = require('./test-v8-esbuild.js');
const testV8Rollup = require('./test-v8-rollup.js');
const testV8Minify = require('./test-v8-minify.js');
Expand Down Expand Up @@ -97,9 +98,13 @@ const test = async () => {

await Promise.all([
testIstanbul(serverUrl),
testV8AndIstanbul(serverUrl),

testV8Minify(serverUrl),

testV8EsBuild(serverUrl),
testV8Rollup(serverUrl),

testV8(serverUrl)
]);

Expand Down

0 comments on commit 8b44888

Please sign in to comment.