I am trying to improve the performance of our jest tests, which contain over 4,000 tests. To see the individual test duration I am using the --verbose
flag.
Is there an easy way to discover the longest running tests or do I have to just scroll through the entire output?
CodePudding user response:
You can write a test reporter and add it to reporters configuration of jest.
./reporters/slow-test.js
:
class JestSlowTestReporter {
constructor(globalConfig, options) {
this._globalConfig = globalConfig;
this._options = options;
this._slowTests = [];
}
onRunComplete() {
console.log();
this._slowTests.sort((a, b) => b.duration - a.duration);
var rootPathRegex = new RegExp(`^${process.cwd()}`);
var slowestTests = this._slowTests.slice(0, this._options.numTests || 10);
var slowTestTime = this._slowTestTime(slowestTests);
var allTestTime = this._allTestTime();
var percentTime = (slowTestTime / allTestTime) * 100;
console.log(
`Top ${slowestTests.length} slowest examples (${slowTestTime / 1000} seconds,`
` ${percentTime.toFixed(1)}% of total time):`
);
for (var i = 0; i < slowestTests.length; i ) {
var duration = slowestTests[i].duration;
var fullName = slowestTests[i].fullName;
var filePath = slowestTests[i].filePath.replace(rootPathRegex, '.');
console.log(` ${fullName}`);
console.log(` ${duration / 1000} seconds ${filePath}`);
}
console.log();
}
onTestResult(test, testResult) {
for (var i = 0; i < testResult.testResults.length; i ) {
this._slowTests.push({
duration: testResult.testResults[i].duration,
fullName: testResult.testResults[i].fullName,
filePath: testResult.testFilePath,
});
}
}
_slowTestTime(slowestTests) {
var slowTestTime = 0;
for (var i = 0; i < slowestTests.length; i ) {
slowTestTime = slowestTests[i].duration;
}
return slowTestTime;
}
_allTestTime() {
var allTestTime = 0;
for (var i = 0; i < this._slowTests.length; i ) {
allTestTime = this._slowTests[i].duration;
}
return allTestTime;
}
}
module.exports = JestSlowTestReporter;
jest.config.js
:
module.exports = {
preset: 'ts-jest/presets/js-with-ts',
reporters: ['default', ['<rootDir>/reporters/slow-test', { numTests: 3 }]],
};
numTests: 3
argument means to find the top 3 slowest test cases.
index.test.js
:
describe('find top slowest test', () => {
test('should 1', (done) => {
setTimeout(() => {
expect(1 1).toBe(2);
done();
}, 1000);
});
test('should 2', (done) => {
setTimeout(() => {
expect(1 1).toBe(2);
done();
}, 1200);
});
test('should 3', (done) => {
setTimeout(() => {
expect(1 1).toBe(2);
done();
}, 100);
});
});
test result:
PASS examples/find-top-slowest-test/index.test.js
find top slowest test
✓ should 1 (1007 ms)
✓ should 2 (1201 ms)
✓ should 3 (103 ms)
Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: 3.588 s, estimated 4 s
Ran all test suites matching /\/Users\/dulin\/workspace\/github.com\/mrdulin\/jest-v26-codelab\/examples\/find-top-slowest-test\/index.test.js/i.
Top 3 slowest examples (2.311 seconds, 100.0% of total time):
find top slowest test should 2
1.201 seconds ./examples/find-top-slowest-test/index.test.js
find top slowest test should 1
1.007 seconds ./examples/find-top-slowest-test/index.test.js
find top slowest test should 3
0.103 seconds ./examples/find-top-slowest-test/index.test.js