So, What Do We Have Here?
In the last post I used Node’s event library to help me unwind the callback Pyramid of Pain. I then used eventing to wire different modules together, just the way I wanted.
There are a lot of questions that come of this, specifically:
- We have modules within modules – how do you structure each one’s events?
- Is this event stuff really necessary when working with Node?
- How do you test this stuff anyway?
These are the questions I asked myself, at least, and I’m assuming you’ll have similar ones. Let’s drill through them.
Modules and Events
The role of a module is, like any module, to encapsulate functionality. Our CMS module will encapsulate everything related to publishing content. Inside of that module we have a Writer and a Publisher – which encapsulate what they do, respectively.
EventEmitters Are Interesting, Not Required
That said – there are a number of different ways to wire up program flow:
- Using promises with the Q library
- Explicitly declaring flow structures with the node-async library. I personally LOVE this library and use it often
- Just going with Callbacks and not worrying.
Once you embrace it and get your mind used to it – it becomes quite fun!
I don’t test events – but it’s definitely possible with sinon. This is a simple spy that will tell you if a certain callback or function was fired. I don’t find this necessary because, ultimately, you’re testing to see if Node’s event library actually works.
Instead I find it easier to test things as you’ve seen me do – individual units and then an overall “acceptance test” that makes sure everything gets fired.
Wrapping It Up
I’ve really enjoyed diving into these posts – but I think here is a good place to stop. This all started because of my interactions with the Ghost team – and I thought it would be a nice idea to explain my thoughts in depth.
I don’t really want to create a blog engine – but if you want to use my code I say go for it! I’ll keep the code up on Github so fork away if you find it useful!
Thanks so much for reading along!