I'm working on an Angular 2-rc3 application and I have some unittests set up and they're working, yay! I just don't understand why they have to be written the way they do. And even more amazing, all the examples I see have the same approach. Specifically, these questions are on the top of my list:
Why is the
TestComponentBuilder
configured in every unittest?it('shows list of blog items by default', inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { return tcb .overrideProviders(BlogRoll, [provide(BlogService, { useValue: mockBlogService })]) .createAsync(BlogRoll) .then((fixture) => { // actual test code }); });
That's already seven lines of code extra per unittest and the readability of my code suffers a lot from this. I've tried placing this in a
beforeEach()
:beforeEach(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { console.log('beforeEach'); return tcb.overrideProviders(BlogRoll, [provide(BlogService, { useValue: mockBlogService })]) .createAsync(BlogRoll) .then(fixture => { // this never gets printed console.log('in then:', fixture.componentInstance); }); }));
but Karma doesn't seem to be able to handle the asyncness, everything in the
then
just doesn't get executed. Is this a bug or is this intended, are we not supposed to do it like this?Why does the creation of this component need to happen asynchronous? There's a
createSync()
on the TestComponentBuilder class, can't we use that? Of course I tried it and found out that the function signatures differ:createAsync(rootComponentType: Type) : Promise<ComponentFixture<any>>
andcreateSync(componentFactory: ComponentFactory<C>) : ComponentFixture<C>
. Why do we need a component factory here, why don't we need it when creating the component async? // Update: RC4 is out andcreateSync()
now accepts a Type. Great.
My sanity thanks you already!