- npm install -g yo
- npm install -g bower
- npm install -g generator-jasmine
- yo jasmine // in project dir
- // so far it is web based test runner
- // install karma as automated test runner
- npm install -g karma
- npm install -g generator-karma
- yo karma —test-framework=jasmine // in project dir
- karma start // in test dir
function helloWorld() { return "Hello World"; } describe(“hello world”, function() { it(“say hello”, function() { expect(helloWorld().toEquals(“Hello World”); }); });
var foo = 1; expect(1).toBe(1); expect(false).not.toBe(true); expect(1).toEqual(1); expect("foo bar").toMatch(/bar/); expect("foo bar").toMatch("bar"); expect(foo).toBeDefined(); expect(foo.a).toBeUndefined(); expect(null).toBeNull(); expect(1).toBeTruthy(); expect(0).toBeFalsy(); expect([1,2]).toContain(1); expect(1).toBeLessThan(2); expect(1).toBeGreaterThan(0); expect(function(){a+1;}).toThrow();
describe("Group Spec", function() { it("spec 1", function() { expect(1).toBe(1); }); it("spec 2", function() { expect(1).not.toBe(2); }); });
var foo = 0; beforeEach(function() { foo += 1; }); afterEach(function() { foo = 0; });
var foo = 0; beforeAll(function() { foo = 1; }); afterAll(function() { foo = 0; });
describe("outer spec", function() { var foo = 1; it("outer expect", function() { expect(foo).toBe(1); }); describe("inner spec", function() { var bar = 2; it("inner expect", function() { expect(foo+bar).toBe(3); }); }); });
create spy:
spyOn(foo, 'setBar');
verify spied with tracking properties:
expect(foo.setBar).toHaveBeenCalled();
-- Types of spies
- and.callThrough
- and.returnValue
- and.callFake
- and.throwError
- and.stub
- jasmine.anything
- jasmine.objectContaining
- jasmine.arrayContaining
- jasmine.stringMatching
- asymmetricMatch
- jasmine.install
- jasmine.uninstall
- jasmine.tick