Hone logo
Hone
Problems

Implementing Jest's test.each for Enhanced Testing

Jest's test.each provides a concise and readable way to run the same test with multiple sets of inputs and expected outputs. This challenge asks you to implement a simplified version of test.each functionality, allowing you to define a test that executes repeatedly with different data sets. This is useful for testing functions with various inputs and verifying consistent behavior.

Problem Description

You need to create a function called each that mimics the core functionality of Jest's test.each. The each function should accept an array of data sets (each data set being an array of arguments) and a test function as arguments. For each data set, the each function should call the test function with the data set's arguments. The test function should receive the arguments passed to it and should ideally return a boolean indicating whether the test passed or failed. The each function should then report the results of each test case, indicating whether it passed or failed.

Key Requirements:

  • Data Set Iteration: The function must iterate through each data set provided in the input array.
  • Argument Passing: Each data set's arguments must be correctly passed to the test function.
  • Test Execution: The test function must be executed for each data set.
  • Result Reporting: The function must clearly indicate whether each test case passed or failed. A simple "passed" or "failed" string is sufficient for reporting.
  • Test Function Signature: The test function should accept an arbitrary number of arguments, corresponding to the number of elements in each data set.

Expected Behavior:

The each function should take an array of data sets and a test function. For each data set, it should call the test function with the data set's values. The function should then output a message indicating whether each test case passed or failed, based on the return value of the test function (true for pass, false for fail).

Edge Cases to Consider:

  • Empty Data Set Array: What should happen if the input array of data sets is empty? The function should not throw an error, but should not execute the test function either.
  • Empty Data Set: What should happen if a data set within the array is empty? The test function should be called with no arguments.
  • Test Function Errors: What should happen if the test function throws an error? The function should report the error as a failure.
  • Data Set Argument Mismatch: While not strictly required, consider how you might handle data sets with a different number of arguments than the test function expects. For simplicity, you can assume the number of arguments in each data set matches the test function's expected arguments.

Examples

Example 1:

Input:
[
  [1, 2],
  [3, 4],
  [5, 6]
]
(a: number, b: number) => a + b === 7

Output:
"Test Case 1: passed (1 + 2 === 7)"
"Test Case 2: passed (3 + 4 === 7)"
"Test Case 3: passed (5 + 6 === 7)"

Explanation: The each function iterates through the data sets. For each data set, it calls the test function with the corresponding arguments. The test function checks if the sum of the arguments equals 7.

Example 2:

Input:
[
  [1, 2],
  [3, 4],
  [5, 6]
]
(a: number, b: number) => a + b === 8

Output:
"Test Case 1: failed (1 + 2 === 8)"
"Test Case 2: failed (3 + 4 === 8)"
"Test Case 3: failed (5 + 6 === 8)"

Explanation: The test function now returns false for all cases because the sum is not equal to 8.

Example 3:

Input:
[
  [1],
  [2],
  [3]
]
(a: number) => a > 0

Output:
"Test Case 1: passed (1 > 0)"
"Test Case 2: passed (2 > 0)"
"Test Case 3: passed (3 > 0)"

Explanation: The test function checks if the input is greater than 0.

Constraints

  • The input array of data sets can contain up to 100 data sets.
  • Each data set can contain up to 5 arguments.
  • The test function must be a function that accepts an arbitrary number of arguments and returns a boolean.
  • The function should not throw errors for empty input arrays or empty data sets.

Notes

  • Focus on the core functionality of iterating through data sets and calling the test function with the correct arguments.
  • You don't need to implement full Jest-style reporting (e.g., grouping tests, detailed error messages). A simple "passed" or "failed" message is sufficient.
  • Consider using a loop (e.g., for...of) to iterate through the data sets.
  • The test function is provided as a callback, so you don't need to define it beforehand.
  • Error handling within the test function is the responsibility of the test function itself. Your each function should simply report whether the test function returned true or false, or if it threw an error.
Loading editor...
typescript