Hone logo
Hone
Problems

Colorful Jest: Adding Color to Your Test Output

Writing clear and informative tests is crucial for maintainable code. Colored output in Jest can significantly improve readability, making it easier to quickly identify passing, failing, and pending tests. This challenge asks you to implement a Jest reporter that adds color to the test output, enhancing the developer experience.

Problem Description

You need to create a custom Jest reporter that adds ANSI color codes to the test output. The reporter should colorize the following elements:

  • Test Suite Names: Green
  • Test Case Names: Blue
  • Pass/Fail Status: Green for pass, Red for fail. Pending tests should remain their default color.
  • Error Messages (on failure): Yellow

The reporter should be compatible with existing Jest features and not interfere with other reporters. It should correctly handle nested test suites and multiple assertions within a single test case. The reporter should be able to handle Unicode characters in test names and error messages.

Examples

Example 1:

Input: Jest run with a test suite "My Suite" containing a passing test "My Test" and a failing test "My Failing Test" with an error message "Something went wrong".
Output:
My Suite
  ✓ My Test
  ✗ My Failing Test
    Something went wrong (yellow)

Explanation: The suite name is green, the passing test is blue with a green checkmark, the failing test is blue with a red 'x', and the error message is yellow.

Example 2:

Input: Jest run with a test suite "Nested Suite" containing a test suite "Inner Suite" with a passing test "Inner Test".
Output:
Nested Suite
  Inner Suite
    ✓ Inner Test

Explanation: Both suite names are green. The passing test is blue with a green checkmark.

Example 3: (Edge Case: Unicode characters)

Input: Jest run with a test suite "Suite with Unicode" containing a test "Test with Unicode: こんにちは".
Output:
Suite with Unicode
  ✓ Test with Unicode: こんにちは

Explanation: The test name, including the Unicode characters, is displayed correctly in blue.

Constraints

  • The reporter must be implemented as a Jest reporter plugin.
  • The reporter must use ANSI escape codes for coloring.
  • The reporter should not significantly impact Jest's performance. Avoid complex string manipulations that could slow down test execution.
  • The reporter must be compatible with both synchronous and asynchronous tests.
  • The reporter should handle test results with and without error messages gracefully.
  • The reporter should not modify the underlying test results object. It should only format the output.

Notes

  • You'll need to create a Jest reporter plugin that extends the BaseTestRunner class.
  • The onTestStart, onTestResult, and onRunComplete methods of BaseTestRunner are key to capturing test information.
  • Use ANSI escape codes to add color to the output. For example, \x1b[31m for red, \x1b[32m for green, \x1b[34m for blue, and \x1b[0m to reset the color.
  • Consider using a string builder for efficient string concatenation, especially when dealing with large test suites.
  • Test your reporter thoroughly with various test scenarios, including nested suites, passing/failing tests, asynchronous tests, and tests with Unicode characters.
  • Remember to package your reporter as a Jest plugin and install it locally for testing. You'll need a jest.config.js file to configure Jest to use your custom reporter.
Loading editor...
typescript