Skip to content

FastCompile throws NG0951 for viewChild.required #2344

@ThiloAschebrock

Description

@ThiloAschebrock

Please provide the environment you discovered this bug in.

Below is a minimal example of a test setup that throws NG0951 when fastCompile: true is enabled:

import {
  Component,
  ElementRef,
  computed,
  viewChild,
} from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { expect, it } from 'vitest';

@Component({
  selector: 'app-test-component',
  template: `
    <div #trigger>origin</div>
    <p>width: {{ panelWidth() }}</p>
  `,
})
class TestComponent {
  readonly trigger = viewChild.required<ElementRef<HTMLElement>>('trigger');

  readonly clientWidth = computed(
    () => this.trigger().nativeElement.clientWidth,
  );
}
 
it('should create the app', () => {
  const fixture = TestBed.createComponent(TestComponent);
  const app = fixture.componentInstance;
  expect(app).toBeTruthy();
 });

Which area/package is the issue in?

vite-plugin-angular

Description

Tests that are passing with fastCompile: false and using viewChild.required are throwing an NG0951 error.

Please provide the exception or error you saw

Error: NG0951: Child query result is required but not available. Find more at https://v21.angular.dev/errors/NG0951

Other information

I cannot reproduce the error in Stackblitz. I am posting it here anyway understanding that Stackblitz behaves differently due to its restricted environment.

I would be willing to submit a PR to fix this issue

  • Yes
  • No

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions