Unit testing Node.js applications
Node.js has a built-in test runner, which is a convenient way to define and run unit tests. As much as I recommend TypeScript for development, unit tests are best written in pure JavaScript. Unit testing requires extensive use of fake objects – known as mocks – to isolate the code being tested from the rest of the application, and the process of creating mocks – known as mocking – relies on creating objects that have just enough functionality to run a test, which can upset the TypeScript compiler. To prepare for pure JavaScript unit tests, Listing 8.2 changes the configuration for the TypeScript compiler.
Deciding Whether to Unit Test
Being able to easily perform unit testing is one of the benefits of using Node.js, but it isn’t for everyone, and I have no intention of pretending otherwise. I like unit testing, and I use it in my projects, but not all of them, and not as consistently as you might...