Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fixture.getDeferBlocks() returns empty array #62047

Open
@tehshin

Description

@tehshin

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

Yes

Description

There is an existing issue open for ng-mocks regarding testing of defer. See help-me-mom/ng-mocks#7742

The problem is that getDeferBlocks returns an empty array when the ViewContainerRef is injected before calling it.

Here is a unit test that reproduces the issue:

import { Component, ViewContainerRef } from '@angular/core';
import {
  DeferBlockBehavior,
  DeferBlockState,
  TestBed,
} from '@angular/core/testing';

@Component({
  selector: 'large-component',
  template: `I'm so large!`,
})
class LargeComponent {}

@Component({
  imports: [LargeComponent],
  template: `
    @defer {
    <large-component />
    } @placeholder { Placeholder } @loading { Loading... }
  `,
})
class DeferTestComponent {}

describe('defer bug', () => {
  it('should render a defer block in different states', async () => {
    TestBed.configureTestingModule({
      deferBlockBehavior: DeferBlockBehavior.Manual,
    });
    const fixture = TestBed.createComponent(DeferTestComponent);
    await fixture.whenStable();

    // having this here will cause getDeferBlocks to return []
    // doing it after getDeferBlocks has no negative side-effects
    fixture.debugElement.injector.get(ViewContainerRef);

    const deferBlocks = await fixture.getDeferBlocks();
    const deferBlockFixture = deferBlocks[0];

    // Renders placeholder state by default.
    expect(fixture.nativeElement.innerHTML).toContain('Placeholder');
    // Render loading state and verify rendered output.
    await deferBlockFixture.render(DeferBlockState.Loading);
    expect(fixture.nativeElement.innerHTML).toContain('Loading');

    // Render final state and verify the output.
    await deferBlockFixture.render(DeferBlockState.Complete);

    expect(fixture.nativeElement.innerHTML).toContain("I'm so large!");
  });
});

I took the example from the Angular documentation and added the line to access the ViewContainerRef.

The unit test will fail with:

TypeError: Cannot read properties of undefined (reading 'render')

I am very interested how injecting a service is affecting the rendering process. I have tested this on Angular 18+.

cheers.

Please provide a link to a minimal reproduction of the bug

see description

Please provide the exception or error you saw


Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 20.0.2
Node: 22.14.0
Package Manager: npm 10.9.2
OS: win32 x64

Angular: 20.0.3
... common, compiler, compiler-cli, core, forms
... platform-browser, router

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.2000.2
@angular-devkit/core         20.0.2
@angular-devkit/schematics   20.0.2
@angular/build               20.0.2
@angular/cli                 20.0.2
@schematics/angular          20.0.2
rxjs                         7.8.2
typescript                   5.8.3
zone.js                      0.15.1

Anything else?

No response

Metadata

Metadata

Assignees

Labels

area: coreIssues related to the framework runtimecore: deferIssues related to @defer blocks.state: has PR

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions