Łukasz Kazimierz Bandzarewicz

@lucassus

Jasmine Cheat Sheet

BDD for JavaScript

Jasmine is a behavior-driven development framework for testing your JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests.

Go to: Builtin matchers | Creating custom matchers

Builtin matchers

1
2
3
4
toBeTruthy(); toBeFalsy(); toBeDefined(); toBeNull();
toEqual(); toBeCloseTo(); toContain(); toMatch();
toBeGreaterThan(); toBeLessThan();
toThrow();
(matchers.js) download
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
describe('built-in matchers', function() {

  describe('toBeTruthy', function() {
    it('passes if subject is true', function() {
      expect(true).toBeTruthy();
      expect(false).not.toBeTruthy();
    });
  });

  describe('toBeFalsy', function() {
    it('passes if subject is false', function() {
      expect(false).toBeFalsy();
      expect(true).not.toBeFalsy();
    });
  });

  describe('toBeDefined', function() {
    it('passes if subject is not undefined', function() {
      expect({}).toBeDefined();
      expect(undefined).not.toBeDefined();
    });
  });

  describe('toBeNull', function() {
    it('passes if subject is null', function() {
      expect(null).toBeNull();
      expect(undefined).not.toBeNull();
      expect({}).not.toBeNull();
    });
  });

  describe('toEqual', function() {
    it('passes if subject and expectation are equivalent', function() {
      expect('Hello World!').toEqual('Hello World!');
      expect('Hello World!').not.toEqual('Goodbye!');
      expect('Hello World!').toNotEqual('Hi!');
      expect([1, 2, 3]).toEqual([1, 2, 3]);
      expect(1).toEqual(1);
      expect({ foo: 1 }).toEqual({ foo: 1 });
    });
  });

  describe('toBeCloseTo', function() {
    it('checks that the expected item is equal to the actual item up to a given level of decimal precision ', function() {
      expect(1.223).toBeCloseTo(1.22);
      expect(1.233).not.toBeCloseTo(1.22);
      expect(1.23326).toBeCloseTo(1.23324, 3);
    });
  });

  describe('toContain', function() {
    it('passes if the expected item is an element in the actual array', function() {
      expect([1, 2, 3]).toContain(2);
      expect([1, 2, 3]).not.toContain(4);
    });
  });

  describe('toMatch', function() {
    it('compares the actual to the expected using a regular expression', function() {
      expect('Hello Jasmine').toMatch(/jasmine/i);
      expect('phone: 123-45-67').toMatch(/\d{3}-\d{2}-\d{2}/);
    });
  });

  describe('toBeGreaterThan', function() {
    it('passes if the actual value is greater than the expected value', function() {
      expect(2).toBeGreaterThan(1);
    });
  });

  describe('toBeLessThan', function() {
    it('passes if the actual value is less than the expected value', function() {
      expect(2).toBeLessThan(3);
    });
  });

  describe('toThrow', function() {
    it('checks that the expected exception was thrown by the actua', function() {
      var object = {
        doSomething: function() {
          throw new Error("Unexpected error!")
        }
      };
      expect(object.doSomething).toThrow(new Error("Unexpected error!"));
    });
  });
});

Try this in jsfiddle

Creating custom matchers

Custom matchers help to document the intent of your specs, and can help to remove code duplication in your specs.

It’s extremely easy to create new matchers for your app. A matcher function receives the actual value as this.actual, and zero or more arguments may be passed in the function call. The function should return true if the actual value passes the matcher’s requirements, and false if it does not.

(custom-matchers.js) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
beforeEach(function() {
  this.addMatchers({
    toBeGET: function() {
      var actual = this.actual.method;
      return actual === 'GET';
    },
    toHaveUrl: function(expected) {
      var actual = this.actual.url;
      this.message = function() {
        return "Expected request to have url " + expected + " but was " + actual
      };
      return actual === expected;
    }
  });
});
(custom-matchers-usage.js) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
describe('request', function() {
  var request = { method: 'GET', url: '/products.json' };

  it('should be GET', function() {
    expect(request).toBeGET();
  });

  it('should have url /products.json', function() {
    expect(request).toHaveUrl('/products.json');
  });

  // this will fail with nice message:
  // "Expected request to have url /tasks.json but was /projects.json"
  it('should have url /tasks.json', function() {
    expect(request).toHaveUrl('/tasks.json');
  });
});

Try is in jsfiddle

Comments