|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +/* eslint-disable no-console */ |
| 18 | + |
| 19 | +class JestSlowTestReporter { |
| 20 | + constructor(globalConfig, options) { |
| 21 | + this._globalConfig = globalConfig; |
| 22 | + this._options = options; |
| 23 | + this._slowTests = []; |
| 24 | + this._slowTestSuites = []; |
| 25 | + } |
| 26 | + |
| 27 | + onRunComplete() { |
| 28 | + const displayResult = (result, isTestSuite) => { |
| 29 | + if (!isTestSuite) console.log(); |
| 30 | + |
| 31 | + result.sort((a, b) => b.duration - a.duration); |
| 32 | + const rootPathRegex = new RegExp(`^${process.cwd()}`); |
| 33 | + const slowestTests = result.slice(0, this._options.numTests || 10); |
| 34 | + const slowTestTime = this._slowTestTime(slowestTests); |
| 35 | + const allTestTime = this._allTestTime(result); |
| 36 | + const percentTime = (slowTestTime / allTestTime) * 100; |
| 37 | + |
| 38 | + if (isTestSuite) { |
| 39 | + console.log( |
| 40 | + `Top ${slowestTests.length} slowest test suites (${slowTestTime / 1000} seconds,` + |
| 41 | + ` ${percentTime.toFixed(1)}% of total time):`, |
| 42 | + ); |
| 43 | + } else { |
| 44 | + console.log( |
| 45 | + `Top ${slowestTests.length} slowest tests (${slowTestTime / 1000} seconds,` + |
| 46 | + ` ${percentTime.toFixed(1)}% of total time):`, |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + for (let i = 0; i < slowestTests.length; i++) { |
| 51 | + const duration = slowestTests[i].duration; |
| 52 | + const filePath = slowestTests[i].filePath.replace(rootPathRegex, '.'); |
| 53 | + |
| 54 | + if (isTestSuite) { |
| 55 | + console.log(` ${duration / 1000} seconds ${filePath}`); |
| 56 | + } else { |
| 57 | + const fullName = slowestTests[i].fullName; |
| 58 | + console.log(` ${fullName}`); |
| 59 | + console.log(` ${duration / 1000} seconds ${filePath}`); |
| 60 | + } |
| 61 | + } |
| 62 | + console.log(); |
| 63 | + }; |
| 64 | + |
| 65 | + displayResult(this._slowTests); |
| 66 | + displayResult(this._slowTestSuites, true); |
| 67 | + } |
| 68 | + |
| 69 | + onTestResult(test, testResult) { |
| 70 | + this._slowTestSuites.push({ |
| 71 | + duration: testResult.perfStats.runtime, |
| 72 | + filePath: testResult.testFilePath, |
| 73 | + }); |
| 74 | + for (let i = 0; i < testResult.testResults.length; i++) { |
| 75 | + this._slowTests.push({ |
| 76 | + duration: testResult.testResults[i].duration, |
| 77 | + fullName: testResult.testResults[i].fullName, |
| 78 | + filePath: testResult.testFilePath, |
| 79 | + }); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + _slowTestTime(slowestTests) { |
| 84 | + let slowTestTime = 0; |
| 85 | + for (let i = 0; i < slowestTests.length; i++) { |
| 86 | + slowTestTime += slowestTests[i].duration; |
| 87 | + } |
| 88 | + return slowTestTime; |
| 89 | + } |
| 90 | + |
| 91 | + _allTestTime(result) { |
| 92 | + let allTestTime = 0; |
| 93 | + for (let i = 0; i < result.length; i++) { |
| 94 | + allTestTime += result[i].duration; |
| 95 | + } |
| 96 | + return allTestTime; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +module.exports = JestSlowTestReporter; |
0 commit comments