I found Mocha as a good solution used together with the Chai assertion library. Let’s see how to set up the testing environment for TypeScript.
As a first step we install the required packages with npm: mocha, chai, ts-node, and type definitions for both libraries:
Let’s create our first test case, and assert that our function works as expected. Obviously, in real TDD, we should write the test first and the function afterward, but for demonstration purposes, it should be ok.
If everything goes well the test should run and you should see in the console output that it passes.
It is a matter of taste which testing library you choose, the most important thing is to have as many tests as possible, they help us create maintainable and stable applications which are desired in software development.