concept loggingservice in category javascript

appears as: loggingService, loggingService
Testing JavaScript Applications

This is an excerpt from Manning's book Testing JavaScript Applications.

If logging errors is critical for debugging your application, you should enforce it with tests. You should have tests which verify whether addToCart calls the loggingService when an error happens. In this case, examining that implementation detail is important because you want to enforce it.

An alternative to asserting on whether loggingService is called, is to check the log file to which it writes. But that approach also has downsides. If you decide to change how you implement loggingService so that it logs to a different file, the test for addItemToCart — and probably many others which rely on this same behavior — will fail too.

Figure 2.14. When you have multiple tests checking whether loggingService writes to the correct file, all of them will fail when you change loggingService. Because you have more tests to update the cost of changing loggingServices increases.
02 14

By asserting that addToCart calls loggingService — an implementation detail — you avoid unrelated tests failing when loggingService changes. If you have rigorous tests for loggingService, they will be the only ones to break when you change the file to which loggingService writes. Fewer breaking tests mean you have fewer tests to update and therefore fewer costs to maintain them.

Figure 2.15. If you change the file to which loggingService writes, its tests will be the only ones to fail. The addItemToCart tests will continue to pass because they are doing what you expect: using the logging service. By structuring your tests in this way, you have fewer tests to update and more precise feedback about what piece of your software doesn’t comply with the tests.
02 15