DEV Community

Nicolas DUBIEN
Nicolas DUBIEN

Posted on • Updated on

Advent of PBT 2021 - Day 3

Advent of PBT 2021 — Learn how to use property based testing and fast-check through examples

Today we will cover an algorithm often taken as an example when we talk about tests. While it is most of the time not really representative for real world examples let's cover it...

Our algorithm today is: fizzbuzz.
It comes with the following documentation and prototype:

 * FizzBuzz
 * @param n - The value to consider
 * @returns Fizz if divisible by 3, Buzz if divisible by 5,
 * the number as a string if none of the conditions apply
declare function fizzbuzz(n: number): string;
Enter fullscreen mode Exit fullscreen mode

A more detailed explanation of what you should expect out of it is given by Wikipedia in

In order to clarify our expectations, we already wrote some examples based tests for it:

it("should be itself for 1, 32 or 121", () => {

it("should be Fizz for 3, 6 or 33", () => {

it("should be Buzz for 5, 10 or 50", () => {

it("should be Fizz Buzz for 15, 30 or 150", () => {
  expect(fizzbuzz(15)).toEqual("Fizz Buzz");
  expect(fizzbuzz(30)).toEqual("Fizz Buzz");
  expect(fizzbuzz(150)).toEqual("Fizz Buzz");
Enter fullscreen mode Exit fullscreen mode

How would you cover it with Property Based Tests?

In order to ease your task we provide you with an already setup CodeSandbox, with examples based tests already written and a possible implementation of the algorithm:

You wanna see the solution? Here is the set of properties I came with to cover today's algorithm:

Back to "Advent of PBT 2021" to see topics covered during the other days and their solutions.

More about this serie on @ndubien or with the hashtag #AdventOfPBT.

Discussion (0)