This is the first post in an ongoing series of blogposts I will be writing about the Jasmine testing framework for javascript.
The Jasmine javascript testing framework is a beautifully developed framework that is very “English” based in its approach. Once your tests are written they read like documentation you would generally write for an application. The major benefit of this is that when you revisit code or hand it off to a colleague it is easy to pickup where you left off and its easy to get to understand what your code is doing.
This introduction to Jasmine will *not* cover any basics of TDD/BDD, if you are new to testing I highly suggest grabbing Roy Osherove’s book on Unit Testing.
Where to download and setup the Jasmine Framework:
- Jasmine on gitHub
- Once you clone the repo there will be a SpecRunner.html file, this is where you will reference your Source files and Tests and will see the results.
- In the HEAD tag of the SpecRunner.html file you find the jasmine-html.js script file, after this script you should list all the source files you would like to test and then the tests for those source files.
Getting Started:
Lets create a basic object:
function Customer() {
this.customerName = '';
};
Save this file as customer.js and link this file in your SpecRunner.html just after the jasmine framework file mentioned in the introduction. Now we will create our first test for our object. Create a new .js file called customer_specs.js and enter the following:
describe("Customer Object Tests", function(){
var customerTest = new Customer();
it("should contain a customerName object", function(){
expect(customerTest.customerName).toBeDefined();
});
};
Once you have saved your changes and linked the customer_spec.js file after the customer.js file in the SpecRunner.html file, load the .html file in your browser of choice. Here you can see that the tests are grouped by the “describe” blocks used by Jasmine. You can nest multiple “describe” blocks to keep your tests organised.
Looking at the test itself lets break it down:
- All tests make use of the it() function. This takes 2 arguments
- Argument 1 is a string, and this string describes the test. In the case above, we want to ensure that the customer object should contain a customerName object.
- The second argument is a function call that contains the code to perform the test.
- In most testing frameworks your test would end with an “assert” in some format. The “assert” in the Jasmine framework is the “expect” function. A callback is passed into the “expect” function.
- A matcher is then applied to the result of the “expect”.
- In this case with are using the “toBeDefined” matcher that will ensure that the callback passed to the expect is defined.
Other basic matches for standard values include:
expect(x).toEqual(y); compares objects or primitives x and y and passes if they are equivalent
expect(x).toBe(y); compares objects or primitives x and y and passes if they are the same object
expect(x).toMatch(pattern); compares x to string or regular expression pattern and passes if they match
expect(x).toBeDefined(); passes if x is not undefined
expect(x).toBeUndefined(); passes if x is undefined
expect(x).toBeNull(); passes if x is null
expect(x).toBeTruthy(); passes if x evaluates to true
expect(x).toBeFalsy(); passes if x evaluates to false
expect(x).toContain(y); passes if array or string x contains y
expect(x).toBeLessThan(y); passes if x is less than y
expect(x).toBeGreaterThan(y); passes if x is greater than y
expect(fn).toThrow(e); passes if function fn throws exception e when executed
Should you want to invert any of these matchers you simply prefix the matcher with “.not”. Instead of calling expect(x).toEqual(‘xyz’), you would call expect(x).not.toEqual(‘xyz’);
Finishing off:
This is just a very basic intro to the Jasmine framework. Next up I will discuss writing custom matchers. Below are some links you may want to check out including the files from a demo I recently gave.
Links:
- Jasmine Documentation
- Demo Files from a talk I gave recently