Here’s an inside look at our gauge library’s test suite:
Getting started with Jasmine
First, download the latest Jasmine build from Github. Then rename the folders to tests. Then add a new spec to the spec folder.
Now, your tests folder will look something like this:
SpecRunner.html will run all the spec suites you ask it to:
SpecRunner is also handy for setting up DOM test harnesses:
Suites and Specs
Jasmine is composed of suites and specs.
A suite is composed of a list of specs:
Once you get this core concept, you’re up and running. Things can get really interesting after that. But, the crux of the matter is you’re testing and that’s great.
You can’t test all the things all the time
A great man once said, “Errors using inadequate data are much less than those using no data at all.” That man was Charles Babbage, a pioneer of computer engineering and science.
Initially, we had around 55 tests. At first, you think Jasmine will be just another step in the development process. And not just another step, but another unneeded step - a waste of time. Try not to think about it like that. Instead, think about Jasmine as a friend. Someone who always has your back covered, even when you don’t expect it. Which is the whole point of this conversation.
Listen, you can’t test all the things all the time. Our gauge library currently has 72 exposed properties and that’s not even including class methods. Do you think any developer can code new features and still validate existing features at the same time? Hell no! Well, maybe Jon Skeet can. But you, my friend, are no Jon Skeet.
Jasmine is beautiful
In each of the scenarios above, you see me using a gauge object. Every spec must have access to an instance of the Gauge class to run.
To accomplish this, we could’ve done something like this:
After awhile, creating instance-after-instance becomes tedious at-best. There are better ways.
Setup and Teardown
Jasmine provides methods to help you Don’t Repeat Yourself (DRY):
Before each spec is called, I create an instance of the Gauge class and attach it to a DOM test harness called tests:
This DOM element is located in SpecRunner.html. After each spec has finished executing, I delete the gauge instance. This technique works remarkably well for many scenarios.
Using Jasmine Matchers
Jasmine BDD has several built-in matchers. A full list of Jasmine matchers are available on the Jasmin Wiki. Here are a few of the matchers we use and how to implement them:
The matcher toEqual compares objects or primitives x and y and passes if they are equivalent.
The matcher toBeUndefined passes if x is undefined.
The matcher toBeGreaterThan passes if x is greater than y.
The matcher toContain passes if array or string x contains y.
You can even extend Jasmine to write your own custom matchers.
Closing thoughts on using Jasmine BDD
With Jasmine, we’re able to focus our limited time and energy to refactor and craft killer features. Jasmine gives us the confidence to know we’re not breaking existing functionality.
We started with 55 tests. Now, we’re up to 111 test specs and counting. I couldn’t be happier with the results. Since version 2.0, Jasmine has meant an increase in quality that is now part of our gauge library’s DNA and that means everything to me.