I need to unit test a component method that does not return a Promise, but internally calls a method in a service that does, and so unit test execution returns immediately to the 'it' block. This results in objects that I am supposed to look at for certain values to not yet be assigned. Angular documentation suggests that I use a Spy object. This seems to be exactly what I need. Unfortunately, even the most basic of unit tests won't let me create a Spy object.
The following code...
describe('my unit test', () => {
var myService;
beforeEach(() => {
myService = jasmine.createSpyObj('myService', ['myMethodOne', 'myMethodTwo']);
myService.myMethodOne();
});
it("my service test", () => {
expect(myService.myMethodOne).toBeDefined();
expect(myService.myMethodOne).toHaveBeenCalled();
});
});
Fails with...
>npm test
> iwp-app@0.0.0 test D:\Users\brock\Source
> jest
FAIL component.spec.ts
my unit test
× my service test (41ms)
● my unit test › my service test
TypeError: jasmine.createSpyObj is not a function
at component.spec.ts:29:25
at ZoneDelegate.Object.<anonymous>.ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:388:26)
at ProxyZoneSpec.Object.<anonymous>.ProxyZoneSpec.onInvoke (node_modules/zone.js/dist/proxy.js:128:39)
at ZoneDelegate.Object.<anonymous>.ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:387:32)
at Zone.Object.<anonymous>.Zone.run (node_modules/zone.js/dist/zone.js:138:43)
at Object.testBody.length (node_modules/jest-zone-patch/index.js:50:27)
● my unit test › my service test
TypeError: Cannot read property 'myMethodOne' of undefined
at create-template.component.spec.ts:34:22
at ZoneDelegate.Object.<anonymous>.ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:388:26)
at ProxyZoneSpec.Object.<anonymous>.ProxyZoneSpec.onInvoke (node_modules/zone.js/dist/proxy.js:128:39)
at ZoneDelegate.Object.<anonymous>.ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:387:32)
at Zone.Object.<anonymous>.Zone.run (node_modules/zone.js/dist/zone.js:138:43)
at Object.testBody.length (node_modules/jest-zone-patch/index.js:50:27)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 4.36s
Ran all test suites.
npm ERR! Test failed. See above for more details.