Skip to content

Commit 246a984

Browse files
atscottthePunderWoman
authored andcommitted
feat(core): add TestBed.getFixture
This adds a new method to the TestBed to retrieve the most recently created component fixture. This is useful for cases where the fixture is created in a beforeEach and needs to be accessed in a test. If multiple fixtures have been created, this method will throw an error to prevent depending on fixture creation order.
1 parent 0d652ba commit 246a984

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

goldens/public-api/core/testing/index.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export interface TestBed {
119119
execute(tokens: any[], fn: Function, context?: any): any;
120120
// @deprecated
121121
flushEffects(): void;
122+
getFixture<T = unknown>(): ComponentFixture<T>;
122123
initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, options?: TestEnvironmentOptions): void;
123124
// (undocumented)
124125
inject<T>(token: ProviderToken<T>, notFoundValue: undefined, options: InjectOptions & {

packages/core/testing/src/test_bed.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ export interface TestBed {
171171

172172
createComponent<T>(component: Type<T>, options?: TestComponentOptions): ComponentFixture<T>;
173173

174+
/**
175+
* Returns the most recently created `ComponentFixture`, or throws an error if one has not
176+
* yet been created.
177+
*/
178+
getFixture<T = unknown>(): ComponentFixture<T>;
179+
174180
/**
175181
* Execute any pending effects.
176182
*
@@ -414,6 +420,10 @@ export class TestBedImpl implements TestBed {
414420
return TestBedImpl.INSTANCE.createComponent(component, options);
415421
}
416422

423+
static getFixture<T = unknown>(): ComponentFixture<T> {
424+
return TestBedImpl.INSTANCE.getFixture();
425+
}
426+
417427
static resetTestingModule(): TestBed {
418428
return TestBedImpl.INSTANCE.resetTestingModule();
419429
}
@@ -709,6 +719,19 @@ export class TestBedImpl implements TestBed {
709719
return fixture;
710720
}
711721

722+
getFixture<T = unknown>(): ComponentFixture<T> {
723+
if (this._activeFixtures.length === 0) {
724+
throw new Error('No fixture has been created yet.');
725+
}
726+
if (this._activeFixtures.length > 1) {
727+
throw new Error(
728+
`More than one component fixture has been created. Use \`TestBed.createComponent\` ` +
729+
`and store the fixture on the test context, rather than using \`TestBed.getFixture\`.`,
730+
);
731+
}
732+
return this._activeFixtures[0];
733+
}
734+
712735
/**
713736
* @internal strip this from published d.ts files due to
714737
* https://github.com/microsoft/TypeScript/issues/36216

packages/platform-browser/test/testing_public_spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,32 @@ Did you run and wait for 'resolveComponentResources()'?`);
11031103
componentFixture.detectChanges();
11041104
expect(componentFixture.nativeElement).toHaveText('injected value: mocked out value');
11051105
}));
1106+
1107+
describe('getFixture', () => {
1108+
it('should return the last created fixture', () => {
1109+
const fixture = TestBed.createComponent(ChildComp);
1110+
expect(TestBed.getFixture()).toBe(fixture);
1111+
});
1112+
1113+
it('should throw if no fixture has been created', () => {
1114+
expect(() => TestBed.getFixture()).toThrowError('No fixture has been created yet.');
1115+
});
1116+
1117+
it('should throw an error if multiple fixtures are present', () => {
1118+
TestBed.createComponent(ChildComp);
1119+
TestBed.createComponent(ParentComp);
1120+
expect(() => TestBed.getFixture()).toThrowError(
1121+
`More than one component fixture has been created. Use \`TestBed.createComponent\` ` +
1122+
`and store the fixture on the test context, rather than using \`TestBed.getFixture\`.`,
1123+
);
1124+
});
1125+
1126+
it('should clear the fixture after reset', () => {
1127+
TestBed.createComponent(ChildComp);
1128+
TestBed.resetTestingModule();
1129+
expect(() => TestBed.getFixture()).toThrowError('No fixture has been created yet.');
1130+
});
1131+
});
11061132
});
11071133
describe('using alternate components', () => {
11081134
beforeEach(() => {

0 commit comments

Comments
 (0)