Mastering Jasmine, a JavaScript Unit Testing Basics

Category Quality Assurance

“More than the act of testing, the act of designing tests is one of the best bug preventers known. The thinking that must be done to create a useful test can discover and eliminate bugs before they are coded — indeed, test-design thinking can discover and eliminate bugs at every stage in the creation of software, from conception to specification, to design, coding and the rest.”
 — Boris Beizer

Unit Testing lets a developer validate a unit (class, function) of software. A developer may unknowingly make a change to an existing unit which could make the software go awry. Unit testing prevents such accidental cases. As soon as the developer writes some awry code for a unit, the test runner will let the person know which test case has failed.

Coming to Javascript, various testing frameworks are available. Few popular ones are:
Mocha
Chai
Jasmine

In the rest of the article, I would focus on few basics of the Jasmine framework.

“Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM (Document Object Model). And it has a clean, obvious syntax so that you can easily write tests.” -Jasmine Official Documentation

Getting started with Jasmine is fairly easy with below mentioned instructions:

Installation:

npm install –save-dev jasmine
Initialise Project:
jasmine init
Seed Project with Examples:
jasmine examples
Run Test Suite:
jasmine

A sample test case from the official doc looks like:

describe(“A suite is just a function”, function() {
var a;
it(“and so is a spec”, function() {
a = true;
expect(a).toBe(true);
});
});

I would dig more into this sample code.
The test case typically comprises of:
Suite
Spec
Matcher
Custom Matcher
beforeEach
afterEach

Suite:
A test suite is a collection of test cases. The test suite does have a name (mentioned in the quotes). Below provided snippet is a typical jasmine test suite with 0 test cases.

describe(“This is a jasmine test suite”, function() {
});

Spec:
A test spec is a test specification for a unit. In jasmine, a spec starts with the keyword ‘it’ and has a spec message. Below provided snippet is a typical jasmine test spec not doing anything.

it(“This is a jasmine test spec”, function() {
});

Matcher:
Matcher is a check that lets you evaluate the output of a unit against a test result. Below provided snippet is a typical jasmine matcher.

expect(func()).toBe(0);

Let’s say, there is a function named func and it always returns a value 0. This function is a unit. If something goes awry with this function, the matcher will fail and hence, the unit test for the unit will fail.

Custom Matchers, beforeEach, afterEach:
Jasmine provides a number of inbuilt matchers. However, we can write our own custom matchers.

beforeEach lets a developer define something before a spec is executed.

An example would be to define a custom matcher in beforeEach and use the custom matcher in the spec. Below is a sample snippet:

beforeEach(function () {
this.addMatchers({
toBeEven: function () {
return (this.actual % 2) === 0;
}
});
});
it(‘should be even’, function () {
expect(isEvenNumber()).toBeEven();
});

afterEach lets a developer define something after a spec is executed.

How do I run tests:
Jasmine provides a spec runner where you may run your tests. Another option would be to use a test runner like karma.
As terminal is always fancier, a headless browser (like PhantomJS) should be preferred over Chrome.

Ready to embark on a transformative journey? Connect with our experts and fuel your growth today!