TDD in action
There are some edge cases that we are not covering in our utils
module. For example, what happens if we pass a string to the sum
function?
import { sum } from "../utils.js"; const result = sum("1", 2); // 12
This is not the expected behavior when we use the sum
function, so we need to fix it.
Let’s add some tests to cover these edge cases in our jest-tests/utils.test.js
file:
describe("Utils Test Suite: sum", () => { it("Should sum two numbers", () => { expect(sum(1, 2)).toBe(3); }); it("Should throw an error if we don't provide a valid number", () => { expect(() => sum("1", 2)).toThrow("Please provide a valid number"); }); });
As you can see, we are using the toThrow
matcher to test that the function is throwing an error. Now, let’s run the test coverage with npm...