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: script to allow simple collection of coverage from all projects #11798

Merged
merged 6 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"files.exclude": {
"**/.git": true,
"**/node_modules": true,
"**/build": true,
"**/coverage": true
"**/build": true
},
"editor.formatOnSave": true,
"eslint.validate": ["javascript", "javascriptreact", "typescript", "typescriptreact"],
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.0",
"description": "amplify-cli",
"scripts": {
"coverage:collect": "ts-node ./scripts/coverage.ts",
"addwords": "node ./scripts/add-to-dict.js",
"test-changed": "nx affected --target=test",
"test": "nx run-many --target=test --all",
Expand Down Expand Up @@ -122,6 +123,9 @@
"execa": "^5.1.1",
"glob": "^7.2.0",
"husky": "^3.1.0",
"istanbul-lib-coverage": "^3.2.0",
"istanbul-lib-report": "^3.0.0",
"istanbul-reports": "^3.1.5",
"jest": "^27.0.0",
"jest-circus": "^27.0.0",
"jest-junit": "^12.0.0",
Expand All @@ -146,4 +150,4 @@
"**/nth-check": "^2.0.1",
"**/undici": "^5.8.0"
}
}
}
65 changes: 65 additions & 0 deletions scripts/coverage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as fs from 'fs';
import * as path from 'path';
import * as istanbul from 'istanbul-lib-coverage';
import * as libReport from 'istanbul-lib-report';
import * as reports from 'istanbul-reports';

sdstolworthy marked this conversation as resolved.
Show resolved Hide resolved
const deleteCoverageDir = () => {
fs.rmdirSync(path.join(process.cwd(), './coverage'), { recursive: true });
};

const makeCoverageDir = () => {
fs.mkdirSync(path.join(process.cwd(), './coverage/workspaces'), { recursive: true });
};

const listPackages = () => {
const packages = fs.readdirSync(path.join(process.cwd(), './packages'));
return packages;
};

const copyPackageCoverage = (pkg: string) => {
const coverageFile = path.join(process.cwd(), `./packages/${pkg}/coverage/coverage-final.json`);
if (fs.existsSync(coverageFile)) {
const target = path.join(process.cwd(), `./coverage/workspaces/${pkg}-coverage-final.json`);
fs.copyFileSync(coverageFile, target);
}
};

const getCoverageFiles = () => {
const files = fs.readdirSync(path.join(process.cwd(), './coverage/workspaces'));
return files.map(f => path.join(process.cwd(), `./coverage/workspaces/${f}`));
};

const mergeCoverageMap = (files: string[]): istanbul.CoverageMap => {
var map = istanbul.createCoverageMap({});
files.forEach(file => {
const json = fs.readFileSync(file).toString();
map.merge(JSON.parse(json));
});
return map;
};

const writeCoverageJson = (map: istanbul.CoverageMap) => {
const json = JSON.stringify(map);
fs.writeFileSync(path.join(process.cwd(), './coverage/monorepo-coverage.json'), json);
};

const generateReport = (coverage: istanbul.CoverageMap) => {
const context = libReport.createContext({
dir: path.join(process.cwd(), './coverage/html'),
defaultSummarizer: 'pkg',
coverageMap: coverage,
});
reports.create('html-spa', {}).execute(context);
};

const main = () => {
deleteCoverageDir();
makeCoverageDir();
listPackages().forEach(copyPackageCoverage);
const coverage = mergeCoverageMap(getCoverageFiles());
generateReport(coverage);
writeCoverageJson(coverage);
};

main();
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17072,7 +17072,7 @@ istanbul-lib-instrument@^5.1.0:

istanbul-lib-report@^3.0.0:
version "3.0.0"
resolved "https://registry.npmjs.org/istanbul-lib-report/-/istanbul-lib-report-3.0.0.tgz#7518fe52ea44de372f460a76b5ecda9ffb73d8a6"
resolved "https://registry.yarnpkg.com/istanbul-lib-report/-/istanbul-lib-report-3.0.0.tgz#7518fe52ea44de372f460a76b5ecda9ffb73d8a6"
integrity sha512-wcdi+uAKzfiGT2abPpKZ0hSU1rGQjUQnLvtY5MpQ7QCTahD3VODhcu4wcfY1YtkGaDD5yuydOLINXsfbus9ROw==
dependencies:
istanbul-lib-coverage "^3.0.0"
Expand All @@ -17096,7 +17096,7 @@ istanbul-reports@^3.0.2:
html-escaper "^2.0.0"
istanbul-lib-report "^3.0.0"

istanbul-reports@^3.1.3:
istanbul-reports@^3.1.3, istanbul-reports@^3.1.5:
version "3.1.5"
resolved "https://registry.yarnpkg.com/istanbul-reports/-/istanbul-reports-3.1.5.tgz#cc9a6ab25cb25659810e4785ed9d9fb742578bae"
integrity sha512-nUsEMa9pBt/NOHqbcbeJEgqIlY/K7rVWUX6Lql2orY5e9roQOthbR3vtY4zzf2orPELg80fnxxk9zUyPlgwD1w==
Expand Down