Angular testing – Jasmine – Error NG0303: Can’t bind to ‘formGroup’ since it isn’t a known property of ‘form’

Jasmine

Jasmine is a Behavior Driven Development testing framework for JavaScript. It does not rely on browsers, DOM, or any JavaScript framework. Thus it’s suited for websites, Node.js projects, or anywhere that JavaScript can run.

How to reproduce it

module.ts

import { ReactiveFormsModule } from '@angular/forms';

@NgModule({
  declarations: [
  ...
  ],
  imports: [
    ...
    ReactiveFormsModule
  ]
})

component.specs.ts

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [ ... ]
    })
      .compileComponents();

    fixture = TestBed.createComponent(...);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

Error message

Error: NG0303: Can't bind to 'formGroup' since it isn't a known property of 'form' (used in the 'Component' component template).
1. If 'form' is an Angular component and it has the 'formGroup' input, then verify that it is a part of an @NgModule where this component is declared.
2. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.

Solution

Import ReactiveFormsModule.

component.specs.ts

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [ ...],
      imports: [ ReactiveFormsModule ]
    })
      .compileComponents();

    fixture = TestBed.createComponent(...);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });
https://www.mldgroup.com

Vyštudovaný top manažér pôsobiaci najmä ako manažér, marketér, softvérový inžinier, konzultant, bloger, YouTuber a zatiaľ neúspešný hudobník a producent. V rámci praxe pôsobil v rôznych odvetviach na rôznych pozíciách v malých aj veľkých firmách, vrátane spoluprác a partnerstiev s významnými firmami či poradenskými spoločnosťami.

Pridaj komentár

Vaša e-mailová adresa nebude zverejnená. Vyžadované polia sú označené *