concept loggingservice in category javascript

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
addToCartcalls theloggingServicewhen an error happens. In this case, examining that implementation detail is important because you want to enforce it.
An alternative to asserting on whether
loggingServiceis called, is to check the log file to which it writes. But that approach also has downsides. If you decide to change how you implementloggingServiceso that it logs to a different file, the test foraddItemToCart— and probably many others which rely on this same behavior — will fail too.Figure 2.14. When you have multiple tests checking whether
loggingServicewrites to the correct file, all of them will fail when you changeloggingService. Because you have more tests to update the cost of changingloggingServicesincreases.![]()
By asserting that
addToCartcallsloggingService— an implementation detail — you avoid unrelated tests failing whenloggingServicechanges. If you have rigorous tests forloggingService, they will be the only ones to break when you change the file to whichloggingServicewrites. 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
loggingServicewrites, its tests will be the only ones to fail. TheaddItemToCarttests 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.![]()