I am trying to create a generic wrapper around TestBed.createComponent
, which takes a type argument and creates the component for that type. However, the TestBed.createComponent
function takes an argument of type Type<T>
. I'm unable to create a Type<T>
from the passed in generic T argument.
export function createTestHarness<T>(): TestHarness<T> {
let component: T;
let fixture: ComponentFixture<T>;
fixture = TestBed.createComponent<T>(**PROBLEM AREA**);
component = fixture.componentInstance;
fixture.detectChanges();
return new TestHarness<T>(component, fixture);
}
Is there a way to derive a Type<T>
from the type passed in?
One option you have is to use the Type<T>
as a parameter to your function:
function createTestHarness<T>(type: Type<T>): TestHarness<T> {
let component: T;
let fixture: ComponentFixture<T>;
fixture = TestBed.createComponent<T>(type);
component = fixture.componentInstance;
fixture.detectChanges();
return new TestHarness<T>(component, fixture);
}
With the following usage:
const harness = createTestHarness(TestComponent);
Which will return a TestHarness<TestComponent>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With