Hone logo
Hone
Problems

Testing Generator Functions with Jest

Generator functions provide a powerful way to create iterators in JavaScript and TypeScript. Testing these functions can be tricky because they don't return a value directly; instead, they yield a series of values over time. This challenge focuses on writing Jest tests to effectively verify the behavior of generator functions, ensuring they produce the expected sequence of values.

Problem Description

You are tasked with writing Jest tests for a TypeScript generator function that produces a sequence of numbers based on a given starting value and a step. The generator function, numberGenerator, should yield numbers starting from start and incrementing by step until it reaches a value greater than or equal to end. Your tests should verify that the generator produces the correct sequence of numbers and handles edge cases appropriately.

What needs to be achieved:

  • Implement Jest tests to verify the output of the numberGenerator function.
  • Ensure the tests cover various scenarios, including different start, step, and end values.
  • Confirm the generator stops correctly when the generated value reaches or exceeds the end value.

Key Requirements:

  • The tests must use Jest's iterator testing capabilities (e.g., expect(generator).toEqual(expectedSequence)).
  • The tests should be clear, concise, and well-documented.
  • The tests should cover both positive and edge case scenarios.

Expected Behavior:

The numberGenerator function should yield a sequence of numbers. The tests should verify that the sequence matches the expected output for different input values. The generator should stop yielding values when the next value would be greater than or equal to the end value.

Edge Cases to Consider:

  • start is equal to end.
  • step is zero (should not yield any values).
  • step is negative (should yield values in descending order).
  • start is greater than end.
  • Large values for start, step, and end (consider potential performance implications, though this is not a primary focus).

Examples

Example 1:

Input: numberGenerator(1, 2, 10)
Output: [1, 3, 5, 7, 9]
Explanation: The generator starts at 1, increments by 2, and stops when the value reaches or exceeds 10.

Example 2:

Input: numberGenerator(5, 1, 5)
Output: [5]
Explanation: The generator starts at 5, increments by 1, and stops when the value reaches or exceeds 5.

Example 3:

Input: numberGenerator(10, -1, 1)
Output: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
Explanation: The generator starts at 10, decrements by 1, and stops when the value reaches or exceeds 1.

Constraints

  • The numberGenerator function is provided (see below). You are only required to write the Jest tests.
  • The input values (start, step, end) will be numbers.
  • The tests should execute within a reasonable time frame (less than 1 second).
  • The tests should not modify the numberGenerator function.

Notes

  • Consider using Jest's toEqual matcher to compare the generated sequence with the expected sequence.
  • Think about how to handle the case where the generator yields no values (e.g., when step is zero or start is greater than or equal to end).
  • The provided numberGenerator function is a starting point. Focus on writing robust and comprehensive tests.
function* numberGenerator(start: number, step: number, end: number): Generator<number, void, unknown> {
  let current = start;
  while (current < end) {
    yield current;
    current += step;
  }
}
Loading editor...
typescript