Hone logo
Hone
Problems

Fast-Check Testing with Jest in TypeScript

Fast-check (or property-based testing) is a powerful technique for uncovering edge cases and ensuring the correctness of your code. This challenge asks you to implement a Jest test suite using fast-check to verify a function that determines if a number is a prime number. This exercise will help you understand how to leverage fast-check to generate a wide range of inputs and test your code against a variety of scenarios, leading to more robust and reliable software.

Problem Description

You are given a TypeScript function isPrime(n: number): boolean that aims to determine if a given number n is a prime number. Your task is to create a Jest test suite using fast-check to thoroughly test this function. The test suite should define properties that a prime number should satisfy and then use fast-check to generate random inputs and verify that these properties hold true.

What needs to be achieved:

  • Implement a Jest test suite using fast-check.
  • Define properties that a prime number should satisfy (e.g., it should be greater than 1, it should only be divisible by 1 and itself).
  • Use fast-check to generate a large number of random integer inputs.
  • Verify that the isPrime function returns the correct result for each generated input, based on the defined properties.

Key Requirements:

  • The test suite must use fast-check for property-based testing.
  • The properties tested must be logically sound and cover a reasonable range of prime and non-prime numbers.
  • The test suite should be well-structured and easy to understand.
  • The tests should be efficient and avoid unnecessary computations.

Expected Behavior:

The test suite should pass for a correctly implemented isPrime function. It should also fail if the isPrime function contains any bugs or incorrect logic. The tests should generate a diverse set of inputs, including small numbers, large numbers, prime numbers, and non-prime numbers.

Edge Cases to Consider:

  • Numbers less than or equal to 1 (not prime).
  • 2 (the only even prime number).
  • Large prime numbers.
  • Numbers that are close to prime numbers (e.g., numbers that are one less or one more than a prime number).
  • Negative numbers (should ideally be handled gracefully, though the problem doesn't explicitly require it).

Examples

Example 1:

Input: isPrime(2)
Output: true
Explanation: 2 is a prime number.

Example 2:

Input: isPrime(4)
Output: false
Explanation: 4 is divisible by 1, 2, and 4, so it's not prime.

Example 3:

Input: isPrime(7)
Output: true
Explanation: 7 is only divisible by 1 and 7, so it's prime.

Constraints

  • The isPrime function will receive integer inputs.
  • The generated random numbers should be within a reasonable range (e.g., -1000 to 10000). You can adjust this range as needed for testing.
  • The test suite should execute within a reasonable time frame (e.g., less than 5 seconds). Optimize your tests if necessary.
  • You are expected to use TypeScript.

Notes

  • You'll need to install fast-check and @types/fast-check as development dependencies in your project: npm install --save-dev fast-check @types/fast-check
  • Consider using fast-check's generators to create specific types of inputs (e.g., integers within a certain range).
  • Think about how to express the properties of a prime number in a way that fast-check can easily verify. For example, you can check that a prime number is not divisible by any number between 2 and the number itself.
  • Focus on creating tests that are concise, readable, and effective at uncovering potential bugs.
  • The isPrime function is not provided; you are only testing it. Assume it exists and is accessible in your test file.
Loading editor...
typescript