Hone logo
Hone
Problems

Jest-Fast-Check: Property-Based Testing for Robust Functions

This challenge is about integrating property-based testing into your Jest test suite using fast-check. Property-based testing helps you write more robust and comprehensive tests by defining general properties that your functions should uphold, rather than testing specific input/output pairs. You will create a simple function and then write Jest tests for it using fast-check to ensure its correctness across a wide range of inputs.

Problem Description

Your task is to implement a function that takes an array of numbers and returns a new array containing only the even numbers from the input array. After implementing this function, you will write Jest tests for it using the fast-check library. These tests should verify that the function correctly filters even numbers by defining and testing properties that should hold true for any valid input array.

Key Requirements:

  1. Implement filterEvenNumbers function:

    • The function should accept an array of numbers (number[]).
    • It should return a new array containing only the even numbers from the input array, preserving their original order.
    • If the input array is empty or contains no even numbers, it should return an empty array.
  2. Write Jest tests using fast-check:

    • You need to define properties that characterize a correct filterEvenNumbers implementation.
    • Use fast-check's fc.assert function within your Jest tests to verify these properties.

Expected Behavior:

  • The filterEvenNumbers function should correctly identify and return only even numbers.
  • The Jest tests should pass, indicating that the function satisfies the defined properties.

Edge Cases to Consider:

  • Empty input array.
  • Arrays containing only odd numbers.
  • Arrays containing only even numbers.
  • Arrays containing a mix of positive, negative, and zero values.

Examples

Example 1:

Input: [1, 2, 3, 4, 5, 6]
Output: [2, 4, 6]
Explanation: The function correctly identifies and returns the even numbers 2, 4, and 6.

Example 2:

Input: [1, 3, 5, 7, 9]
Output: []
Explanation: The input array contains no even numbers, so an empty array is returned.

Example 3:

Input: []
Output: []
Explanation: An empty input array results in an empty output array.

Constraints

  • The input array will contain only numbers.
  • The function should not mutate the original input array.
  • The fast-check library should be used for property-based testing.
  • Tests should be written in TypeScript.

Notes

  • To use fast-check with Jest, you'll typically need to install it: npm install --save-dev fast-check @types/jest.
  • Consider properties such as:
    • Every number in the output array must be even.
    • The order of even numbers in the output array must be the same as in the input array.
    • The output array should not contain any numbers that were not present in the input array.
  • Think about how fast-check can generate various types of number arrays for testing.
Loading editor...
typescript