Lesson

Write a property-based test with TypeScript and fast-check

Access
Pro
Transcript
Available

Learn how to identify and fix a bug in a function using property-based testing in TypeScript with the fast-check library.

We start with a function isPrime that checks whether a number is prime. This function was initially tested with a list of known prime and non-prime numbers, which it passes successfully.

However, using fast-check, we implement property-based testing that tests the function across a broad range of integers to catch more errors. This helps us discover a bug where the function incorrectly identified perfectly square numbers as prime due to an off-by-one error.