Jasmine JavaScript Testing

This post enlighten by Jasmine JavaScript Testing

unit test

a piece of code tests a functionality of unit of application code.

Jasmine

Jasmine: behavior-driven development test framework

TDD vs. BDD

test-driven development turns into behavior-driven development by Dan North

BDD
  • Given: this provides an initial context
  • When: this defines the event occurs
  • Then: this ensures an outcome
Jasmine BDD example
1
2
3
4
5
6
describe("Player", function() {
describe("when song has been paused", function() {
it("should indicate that the song is paused", function() {
});
});
});
spec

each unit test call spec, short for specification

write a Jasmine test

describe function is a global Jasmine function, used to define test contest, it creates a new test suite(a collection of test cases), acceptes two params:

  • string: name of test suite
  • function: a function that will contains all its specs
1
describe("Investment", function() { });

it, another golbal Jasmine function, accepts two params:

  • string: title of the spec
  • function: a function that will contains the spec code
1
2
3
4
describe("Investment", function() {
it("should be of a stock", function() {
});
});

expect, an assertion or expectation is a comparison between two values, result of comparison is true means success, along with a matcher that indicates what comparison must be made with the values.

1
2
3
4
5
describe("Investment", function() {
it("should be of a stock", function() {
expect(investment.stock).toBe(stock);
});
});
Setup and teardown
  • beforeEach: setup function, runs from outside before every spec(it)
  • run spec(it)
  • afterEach: teardown function, runs from outside after every spec(it)
Write unit test in *Spec.js
β‡…
Debug and write more unit test