Here’s a quick talk by Vojta Jína on:
- Jasmine (a DSL for writing JS tests) and Karma (a runner for those tests) and
- Using them to write unit tests for JS code, and how you can make your JS code more testable.
On the last point, Vojta introduced three principles:
- Inversion of control (aka dependency injection)
- Avoid global, mutable state
- Law of demeter
On dependency injection: rather than (say) having a function which creates an object and then uses it, it’s better to provide the (already created) object as an argument to the function.
On global, mutable state: avoid it by (drumroll) passing it as an argument to the function!
On the law of demeter: objects should know as little about other objects in the system as is possible. So rather than (a) passing an object, (b) getting an attribute of that object and (c) using it (eg user.email), just pass the user’s email as an argument in the first place.