This is my before each block for unit testing. It works great, making a new component instance available for each test. However, it takes approx 4 secs to execute the beforeEach block. When you've got 200 tests, that is slow!
I want to move the bulk of the code to a beforeAll block (which works if you use the Jasmine done()
callback instead of using async) but I still am unable to create a new instance for each test. Some tests fail because the sole instance has been tampered with by previous tests.
How do I ...
- Create a new component instance without executing this time consuming code for EVERY test.
or
- Reset the instance to its untampered with state.
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [AppModule],
providers: [{provide: APP_BASE_HREF, useValue: '/'},
MockBackend,
BaseRequestOptions,
{
provide: Http,
useFactory: (pBackend: MockBackend, pOptions: BaseRequestOptions) => {
return new Http(pBackend, pOptions);
},
deps: [MockBackend, BaseRequestOptions]
}]
}).compileComponents()
.then(() => {
fix = TestBed.createComponent(Route1DetailComponent);
instance = fix.componentInstance;
injector = fix.debugElement.injector;
});
}));