Our first tests#
As a reminder, here’s our final implementation:
export const fizzBuzz = (i: number): string => {
if (i % 15 === 0) {
return "fizzbuzz";
}
if (i % 3 === 0) {
return "fizz";
}
if (i % 5 === 0) {
return "buzz";
}
return i.toString(10);
};
Note that we export the fizzbuzz function, so we can import it later on.
Writing some tests#
Writing a main and checking the output by hand is not fun, so let’s write some tests instead:
import { fizzBuzz } from "./fizzbuzz.ts";
import { assertEquals } from "@std/assert";
Deno.test("Print 'fizz' when divisible by 3", () => {
assertEquals(fizzBuzz(3), "fizz");
assertEquals(fizzBuzz(6), "fizz");
assertEquals(fizzBuzz(9), "fizz");
});
Deno.test("Print 'buzz' when divisible by 5", () => {
assertEquals(fizzBuzz(5), "buzz");
assertEquals(fizzBuzz(10), "buzz");
assertEquals(fizzBuzz(20), "buzz");
});
Deno.test("Print 'fizzbuzz' when divisible by 15", () => {
assertEquals(fizzBuzz(15), "fizzbuzz");
assertEquals(fizzBuzz(30), "fizzbuzz");
});
Deno.test("Print number otherwise", () => {
assertEquals(fizzBuzz(2), "2");
assertEquals(fizzBuzz(4), "4");
});
Running the tests#
$ deno test fizzbuzz_test.ts
Check fizzbuzz_test.ts
running 4 tests from ./fizzbuzz_test.ts
Print 'fizz' when divisible by 3 ... ok
Print 'buzz' when divisible by 5 ... ok
Print 'fizzbuzz' when divisible by 15 ... ok
Print number otherwise ... ok
ok | 4 passed | 0 failed (4ms)