- Install Jasmine. (https://jasmine.github.io/setup/nodejs.html)
- Init global vs local.
- jasmine init. this will generate /spec and jasmine config file.
- Globals and Matchers
- Spy (and, calls) and jasmine clock.
- Dom (native HTML/angular)
- tbd
- afterAll
- afterEach
- beforeAll
- beforeEach
- describe
- expect
- fail
- fdescribe
- fit
- it
- pending
- spyOn
- spyOnProperty es6
- xdescribe
- xit
- nothing
- toBe
- toBeCloseTo(expected, precisionopt)
- toBeDefined
- toBeFalsy
- toBeGreaterThan
- toBeGreaterThanOrEqual
- toBeLessThan
- toBeLessThanOrEqual
- toBeNaN
- toBeNegativeInfinity
- toBeNull
- toBePositiveInfinity
- toBeTruthy
- toBeUndefined
- toContain
- toEqual
- toHaveBeenCalled
- toHaveBeenCalledBefore.
- toHaveBeenCalledTimes.
- toHaveBeenCalledWith
- toMatch
- toThrow
- toThrowError
##Spy
- callSignature: spyOn(obj, methodName)
- returns: Spy
spyOnProperty (https://jasmine.github.io/api/3.4/global.html#spyOnProperty)
- callSignature: jasmine.createSpyObj(baseNameopt, methodNames)
- returns: Object
jasmine.createSpy (https://jasmine.github.io/api/3.4/jasmine.html#.createSpy)
- callSignature: jasmine.createSpyObj(baseNameopt, methodNames)
- returns: Object
jasmine.createSpyObj (https://jasmine.github.io/api/3.4/jasmine.html#.createSpyObj)
- callSignature: jasmine.createSpyObj(baseNameopt, methodNames)
- returns: Object
- identity.
- callFake.
- callThrough
- exec
- rejectWith
- resolveTo
- returnValue
- returnValues
- stub
- throwError
- clock
- done
{
provide: UserService,
useValue: SpyObject
}
- No resetting of spyObj by testbed.
##TestBed
- Enable auto change detection
{
provide: ComponentFixtureAutoDetect,
useValue: true
}
-
ComponentFixtureAutoDetect service responds to asynchronous activities such as promise resolution, timers, and DOM events.
-
Direct, synchronous update of the component property is invisible.
-
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().
-
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);
}
-
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.
-
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.
-
fakeAsync() is not needed jasmine.clock and throws an error if nested.
-
By default fakeAsync() supports the following macroTasks.
- setTimeout
- setInterval
- requestAnimationFrame
- webkitRequestAnimationFrame
- mozRequestAnimationFrame
- Custom Microtask decleration.
window['__zone_symbol__FakeAsyncTestMacroTask'] = [{
source: 'HTMLCanvasElement.toBlob',
callbackArgs: [{
size: 200
}]
}
];