Every shipped app should come with a minimum set of tests. Writing tests lets you find problems early, facilitate changes and document your code. FoalTS is designed to be easily testable and provides the tools you need to write tests right away.
The testing ecosystem is based on the Mocha framework. It provides functions to help you structuring your tests and also making assertions.
describefunction groups tests (or groups of tests) together.
- And the
itfunction defines an individual test.
Using these two helpers lets you organize your tests in a readable way and print comprehensive reports.
Running this file with mocha gives you the below report:
In addition to the Mocha framework, you can use the Node.js built-in assert module. It provides some useful functions such as ok, strictEqual or deepStrictEqual to make your tests more readable and concise. You can also use third party libraries such as chai or expect.
The previous code can be refactored as follows: