Skip to content

Latest commit

 

History

History
162 lines (124 loc) · 3.83 KB

README.md

File metadata and controls

162 lines (124 loc) · 3.83 KB

Jasmine API and Angular Testing Utils

Setup

  1. Install Jasmine. (https://jasmine.github.io/setup/nodejs.html)
  2. Init global vs local.
  3. jasmine init. this will generate /spec and jasmine config file.

Probable Sequence

  1. Globals and Matchers
  2. Spy (and, calls) and jasmine clock.
  3. Dom (native HTML/angular)
  4. tbd

Global

  1. afterAll
  2. afterEach
  3. beforeAll
  4. beforeEach
  5. describe
  6. expect
  7. fail
  8. fdescribe
  9. fit
  10. it
  11. pending
  12. spyOn
  13. spyOnProperty es6
  14. xdescribe
  15. xit

Matchers

  1. nothing
  2. toBe
  3. toBeCloseTo(expected, precisionopt)
  4. toBeDefined
  5. toBeFalsy
  6. toBeGreaterThan
  7. toBeGreaterThanOrEqual
  8. toBeLessThan
  9. toBeLessThanOrEqual
  10. toBeNaN
  11. toBeNegativeInfinity
  12. toBeNull
  13. toBePositiveInfinity
  14. toBeTruthy
  15. toBeUndefined
  16. toContain
  17. toEqual
  18. toHaveBeenCalled
  19. toHaveBeenCalledBefore.
  20. toHaveBeenCalledTimes.
  21. toHaveBeenCalledWith
  22. toMatch
  23. toThrow
  24. toThrowError

##Spy

  1. callSignature: spyOn(obj, methodName)
  2. returns: Spy
  1. callSignature: jasmine.createSpyObj(baseNameopt, methodNames)
  2. returns: Object
  1. callSignature: jasmine.createSpyObj(baseNameopt, methodNames)
  2. returns: Object
  1. callSignature: jasmine.createSpyObj(baseNameopt, methodNames)
  2. returns: Object

Spy Strategies

  1. identity.
  2. callFake.
  3. callThrough
  4. exec
  5. rejectWith
  6. resolveTo
  7. returnValue
  8. returnValues
  9. stub
  10. throwError

jasmine Async

  1. clock
  2. done

Using with testbed

{
    provide: UserService,
    useValue: SpyObject
}
  1. No resetting of spyObj by testbed.

##TestBed

  1. Enable auto change detection
{
    provide: ComponentFixtureAutoDetect,
    useValue: true
}
  1. ComponentFixtureAutoDetect service responds to asynchronous activities such as promise resolution, timers, and DOM events.

  2. Direct, synchronous update of the component property is invisible.

  3. Angular doesn't know that you set the input element's value property. It won't read that property until you raise the element's input event by calling dispatchEvent(). Then you call detectChanges().

  4. Always get the service from an injector

    Do not reference the stubfakeSpy object that's provided to the testing module in the body of your test. It does not work! The userService instance injected into the component is a completely different object, a clone of the provided stub fake Spy.

   {
       userService = fixture.debugElement.injector.get(UserService);
   }
  1. The fakeAsync() function enables a linear coding style by running the test body in a special fakeAsync test zone. The test body appears to be synchronous. There is no nested syntax (like a Promise.then()) to disrupt the flow of control.

  2. The tick() function is one of the Angular testing utilities that you import with TestBed. It's a companion to fakeAsync() and you can only call it within a fakeAsync() body.

  3. fakeAsync() is not needed jasmine.clock and throws an error if nested.

  4. By default fakeAsync() supports the following macroTasks.

    1. setTimeout
    2. setInterval
    3. requestAnimationFrame
    4. webkitRequestAnimationFrame
    5. mozRequestAnimationFrame
    6. Custom Microtask decleration.
      window['__zone_symbol__FakeAsyncTestMacroTask'] = [{
              source: 'HTMLCanvasElement.toBlob',
              callbackArgs: [{
                  size: 200
              }]
          }

      ];