Unit Test with Jasmine - Tuong-Nguyen/JavaScript-Structure GitHub Wiki

Jasmine is the testing framework used by Angular team. Therefore, it would be good to use it for testing our Angular project.

Jasmine

Structure

describe
beforeAll/afterAll beforeEach/afterEach it
expect

Pending/Focus

  • xit

  • pending

  • fit

  • fdescribe

Matcher

expect(actual).toBe(matcher);
  • String
  • Existence
  • Object
  • Array
  • Custom

Spy

  • Track function invocation
  • Parameter lists for all invocations
  • Can call through
  • Can call another function
  • Can return arbitrary value
  • Can throw Error

Mock the clock and Date

  • setTimeout - setInterval
  • Set Date

Asynchronous

  • Default timeout = 5 seconds
  • Done

Setup Jasmine

Server (Node)

Install

npm install jasmine --save

Create configuration file

"node_modules/.bin/jasmine" init

Run

Add a task test into package.json

"scripts": {
    "test": "jasmine"
  }

Run the tests with command: npm test

Combine with compile script for compiling typescript files:

"scripts": {
    "compile": "tsc -p .",
    "test": "jasmine"
  }