Hone logo
Hone
Problems

Jest Custom Reporter: Detailed Statistics and Filtering

This challenge asks you to build a custom Jest reporter that provides more detailed statistics than the default reporters, including breakdowns of test results by file and category (if you define categories). The reporter should also allow filtering of test results based on status (passed, failed, skipped). This is useful for gaining deeper insights into test suite performance and identifying areas needing improvement.

Problem Description

You need to create a Jest custom reporter in TypeScript that extends the JestReporter base class. The reporter should:

  1. Collect Detailed Statistics: For each test file, track the number of tests passed, failed, and skipped. Also, if a test has a category property (e.g., test.category = 'integration'), track the number of tests passed, failed, and skipped per category within each file.
  2. Filtering: Implement a filtering mechanism. The reporter should accept a filter argument (e.g., "passed", "failed", "skipped", or "all") via a command-line argument (e.g., --reporter=<path-to-reporter-file> --filter=failed).
  3. Formatted Output: Present the collected statistics in a clear, human-readable format to the console. The output should include:
    • A summary of the entire test suite (total tests, passed, failed, skipped).
    • For each file:
      • File name
      • Total tests in the file
      • Passed tests in the file
      • Failed tests in the file
      • Skipped tests in the file
      • (If categories are used) A breakdown of passed, failed, and skipped tests per category within the file.
  4. Handle Test Categories: Assume tests can optionally have a category property defined on the test object (e.g., test.category = 'integration'). The reporter should correctly aggregate statistics based on these categories.
  5. Respect Filtering: Only display results that match the specified filter. If no filter is provided, display all results ("all").

Examples

Example 1:

Input:  Test suite with 3 files: file1.ts (2 passed, 1 failed), file2.ts (3 passed), file3.ts (1 passed, 1 skipped).  No filter specified.
Output:
Jest Reporter Statistics:

Total:
  All tests:       6
  Passed:          5
  Failed:          1
  Skipped:         1

file1.ts:
  Total:           3
  Passed:          2
  Failed:          1
  Skipped:         0

file2.ts:
  Total:           3
  Passed:          3
  Failed:          0
  Skipped:         0

file3.ts:
  Total:           2
  Passed:          1
  Failed:          0
  Skipped:         1

Example 2:

Input: Test suite with 3 files: file1.ts (2 passed, 1 failed, category 'unit'), file2.ts (3 passed, category 'integration'), file3.ts (1 passed, 1 skipped). Filter specified: --filter=failed
Output:
Jest Reporter Statistics:

Total:
  All tests:       6
  Passed:          4
  Failed:          1
  Skipped:         1

file1.ts:
  Total:           3
  Passed:          2
  Failed:          1
  Skipped:         0

Example 3: (Edge Case - Empty Test Suite)

Input:  An empty test suite (no tests run).
Output:
Jest Reporter Statistics:

Total:
  All tests:       0
  Passed:          0
  Failed:          0
  Skipped:         0

Constraints

  • The reporter must be written in TypeScript.
  • The reporter must extend the JestReporter base class.
  • The filtering mechanism must be implemented using command-line arguments passed to Jest.
  • The output must be printed to the console using console.log.
  • The reporter should handle cases where tests do not have a category property gracefully (i.e., don't throw errors).
  • The reporter should be compatible with Jest versions 27 or higher.

Notes

  • You'll need to use the process.argv array to access command-line arguments.
  • The onTestStart, onTestResult, onRunComplete methods of the JestReporter class are crucial for collecting and processing test data.
  • Consider using a data structure (e.g., a map or object) to store the statistics for each file and category.
  • Think about how to handle potential errors or unexpected input gracefully.
  • The test.category property is optional and can be a string.
  • Focus on clear and readable code. Good formatting and comments are appreciated.
  • You don't need to implement any complex UI or formatting beyond console output. The goal is to demonstrate the ability to collect and filter test statistics.
Loading editor...
typescript