One definition of unit testing is the process of taking the smallest piece of testable code in an application, isolating it from the remainder of your codebase, and determining if it behaves exactly as expected.
For an application to be considered well tested, each function should ideally have its own separate unit tests where it’s tested against the different conditions you expect it to handle. All tests must pass before functionality is considered complete. This allows developers to modify a unit of code and its dependencies with a level of confidence about whether these changes have caused any breakage.
A basic example of unit testing is where a developer asserts that passing specific values to a sum function results in the correct value being returned. For an example more relevant to this book, we may wish to assert that adding a new todo item to a list correctly adds a model of a specific type to a Todos collection.
When you are building modern web applications, it’s typically considered a best practice to include automated unit testing as a part of your development process. In the following chapters, we are going to look at three different solutions for unit-testing your Backbone.js apps: Jasmine, QUnit, and SinonJS.