gpt4 book ai didi

node.js - 获取按持续时间排序的 Jest 测试

转载 作者:行者123 更新时间:2023-12-05 09:30:57 26 4
gpt4 key购买 nike

我正在努力提高我们的 Jest 测试的性能,其中包含 4,000 多个测试。要查看单个测试持续时间,我使用 --verbose 标志。

是否有一种简单的方法来发现运行时间最长的测试,还是我必须滚动浏览整个输出?

最佳答案

你可以编写一个测试报告器并将其添加到reporters 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 参数表示找到前 3 个最慢的测试用例。

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);
});
});

测试结果:

 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

关于node.js - 获取按持续时间排序的 Jest 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69198091/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com