Skip to content

fixture.getDeferBlocks() returns empty array #62047

Closed

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

Activity

  1. added
    area: core
    core: defer
    on Jun 13, 2025
  2. added this to the needsTriage milestone on Jun 13, 2025
  3. JeanMeche commented on Jun 13, 2025

    @JeanMeche
    Member

    Note: This is also reproduceable with the ViewContrainerRef injected directly in the component.

  4. self-assigned this
    on Jun 20, 2025
  5. added a commit that references this issue on Jun 20, 2025
    9b08a8f
  6. added a commit that references this issue on Jun 23, 2025
    369f03a
  7. added a commit that references this issue on Jun 23, 2025
    3eee19d
  8. angular-automatic-lock-bot commented on Jul 24, 2025

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  9. locked and limited conversation to collaborators on Jul 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: corecore: deferstate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions