Recently I had a problem where in an Angular unit test where I had an object that was being initialized in ngOnInit from a service. As far as I could tell, everything should be working, but the test would fail because I was trying to set a property on the object before it had finished initializing.
Inventory component and service
Since the “ItemCache” object is being initialized inside of the call back of a promise, we have to wait for that to finish before we can access it.
Test Spec
In the unit test setup, I just needed to create a second beforeEach to set the property.
It only took me most of two days to figure out….