Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork753
Converting Playwright to Istanbul Coverage
cenfun edited this pageFeb 25, 2024 ·3 revisions
How to convertplaywright coverage format toistanbul coverage
To convert coverage generated fromplaywright toistanbul coverage, you first need to install
Once installed, convert the coverage to a format whichistanbul can recognize, by writing a script as shown below.
importglobfrom'glob'importv8toIstanbulfrom'v8-to-istanbul'letcoverageimportfsfrom'fs'constcoverageFolder=`${process.cwd()}/coverage`asyncfunctionisExists(path){try{awaitfs.access(path,null)returntrue}catch{returnfalse}}glob.sync(process.cwd()+'/output/coverage/**/').forEach(item=>{constdirectory=fs.opendirSync(item)letfilewhile((file=directory.readSync())!==null){if(file&&file.name.includes('.coverage.json')===true){constfileName=file.nameif(fileName){coverage=require(`${process.cwd()}/output/coverage/${fileName}`)}}}directory.closeSync()})void(async()=>{for(constentryofcoverage){// Used to get file nameconstfile=entry.url.match(/(?:http(s)*:\/\/.*\/)(?<file>.*)/)constconverter=v8toIstanbul(file.groups.file,0,{source:entry.source,})awaitconverter.load()converter.applyCoverage(entry.functions)// Store converted coverage file which can later be used to generate reportconstexist=awaitisExists(coverageFolder)if(!exist){fs.mkdirSync(coverageFolder,{recursive:true})}awaitfs.writeFileSync(`${coverageFolder}/final.json`,JSON.stringify(converter.toIstanbul(),null,2))}})()
Or simply use the plugincodeceptjs-monocart-coverage
- Install
npm i codeceptjs-monocart-coverage
- Usage
// codecept.conf.js{plugins:{monocart:{require:'codeceptjs-monocart-coverage',enabled:true,coverageOptions:{// more options: https://github.com/cenfun/monocart-coverage-reportsname:'My CodeceptJS Coverage Report',outputDir:'coverage-reports'}}},helpers:{// Coverage is only supported in Playwright or PuppeteerPlaywright:{browser:'chromium',url:'http://localhost',show:false}// Puppeteer: {// url: 'http://localhost',// show: false// }}}