Hone logo
Hone
Problems

Enhancing Jest Test Reports with Custom Summaries

Jest is a powerful JavaScript testing framework, but by default, its test reports can be quite verbose. This challenge focuses on creating a custom Jest reporter that generates a concise, yet informative summary of test results, highlighting key statistics and potential areas for improvement. This is crucial for quickly understanding the overall health of a project's test suite without sifting through individual test logs.

Problem Description

Your task is to create a custom Jest reporter in TypeScript that generates a detailed report summarizing test execution. This reporter should aggregate information from various test events (like runStart, testStart, testEnd, runEnd) and present it in a human-readable format. The report should go beyond the default Jest output by providing specific insights that can help developers quickly assess test suite quality.

Key Requirements:

  1. Custom Reporter Class: Create a TypeScript class that implements Jest's Reporter interface.
  2. Event Handling: Implement methods to handle relevant Jest test events:
    • onRunStart(results: AggregatedResult): Called when the test run starts.
    • onTestStart(test: Test): Called before each test suite starts.
    • onTestResult(test: Test, testResult: TestResult, aggResults: AggregatedResult): Called after each test suite finishes.
    • onRunComplete(test: Suite | null, results: AggregatedResult): Called when the entire test run is completed.
  3. Report Generation: In onRunComplete, generate a report string containing the following information:
    • Overall Summary:
      • Total test suites executed.
      • Total tests executed.
      • Number of tests passed.
      • Number of tests failed.
      • Number of tests skipped.
      • Number of tests pending.
    • Failure Details: A list of failed tests, including:
      • Test suite name.
      • Test name.
      • Error message and stack trace (truncated if excessively long, e.g., max 5 lines).
    • Performance Insights (Optional but Recommended):
      • Slowest tests (e.g., top 3 tests exceeding a defined threshold, like 500ms).
      • Average test execution time.
  4. Output Format: The generated report should be a plain text string. The reporter should output this string to the console using console.log().
  5. Configuration: The reporter should be configurable to accept options, such as a slowThreshold for identifying slow tests.

Expected Behavior:

When Jest runs with your custom reporter enabled, it should execute all tests. Upon completion, the custom reporter should print a clear, structured summary to the console, detailing the test outcomes and any identified issues or performance concerns.

Edge Cases to Consider:

  • No Tests Run: What happens if no tests are executed (e.g., due to file filtering)?
  • All Tests Pass: The report should still be informative, highlighting the success.
  • All Tests Fail: The failure details should be comprehensive.
  • Long Error Messages/Stack Traces: Implement a mechanism to prevent overly long output for failures.

Examples

Example 1: (A typical successful run)

Input: (Jest runs a suite of tests)

Output:
-------------------- Test Report Summary --------------------
Run Start: 2023-10-27T10:00:00.000Z
Run End: 2023-10-27T10:00:05.000Z

Total Test Suites: 2
Total Tests: 10
Tests Passed: 9
Tests Failed: 1
Tests Skipped: 0
Tests Pending: 0

-------------------- Failure Details --------------------
✖ 'User Authentication' - 'should login successfully'
  Error: Expected "user" to be defined.
  at Object.<anonymous> (/path/to/your/tests/auth.test.ts:15:10)
  at Generator.next (<anonymous>)
  at fulfilled (/path/to/your/tests/auth.test.ts:5:58)

-------------------- Performance Insights --------------------
Slowest Tests (Threshold: 500ms):
- 'Data Fetching' - 'should fetch user data efficiently': 750ms

Average Test Execution Time: 500ms
-----------------------------------------------------------

Example 2: (A run with multiple failures and no slow tests)

Input: (Jest runs a suite with several failures)

Output:
-------------------- Test Report Summary --------------------
Run Start: 2023-10-27T10:05:00.000Z
Run End: 2023-10-27T10:05:10.000Z

Total Test Suites: 3
Total Tests: 15
Tests Passed: 10
Tests Failed: 3
Tests Skipped: 1
Tests Pending: 1

-------------------- Failure Details --------------------
✖ 'API Integration' - 'should correctly handle POST requests'
  Error: API returned status code 500
  at /path/to/your/tests/api.test.ts:45:12

✖ 'User Management' - 'should update user profile'
  Error: Validation failed: Email format is invalid.
  at /path/to/your/tests/user.test.ts:22:8

✖ 'Database Operations' - 'should insert new record'
  Error: Duplicate key violation.
  at /path/to/your/tests/db.test.ts:30:11

-------------------- Performance Insights --------------------
No tests exceeded the slow threshold of 500ms.

Average Test Execution Time: 250ms
-----------------------------------------------------------

Constraints

  • Language: TypeScript
  • Jest Version: Compatible with Jest v27 or later.
  • Output: Plain text string printed to console.log.
  • Error Trace Truncation: Stack traces for failures should be truncated to a maximum of 5 lines.
  • Slow Threshold: The reporter should accept a slowThreshold option (in milliseconds), defaulting to 500ms.

Notes

  • You will need to install Jest and its types (@types/jest) if you haven't already.
  • To use a custom reporter, you'll typically modify your Jest configuration file (jest.config.js or jest.config.ts) to include your reporter.
  • Refer to the Jest documentation on Custom Reporters for how to implement and configure them.
  • Consider using moment.js or the built-in Date object for formatting timestamps.
  • The AggregatedResult and TestResult objects from Jest provide a wealth of information; explore their properties to gather the necessary data.
  • For performance insights, you'll need to access testResults[i].endTime - testResults[i].startTime.
Loading editor...
typescript