WARNING

Written back in mid June of 2014

Jasmine is a popular Open Source BDD testing framework that supports both client-side and server-side testing.

# Setup

The easiest way to get started is to download a standalone Jasmine release, unzip it, copy into your project and start testing. It contains a SpecRunner.html that you open for running your tests. For automated test running you can use your favorite IDE or Karma

# Describe Blocks

describe('User', function() {
    /* tests */
});
1
2
3
describe('User', function() {
    describe('when new', function() {
        /* tests */
    });
});
1
2
3
4
5

# Writing Tests

  • it()
  • beforeEach & afterEach
  • Matchers & Custom Matchers
describe('User', function() {
    var sut;

    beforeEach(function() {
        sut = new SUT();
    });

    afterEach(function() {
        /* cleanup code */
    });

    it('should be able to play a song', function() {
        /* code and assertions */
    });
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# Matchers

  • expect(x).toEqual(y);
  • expect(x).toBe(y); // ===
  • expect(x).toMatch(pattern); //regex
  • expect(x).toBeDefined();
  • expect(x).toBeUndefined();
  • expect(x).toBeNull();
  • expect(x).toBeTruthy();
  • expect(x).toBeFalsy();
  • expect(x).toContain(y);
  • expect(x).toBeLessThan(y);
  • expect(x).toBeGreaterThan(y);
  • expect(function() {fn();}).toThrow(ex);

You can negate these matchers by expressing expect(x).not.toBe...

# Custom Matchers

beforeEach(function() {
    this.addMatchers({
        toBeFive: function() {
            return this.actual === 5;
        }
    });
});
1
2
3
4
5
6
7

Example:

var Calculator = function() {

};

Calculator.prototype.add = function (a, b) {
	return a + b;
}

Calculator.prototype.divide = function (a, b) {
	return a / b;
}
1
2
3
4
5
6
7
8
9
10
11
describe("Calculator", function () {
	var calc;

	beforeEach(function () {
		calc = new Calculator();

		this.addMatchers({
			toBeBetween: function(a, b) {
				return this.actual >= a && this.actual <= b;
			}
		})
	});

	it("should be able to add 1 and 1", function () {
		expect(calc.add(1, 1)).toBe(2);
	});

	it("should be able to divide 6 and 2", function () {
		expect(calc.divide(6, 2)).toBe(3);
	});

	it("should be able to divide a rational number", function () {
		//expect(calc.divide(1, 3)).toBeGreaterThan(0.3);
		//expect(calc.divide(1, 3)).toBeLessThan(0.4);
		expect(calc.divide(1, 3)).toBeBetween(0.3, 0.34);
	});
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# Running Tests

  • Runs in the browser
  • Passed and skipped tests are hidden by default
  • Tests can be filtered by describe block or individual test
  • To skip a test add an x in front of the describe- or it declaration
xit('should be skipped', function () {
    /* this test should be skipped */
});
1
2
3
xdescribe('MyClass', function () {
    /* all tests inside are skipped */
});
1
2
3

# Jasmine Spies

  • Mostle represents a subset of the functionality provided by sinon.
  • Built for mocking single functions
  • Supports both replacement and pass-through mocking
  • Has many built in matchers
Updated: 1/14/2019, 6:58:59 PM