Skip to content

Commit e5c9f7a

Browse files
crisbetoAndrewKushnir
authored andcommitted
fix(ivy): provide NgZone to ComponentRef (angular#26898)
Fixes the r3 `TestBed` not providing an `NgZone` to the `ComponentFixture`. PR Close angular#26898
1 parent 9729e8f commit e5c9f7a

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

packages/core/testing/src/r3_test_bed.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {ComponentFixture} from './component_fixture';
1212
import {MetadataOverride} from './metadata_override';
1313
import {ComponentResolver, DirectiveResolver, NgModuleResolver, PipeResolver, Resolver} from './resolvers';
1414
import {TestBed} from './test_bed';
15-
import {ComponentFixtureAutoDetect, TestBedStatic, TestComponentRenderer, TestModuleMetadata} from './test_bed_common';
15+
import {ComponentFixtureAutoDetect, ComponentFixtureNoNgZone, TestBedStatic, TestComponentRenderer, TestModuleMetadata} from './test_bed_common';
1616

1717
let _nextRootElementId = 0;
1818

@@ -357,11 +357,16 @@ export class TestBedRender3 implements Injector, TestBed {
357357
`It looks like '${stringify(type)}' has not been IVY compiled - it has no 'ngComponentDef' field`);
358358
}
359359

360-
const componentFactory = new ComponentFactory(componentDef);
361-
const componentRef =
362-
componentFactory.create(Injector.NULL, [], `#${rootElId}`, this._moduleRef);
360+
const noNgZone: boolean = this.get(ComponentFixtureNoNgZone, false);
363361
const autoDetect: boolean = this.get(ComponentFixtureAutoDetect, false);
364-
const fixture = new ComponentFixture<any>(componentRef, this.get(NgZone), autoDetect);
362+
const ngZone: NgZone = noNgZone ? null : this.get(NgZone, null);
363+
const componentFactory = new ComponentFactory(componentDef);
364+
const initComponent = () => {
365+
const componentRef =
366+
componentFactory.create(Injector.NULL, [], `#${rootElId}`, this._moduleRef);
367+
return new ComponentFixture<any>(componentRef, ngZone, autoDetect);
368+
};
369+
const fixture = ngZone ? ngZone.run(initComponent) : initComponent();
365370
this._activeFixtures.push(fixture);
366371
return fixture;
367372
}

0 commit comments

Comments
 (0)