Skip to content

Commit dc3131c

Browse files
atscottmattrbeck
authored andcommitted
feat(core): TestBed.getFixture -> TestBed.getLastFixture and update implementation
TestBed.getFixture is now TestBed.getLastFixture and returns the last fixture created rather than throwing if there are more than 1.
1 parent 4874c54 commit dc3131c

File tree

4 files changed

+14
-23
lines changed

4 files changed

+14
-23
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export interface TestBed {
120120
execute(tokens: any[], fn: Function, context?: any): any;
121121
// @deprecated
122122
flushEffects(): void;
123-
getFixture<T = unknown>(): ComponentFixture<T>;
123+
getLastFixture<T = unknown>(): ComponentFixture<T>;
124124
initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, options?: TestEnvironmentOptions): void;
125125
// (undocumented)
126126
inject<T>(token: ProviderToken<T>, notFoundValue: undefined, options: InjectOptions & {

packages/core/testing/src/test_bed.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export interface TestBed {
175175
* Returns the most recently created `ComponentFixture`, or throws an error if one has not
176176
* yet been created.
177177
*/
178-
getFixture<T = unknown>(): ComponentFixture<T>;
178+
getLastFixture<T = unknown>(): ComponentFixture<T>;
179179

180180
/**
181181
* Execute any pending effects.
@@ -420,8 +420,8 @@ export class TestBedImpl implements TestBed {
420420
return TestBedImpl.INSTANCE.createComponent(component, options);
421421
}
422422

423-
static getFixture<T = unknown>(): ComponentFixture<T> {
424-
return TestBedImpl.INSTANCE.getFixture();
423+
static getLastFixture<T = unknown>(): ComponentFixture<T> {
424+
return TestBedImpl.INSTANCE.getLastFixture();
425425
}
426426

427427
static resetTestingModule(): TestBed {
@@ -719,17 +719,11 @@ export class TestBedImpl implements TestBed {
719719
return fixture;
720720
}
721721

722-
getFixture<T = unknown>(): ComponentFixture<T> {
722+
getLastFixture<T = unknown>(): ComponentFixture<T> {
723723
if (this._activeFixtures.length === 0) {
724724
throw new Error('No fixture has been created yet.');
725725
}
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];
726+
return this._activeFixtures[this._activeFixtures.length - 1];
733727
}
734728

735729
/**

packages/platform-browser/test/testing_public_spec.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,29 +1104,26 @@ Did you run and wait for 'resolveComponentResources()'?`);
11041104
expect(componentFixture.nativeElement).toHaveText('injected value: mocked out value');
11051105
}));
11061106

1107-
describe('getFixture', () => {
1107+
describe('getLastFixture', () => {
11081108
it('should return the last created fixture', () => {
11091109
const fixture = TestBed.createComponent(ChildComp);
1110-
expect(TestBed.getFixture()).toBe(fixture);
1110+
expect(TestBed.getLastFixture()).toBe(fixture);
11111111
});
11121112

11131113
it('should throw if no fixture has been created', () => {
1114-
expect(() => TestBed.getFixture()).toThrowError('No fixture has been created yet.');
1114+
expect(() => TestBed.getLastFixture()).toThrowError('No fixture has been created yet.');
11151115
});
11161116

1117-
it('should throw an error if multiple fixtures are present', () => {
1117+
it('should return the last fixture when multiple fixtures are present', () => {
11181118
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-
);
1119+
const parentFixture = TestBed.createComponent(ParentComp);
1120+
expect(TestBed.getLastFixture()).toBe(parentFixture);
11241121
});
11251122

11261123
it('should clear the fixture after reset', () => {
11271124
TestBed.createComponent(ChildComp);
11281125
TestBed.resetTestingModule();
1129-
expect(() => TestBed.getFixture()).toThrowError('No fixture has been created yet.');
1126+
expect(() => TestBed.getLastFixture()).toThrowError('No fixture has been created yet.');
11301127
});
11311128
});
11321129
});

packages/private/testing/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export async function expectText(
245245
text: string | RegExp,
246246
options: ExpectTextOptions = {},
247247
): Promise<void> {
248-
const container = options.container || TestBed.getFixture().nativeElement;
248+
const container = options.container || TestBed.getLastFixture().nativeElement;
249249
await waitFor(() => {
250250
const content = container.textContent || '';
251251
if (typeof text === 'string') {

0 commit comments

Comments
 (0)