Skip to content

Commit

Permalink
chore: 覆盖率收集性能优化
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuineng committed Jan 29, 2024
1 parent a60e36c commit e579a8d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
53 changes: 37 additions & 16 deletions lib/helper/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,50 @@ module.exports = wd => {
* @type utility
* @return {Promise.<boolean>}
*/
wd.addPromiseChainMethod('coverage', function() {
wd.addPromiseChainMethod('coverage', function(opts = {}) {
let {
coverageIgnore = program.coverageIgnore,
keys,
} = opts;
const tempDir = path.join(cwd, 'coverage', '.temp');
_.mkdir(tempDir);
return this.execute('return window.__coverage__')
.then(__coverage__ => {
if (!__coverage__) {
return this.execute('return location.href').then(url => {
logger.info(`>> coverage failed: ${url}`);
return this.execute('return { allKeys: Object.keys(window.__coverage__) }')
.then(async res => {
const { allKeys } = res;
if (!keys) {
keys = allKeys;
}
if (coverageIgnore) {
logger.info('handle coverageIgnore');
const ignoreReg = new RegExp(coverageIgnore, 'i');
keys = keys.filter(k => {
return !ignoreReg.test(k);
});
}
const file = path.join(tempDir, `${+new Date()}_coverage.json`);
if (program.coverageIgnore) {
const ignoreReg = new RegExp(program.coverageIgnore, 'i');
for (const k in __coverage__) {
if (ignoreReg.test(k)) {
delete __coverage__[k];
if (!keys.length) {
logger.info('coverage keys is empty');
return;
}
const covFile = path.join(tempDir, `${+new Date()}_coverage.json`);
const writer = fs.createWriteStream(covFile);
writer.write('{');
for (let i = 0; i < keys.length; i++) {
const coverage = await this.execute(`return window.__coverage__['${keys[i]}']`);
if (coverage) {
const coverageJSON = JSON.stringify(coverage);
writer.write(`"${keys[i]}":${coverageJSON}`);
if (i < keys.length - 1) {
writer.write(',');
}
}
}
fs.writeFileSync(file, JSON.stringify(__coverage__, null, 2));
})
.catch(e => {
console.log(e);
writer.write('}');
await new Promise(resolve => {
writer.end(() => {
writer.close();
resolve();
});
});
});
});
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "macaca-wd",
"version": "4.3.4",
"version": "4.3.5",
"description": "Macaca webdirver API for Node.js",
"keywords": [
"macaca",
Expand Down

0 comments on commit e579a8d

Please sign in to comment.