Mat*_*att 4 unit-testing node.js jestjs
我正在努力提高我们的笑话测试的性能,其中包含 4,000 多个测试。为了查看单独的测试持续时间,我使用了该--verbose标志。
有没有一种简单的方法可以发现运行时间最长的测试,或者我是否必须滚动浏览整个输出?
sli*_*wp2 13
您可以编写一个测试报告器并将其添加到jest 的报告器配置中。
\n./reporters/slow-test.js:
class JestSlowTestReporter {\n constructor(globalConfig, options) {\n this._globalConfig = globalConfig;\n this._options = options;\n this._slowTests = [];\n }\n\n onRunComplete() {\n console.log();\n this._slowTests.sort((a, b) => b.duration - a.duration);\n var rootPathRegex = new RegExp(`^${process.cwd()}`);\n var slowestTests = this._slowTests.slice(0, this._options.numTests || 10);\n var slowTestTime = this._slowTestTime(slowestTests);\n var allTestTime = this._allTestTime();\n var percentTime = (slowTestTime / allTestTime) * 100;\n\n console.log(\n `Top ${slowestTests.length} slowest examples (${slowTestTime / 1000} seconds,` +\n ` ${percentTime.toFixed(1)}% of total time):`\n );\n\n for (var i = 0; i < slowestTests.length; i++) {\n var duration = slowestTests[i].duration;\n var fullName = slowestTests[i].fullName;\n var filePath = slowestTests[i].filePath.replace(rootPathRegex, \'.\');\n\n console.log(` ${fullName}`);\n console.log(` ${duration / 1000} seconds ${filePath}`);\n }\n console.log();\n }\n\n onTestResult(test, testResult) {\n for (var i = 0; i < testResult.testResults.length; i++) {\n this._slowTests.push({\n duration: testResult.testResults[i].duration,\n fullName: testResult.testResults[i].fullName,\n filePath: testResult.testFilePath,\n });\n }\n }\n\n _slowTestTime(slowestTests) {\n var slowTestTime = 0;\n for (var i = 0; i < slowestTests.length; i++) {\n slowTestTime += slowestTests[i].duration;\n }\n return slowTestTime;\n }\n\n _allTestTime() {\n var allTestTime = 0;\n for (var i = 0; i < this._slowTests.length; i++) {\n allTestTime += this._slowTests[i].duration;\n }\n return allTestTime;\n }\n}\n\nmodule.exports = JestSlowTestReporter;\nRun Code Online (Sandbox Code Playgroud)\njest.config.js:
module.exports = {\n preset: \'ts-jest/presets/js-with-ts\',\n reporters: [\'default\', [\'<rootDir>/reporters/slow-test\', { numTests: 3 }]],\n};\nRun Code Online (Sandbox Code Playgroud)\nnumTests: 3argument 意味着找到前 3 个最慢的测试用例。
index.test.js:
describe(\'find top slowest test\', () => {\n test(\'should 1\', (done) => {\n setTimeout(() => {\n expect(1 + 1).toBe(2);\n done();\n }, 1000);\n });\n test(\'should 2\', (done) => {\n setTimeout(() => {\n expect(1 + 1).toBe(2);\n done();\n }, 1200);\n });\n test(\'should 3\', (done) => {\n setTimeout(() => {\n expect(1 + 1).toBe(2);\n done();\n }, 100);\n });\n});\nRun Code Online (Sandbox Code Playgroud)\n测试结果:
\n PASS examples/find-top-slowest-test/index.test.js\n find top slowest test\n \xe2\x9c\x93 should 1 (1007 ms)\n \xe2\x9c\x93 should 2 (1201 ms)\n \xe2\x9c\x93 should 3 (103 ms)\n\nTest Suites: 1 passed, 1 total\nTests: 3 passed, 3 total\nSnapshots: 0 total\nTime: 3.588 s, estimated 4 s\nRan all test suites matching /\\/Users\\/dulin\\/workspace\\/github.com\\/mrdulin\\/jest-v26-codelab\\/examples\\/find-top-slowest-test\\/index.test.js/i.\n\nTop 3 slowest examples (2.311 seconds, 100.0% of total time):\n find top slowest test should 2\n 1.201 seconds ./examples/find-top-slowest-test/index.test.js\n find top slowest test should 1\n 1.007 seconds ./examples/find-top-slowest-test/index.test.js\n find top slowest test should 3\n 0.103 seconds ./examples/find-top-slowest-test/index.test.js\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
2987 次 |
| 最近记录: |