Hone logo
Hone
Problems

Jest Test Analytics: Track and Report Test Results

Understanding test performance is crucial for maintaining a healthy and reliable codebase. This challenge asks you to implement a system that tracks key metrics about your Jest tests (e.g., number of tests passed, failed, total, execution time) and reports them in a clear and concise format after the test suite completes. This will allow you to identify slow tests, flaky tests, and areas where test coverage might be lacking.

Problem Description

You need to create a Jest reporter that collects and displays analytics about the test run. The reporter should:

  1. Collect Metrics: Track the following metrics during the test run:

    • totalTests: The total number of tests executed.
    • passedTests: The number of tests that passed.
    • failedTests: The number of tests that failed.
    • skippedTests: The number of tests that were skipped.
    • totalDuration: The total execution time of all tests in milliseconds.
  2. Report Results: After all tests have completed, the reporter should output a summary to the console in a human-readable format. The output should include all the collected metrics.

  3. Integration: The reporter should be easily integrated into a Jest configuration file.

  4. Error Handling: The reporter should gracefully handle any errors that might occur during test execution without crashing the test suite. It should log the error but continue reporting the available metrics.

  5. Asynchronous Tests: The reporter must correctly handle asynchronous tests (e.g., tests using async/await).

Examples

Example 1:

Input: A Jest test suite with 5 tests: 3 pass, 1 fail, 1 skip. Total duration: 100ms.
Output:
Jest Test Analytics:

Total Tests:       5
Passed Tests:      3
Failed Tests:      1
Skipped Tests:     1
Total Duration:    100ms

Example 2:

Input: A Jest test suite with 10 tests, all passing. Total duration: 500ms.
Output:
Jest Test Analytics:

Total Tests:       10
Passed Tests:      10
Failed Tests:      0
Skipped Tests:     0
Total Duration:    500ms

Example 3: (Edge Case - Error during test execution)

Input: A Jest test suite where one test throws an error.
Output:
Jest Test Analytics:

Total Tests:       [Number of tests executed before the error]
Passed Tests:      [Number of tests passed before the error]
Failed Tests:      [Number of tests failed before the error, including the one that threw]
Skipped Tests:     [Number of tests skipped]
Total Duration:    [Total duration up to the point of the error]
Error: [Error message from the test that threw]

Constraints

  • The reporter must be written in TypeScript.
  • The reporter should not significantly impact the overall test execution time (aim for minimal overhead).
  • The output format should be consistent and easy to parse visually.
  • The reporter should be compatible with Jest versions 25 or higher.
  • The reporter should not modify the existing Jest output. It should be appended to it.

Notes

  • You'll need to create a Jest custom reporter. Refer to the Jest documentation for details on how to create custom reporters: https://jestjs.io/docs/creating-custom-reporters
  • Consider using the process.on('uncaughtException', ...) or process.on('unhandledRejection', ...) to catch errors that might occur during test execution.
  • The jest-runner API provides access to test events (start, pass, fail, skip, end). Use these events to collect the necessary metrics.
  • Think about how to format the output for readability. A simple table or list is often effective.
  • Focus on correctness and clarity of the code. While performance is a consideration, prioritize a working solution first.
Loading editor...
typescript