Cheat Sheet - Jasmine

We reuse many things during any application development. While working with Jasmine, generally there two sources, Jasmine Docs or Google. Even though we refer these resources, it consumes lot of time to get what we want i.e. right syntax and example.

To avoid doing this every time I have enlisted few of them as cheat sheet which can be used whenever required.


  1. Create spy object with spy methods
    mySpy = jasmine.createSpyObj('Contact', ['get','set']);
  2. Call fake method instead of real one.
    mySpy.pickContact.and.callFake(function () {
        return {
            id: 10
        };
    });
  3. Call a spy method and return a value
    var contact = {
            id: 10
    };
    mySpy.pickContact.and.returnValue(contact);
  4. Call a spy method and return a async value
    var contact = {
            id: 10
    };
    mySpy.pickContact.and.returnValue(asyncData(contact));
  5. Check call counts
    expect(mySpy.get.calls.count()).toBe(1, 'one call');
  6. Check most recent call
    expect(mySpy.get.calls.mostRecent().args[1]).toEqual('getErr');
  7. Prefer spies as they are usually the easiest way to mock services.
  8. fakeAsync()  with tick() →  No actual call from spec file
  9. async() with fixture.whenStable() → Actual call from spec file

Comments

Popular posts from this blog

Cheat Sheet - Git

Stack implementation using VanillaJS

JavaScript Best Practices