Show Menu
Cheatography

Jasmine Test Cheat Sheet (DRAFT) by

Jasmine Test

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Jasmine test block

describe("A suite", function() {
  describe("Optional subsection", function() {
    beforeEach(function() {
      // runs before every it(...) in the block
    });

    afterEach(function() {
      // runs after every it(...) in the block
    });

    it("contains tests", function() {
      // assert using matchers
      expect(true).toBe(true);
    });
  });
});

Focused tests

ddescr­ibe­(block, function() { ... })
Run only tests in this block
iit(test, function() { ... })
Run only this test. Takes presedence over ddescr­ibe­(...)

Skipped tests

xdescribe( label, functi­on(){ ... })
skip block
xit( label, functi­on(){ ... })
skip test
 

Matchers

expect­(1).to­Be(1);
match with ===
expect­({a­:1}­).t­oEq­ual­({a­:1});
match simple literals and variables
expect­("foo bar"­).t­oMa­tch­(/bar/)
match regular expres­sions
expect­(fo­o).t­oB­eDe­fin­ed();
match against undefined
expect­(ba­r).t­oB­eUn­def­ined();
identical to expect­(ba­r).n­ot.to­BeD­efi­ned();
expect­(nu­ll).to­BeN­ull();
match against null
expect­(1).to­BeT­rut­hy();
expect­(0).to­BeF­alsy();
expect­(["f­oo", "­bar­"­]).t­oC­ont­ain­("ba­r");
match an item in an Array
expect­(1).to­BeL­ess­Tha­n(2);
expect­(2).to­BeG­rea­ter­Tha­n(1);
expect­(1).to­BeC­los­eTo­(1.5, 1);
expect(function).toTh­row();
expect(function() { throw 'error messag­e';­}).t­oT­hrow()