forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.config.codeowner.js
More file actions
144 lines (123 loc) · 4.51 KB
/
jest.config.codeowner.js
File metadata and controls
144 lines (123 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const fs = require('fs');
const open = require('open').default;
const path = require('path');
const baseConfig = require('./jest.config.js');
const CODEOWNERS_MANIFEST_FILENAMES_BY_TEAM_PATH = 'codeowners-manifest/filenames-by-team.json';
const codeownerName = process.env.CODEOWNER_NAME;
if (!codeownerName) {
console.error('ERROR: CODEOWNER_NAME environment variable is required');
process.exit(1);
}
const outputDir = `./coverage/by-team/${createOwnerDirectory(codeownerName)}`;
const codeownersFilePath = path.join(__dirname, CODEOWNERS_MANIFEST_FILENAMES_BY_TEAM_PATH);
if (!fs.existsSync(codeownersFilePath)) {
console.error(`Codeowners file not found at ${codeownersFilePath} ...`);
console.error('Please run: yarn codeowners-manifest first to generate the mapping file');
process.exit(1);
}
const codeownersData = JSON.parse(fs.readFileSync(codeownersFilePath, 'utf8'));
const teamFiles = codeownersData[codeownerName] || [];
if (teamFiles.length === 0) {
console.error(`ERROR: No files found for team "${codeownerName}"`);
console.error('Available teams:', Object.keys(codeownersData).join(', '));
process.exit(1);
}
const sourceFiles = teamFiles.filter((file) => {
const ext = path.extname(file);
return (
['.ts', '.tsx', '.js', '.jsx'].includes(ext) &&
// exclude all tests and mocks
!path.matchesGlob(file, '**/test/**/*') &&
!file.includes('.test.') &&
!file.includes('.spec.') &&
!path.matchesGlob(file, '**/__mocks__/**/*') &&
// and storybook stories
!file.includes('.story.') &&
// and generated files
!file.includes('.gen.ts') &&
// and type definitions
!file.includes('.d.ts') &&
!file.endsWith('/types.ts') &&
// and anything in graveyard
!path.matchesGlob(file, '**/graveyard/**/*')
);
});
const testFiles = teamFiles.filter((file) => {
const ext = path.extname(file);
return ['.ts', '.tsx', '.js', '.jsx'].includes(ext) && (file.includes('.test.') || file.includes('.spec.'));
});
if (testFiles.length === 0) {
console.log(`No test files found for team ${codeownerName}`);
process.exit(0);
}
console.log(
`🧪 Collecting coverage for ${sourceFiles.length} testable files and running ${testFiles.length} test files of ${teamFiles.length} files owned by ${codeownerName}.`
);
module.exports = {
...baseConfig,
collectCoverage: true,
collectCoverageFrom: sourceFiles.map((file) => `<rootDir>/${file}`),
coverageReporters: ['none'],
coverageDirectory: '/tmp/jest-coverage-ignore',
coverageProvider: 'v8',
reporters: [
'default',
[
'jest-monocart-coverage',
{
name: `Coverage Report - ${codeownerName} owned files`,
outputDir: outputDir,
reports: ['console-summary', 'v8', 'json', 'lcov'],
sourceFilter: (coveredFile) => sourceFiles.includes(coveredFile),
all: {
dir: ['./packages', './public'],
filter: (filePath) => {
const relativePath = filePath.replace(process.cwd() + '/', '');
return sourceFiles.includes(relativePath);
},
},
cleanCache: true,
onEnd: (coverageResults) => {
const reportURL = `file://${path.resolve(outputDir)}/index.html`;
console.log(`📄 Coverage report saved to ${reportURL}`);
if (process.env.SHOULD_OPEN_COVERAGE_REPORT === 'true') {
openCoverageReport(reportURL);
}
// TODO: Emit coverage metrics https://github.com/grafana/grafana/issues/111208
},
},
],
],
testRegex: undefined,
testMatch: testFiles.map((file) => `<rootDir>/${file}`),
};
/**
* Create a filesystem-safe directory structure for different owner types
* @param {string} owner - CODEOWNERS owner (username, team, or email)
* @returns {string} Directory path relative to coverage/by-team/
*/
function createOwnerDirectory(owner) {
if (owner.includes('@') && owner.includes('/')) {
// Example: @grafana/dataviz-squad
const [org, team] = owner.substring(1).split('/');
return `teams/${org}/${team}`;
} else if (owner.startsWith('@')) {
// Example: @jesdavpet
return `users/${owner.substring(1)}`;
} else {
// Example: user@domain.tld
const [user, domain] = owner.split('@');
return `emails/${user}-at-${domain}`;
}
}
/**
* Open the given file URL in the default browser safely, without shell injection risk.
* @param {string} reportURL
*/
async function openCoverageReport(reportURL) {
try {
await open(reportURL);
} catch (err) {
console.error(`Failed to open coverage report: ${err}`);
}
}