describe("Jasmine Cheatsheet", function() {
  it("is your Swiss Army knife!", function() {
    expect(helpYou).toUseJasmineLikeAPro();
  });

  describe("Credits", function () {
    it("is based on the Official Jasmine Documentation", function () {
      expected("http://jasmine.github.io/edge/introduction.html").shouldBeVisited();
    });
  });

  describe("Contribution", function () {
    it("is free to fork and contribute", function () {
      expected(forks).and.pullRequests();
    });
  });

  afterAll(function() {
    var author = "Lennon Jesus";
  });
});
Current Jasmine version:

npm version

Basic Structure

describe("A basic Jasmine structure", function() {

  var trueValue = false;

  beforeEach(function() {
    // initialization code...
    // mocks...
    // spies...

    trueValue = true;
  });

  it("should be true", function() {
      expect(trueValue).toBe(true);
  });

  afterEach(function() {
    // tear down...
    // good bye...
    // finish him...
    trueValue = false;
  });
});

Spies how to...

...create a spy

spyOn(obj, 'method');

jasmine.createSpy('optional name');

jasmine.createSpyObj('name', ['fnct1', 'fnct2', ..., 'fnctN']);

...modify the behavior of a spy

spyOn(obj, 'method').and.returnValue(val); // all calls to the function will return a specific value

spyOn(obj, 'method').and.returnValues(val1, val2); // all calls to the function will return specific values in order until it reaches the end of the return values list, at which point it will return undefined for all subsequent calls

spyOn(obj, 'method').and.callFake(fn); //all calls to the spy will delegate to the supplied function

spyOn(obj, 'method').and.callThrough(); // it will delegate to the actual implementation

spyOn(obj, 'method').and.throwError(err); //all calls to the spy will throw the specified value as an error

spyOn(obj, 'method').and.stub(); // the original stubbing behavior can be returned at any time with and.stub

...verify interactions

obj.method.calls.count(); //returns the number of times the spy was called

obj.method.calls.any(); //returns false if the spy has not been called at all, and then true once at least one call happens

obj.method.calls.reset(); // clears all tracking for a spy

...inspect calls

obj.method.calls.first(); // returns the context (the this) and arguments for the first call

obj.method.calls.mostRecent();

obj.method.calls.all();

...call description object

{
  object: {...},  // 'this' object
  args: []        // the arguments
}

Matchers

toBe

expect(obj).toBe(null);M

toEqual

expect(obj).toEqual({id: 7});

toMatch

expect(msg).toMatch(/abc/);

toBeDefined

expect(obj).toBeDefined();

toBeUndefined

expect(obj).toBeUndefined();

toBeTruthy

expect('a').toBeTruthy();

toBeFalsy

expect(obj).toBeFalsy();

toContain

expect(arr).toContain();

toBeLessThan

expect(21).toBeLessThan(42);

toBeGreaterThan

expect(42).toBeGreaterThan(21);

toBeCloseTo

expect(1.2).toBeCloseTo(1.23, 1);

toThrow

expect(fnct).toThrow();

toThrowError

expect(obj).toThrowError();

toBeNull

expect(obj).toBeNull();

not

expect(obj).not.toEqual({id: 7});
expect(msg).not.toMatch(/abc/);
expect(obj).not.toBeUndefined();
expect(obj).not.toBeNull();
expect(obj).not.xxx...