If you’ve been following my Twitter, you won’t be surprised by this week’s Spotlight choice. Ember-Sinon is an essential addon that provides an Ember CLI wrapper around Sinon, a framework-agnostic stubbing library. Some of you may know of my own related library, Js.Edgar, and wonder why I’m promoting Sinon instead. Simply put, I wrote Edgar during a time when Sinon didn’t play nicely with Ember CLI, before Ember-Sinon existed. Furthermore, I wrote it to be a super lightweight library focused on the unit testing story for spies & stubs. It still does that very well, and I’m proud to have an easy-to-understand DSL for the API that effectively accomplishes its goal. However, Sinon was built for more extensive purposes, and it may be better suited for ambitious applications. Additionally, it has an Ember CLI addon, whereas I haven’t gotten around to writing one for Edgar yet (though you can still easily use it as a global in your Ember app). There are still use-cases where each library is useful, especially given that Sinon has some overlap with addons like Ember-CLI-Mirage. YMMV, so pick what works for you, which brings me back to the main point.
Ember-Sinon gives us an easy bridge to the Sinon library, allowing us to create spies, stubs, mocks, and more in our tests. For unit testing, this means we can test the I/O of a specific method without allowing any additional methods to be executed, so that our assertions are verifying the exact behavior we want to test. If you’ve seen Ember-Sinon before, you might know that it had a shim to integrate Sinon-QUnit, a great library that turns each test callback into a sandboxed Sinon environment, making it easy to create and cleanup spies without any manual work. However, this created a problem for two reasons:
- Ember-QUnit also wraps the test callbacks and executes them with a custom context from Ember-Test-Helpers, which in turn destroyed the context that the Sinon sandbox was created in.
- With Sinon being a framework-agnostic library, it didn’t make much sense for Ember-Sinon to deviate from that path to specifically support QUnit.
So, for my Open Source Good Deed™ this week, I decided to solve both of these problems with a brand new addon, Ember-Sinon-QUnit! This project exports a new test that properly wraps test callbacks to play nice with Ember-QUnit so that developers have a simple drop-in solution for sandboxing their spies, stubs, and mocks. Furthermore, the release of this addon enabled Ember-Sinon to drop explicit support for QUnit, which improves the story for integrating Sinon with other testing libraries, such as Ember-CLI-Mocha. In fact, mocha users could build a similar Ember-Sinon-Mocha addon that applies the same sandboxing technique to their tests. If you do, let me know and I’ll update this post to recommend it, too.
I recommend Sinon, Ember-Sinon, and Ember-Sinon-QUnit so strongly that I have added them into Ember-CLI-Opinionated’s latest release. ECO now provides a “Testing” enhancement under the extended questionnaire that includes these and other essential addons for testing your Ember applications. Regardless of how you install them, go out and improve your testing story by adding Ember-Sinon and Ember-Sinon-QUnit to your projects today!