Exploring unit testing
We already know that unit testing is the process of verifying the correctness of individual units of code, namely, functions and methods. The goal of unit testing is to ensure that each separate unit performs its task correctly, which, in turn, increases confidence in the reliability of the entire application. Here is an example:
export function sum(a: number, b: number): number {
return a + b;
}
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
The preceding code represents the most basic and simplest test of a function that adds two values. The test code itself is a function that calls a special method, expect, which takes a value and then has a series of methods allowing for the checking and comparing of results.
Looking at this code, the first questions that might come to mind are: Is it really necessary to write another three lines of tests for such a simple three-line function? Why test such a function at all? I would answer...