ci: Find duplicate and slow tests (#9188)

This commit is contained in:
Diamond Lewis
2024-07-16 03:34:10 -05:00
committed by GitHub
parent 872b9eb47b
commit e355f36ed4
13 changed files with 43 additions and 170 deletions

View File

@@ -1,15 +1,45 @@
// Sets a global variable to the current test spec
// ex: global.currentSpec.description
const { performance } = require('perf_hooks');
global.currentSpec = null;
const timerMap = {};
const duplicates = [];
/** The minimum execution time in seconds for a test to be considered slow. */
const slowTestLimit = 2;
class CurrentSpecReporter {
specStarted(spec) {
if (timerMap[spec.fullName]) {
console.log('Duplicate spec: ' + spec.fullName);
duplicates.push(spec.fullName);
}
timerMap[spec.fullName] = performance.now();
global.currentSpec = spec;
}
specDone() {
specDone(result) {
if (result.status === 'excluded') {
delete timerMap[result.fullName];
return;
}
timerMap[result.fullName] = (performance.now() - timerMap[result.fullName]) / 1000;
global.currentSpec = null;
}
}
global.displaySlowTests = function() {
const times = Object.values(timerMap).sort((a,b) => b - a);
if (times.length > 0) {
console.log(`Slow tests with execution time >=${slowTestLimit}s:`);
}
times.forEach((time) => {
if (time >= slowTestLimit) {
console.warn(`${time.toFixed(1)}s:`, Object.keys(timerMap).find(key => timerMap[key] === time));
}
});
console.log('\n');
duplicates.forEach((spec) => {
console.warn('Duplicate spec: ' + spec);
});
};
module.exports = CurrentSpecReporter;