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

chore: 覆盖率收集性能优化 #122

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.length && allKeys) {
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
Loading