Jest Test Aggregation for Performance Analysis
You're tasked with building a reporting mechanism for your Jest test suite to better understand performance bottlenecks. Your goal is to aggregate test results, focusing on execution times, and generate a summary report. This will help identify slow-running tests and areas for optimization.
Problem Description
Your task is to create a function that takes an array of Jest test result objects and produces an aggregated report. This report should highlight key performance metrics such as the total number of tests run, the number of tests that passed, failed, and were skipped, and importantly, a list of the slowest tests sorted by execution time.
Key Requirements:
- Aggregate Test Counts: Calculate the total number of tests, passes, failures, and skips.
- Identify Slowest Tests: Determine the top N slowest tests based on their
durationproperty. N should be a configurable parameter. - Format the Report: Present the aggregated data in a clear, human-readable format.
Expected Behavior:
The function should return an object containing the aggregated counts and a sorted list of the slowest tests.
Edge Cases to Consider:
- An empty input array of test results.
- Tests with a duration of 0.
- Scenarios where N (the number of slowest tests to report) is larger than the total number of tests.
Examples
Example 1:
Input: [
{
"name": "renders without crashing",
"status": "passed",
"duration": 150,
"ancestorTitles": ["App"]
},
{
"name": "handles user input",
"status": "passed",
"duration": 300,
"ancestorTitles": ["App", "Form"]
},
{
"name": "throws an error on invalid data",
"status": "failed",
"duration": 200,
"ancestorTitles": ["Utils", "Validation"]
},
{
"name": "skips slow tests",
"status": "skipped",
"duration": 0,
"ancestorTitles": ["Performance"]
}
]
N: 2
Output: {
"totalTests": 4,
"passed": 2,
"failed": 1,
"skipped": 1,
"slowestTests": [
{
"name": "handles user input",
"duration": 300,
"ancestorTitles": ["App", "Form"]
},
{
"name": "throws an error on invalid data",
"duration": 200,
"ancestorTitles": ["Utils", "Validation"]
}
]
}
Explanation: The report summarizes the counts and lists the top 2 slowest tests, sorted by duration in descending order.
Example 2:
Input: []
N: 5
Output: {
"totalTests": 0,
"passed": 0,
"failed": 0,
"skipped": 0,
"slowestTests": []
}
Explanation: For an empty input, all counts are zero and the slowestTests array is empty.
Example 3:
Input: [
{
"name": "fast test",
"status": "passed",
"duration": 10,
"ancestorTitles": ["Simple"]
},
{
"name": "another fast test",
"status": "passed",
"duration": 5,
"ancestorTitles": ["Simple"]
}
]
N: 5
Output: {
"totalTests": 2,
"passed": 2,
"failed": 0,
"skipped": 0,
"slowestTests": [
{
"name": "fast test",
"duration": 10,
"ancestorTitles": ["Simple"]
},
{
"name": "another fast test",
"duration": 5,
"ancestorTitles": ["Simple"]
}
]
}
Explanation: When N is greater than the total number of tests, all tests (that have a duration) are included in the slowestTests list, sorted by duration.
Constraints
- The input
resultsarray will contain objects conforming to a Jest test result structure, each guaranteed to have at leastname(string),status(string: "passed", "failed", "skipped", etc.), andduration(number in milliseconds). Nwill be a non-negative integer.- The solution should be efficient enough to handle a large number of test results (e.g., thousands) without significant performance degradation.
Notes
- You'll need to define the structure of the input test result objects and the output report object using TypeScript interfaces.
- Consider how you will handle tests that might not have a
durationproperty (though the problem statement guarantees it, robust code might handle this). - The
slowestTestsshould only include tests that actually ran and have a non-zero duration, if you decide to filter out zero-duration tests for the "slowest" list. However, for this challenge, include all tests in the sorting if their duration is greater than 0. - Focus on correctness and clarity of the aggregation logic.