Rasshu
Rasshu

Reputation: 1799

Angular 7 testing: "Can't bind to formGroup since it isn't a known property of form"

The console logs error:

15 02 2019 14:50:24.868:INFO [Chrome 72.0.3626 (Windows 10.0.0)]: Connected on socket BiEioS9fHwq-QLg3AAAA with id 27946068 Chrome 72.0.3626 (Windows 10.0.0) LoginComponent should create FAILED Can't bind to 'formGroup' since it isn't a known property of 'form'. ("

        <div class="row">

(etc)

I'm running it using command ng test. My spec file:

describe('LoginComponent', () => {

    let component: LoginComponent;
    let fixture: ComponentFixture<LoginComponent>;

    const fakeActivatedRoute = {
        snapshot: { data: {} }
    } as ActivatedRoute;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [
                LoginComponent
            ],
            imports: [
                RouterTestingModule,
                HttpClientModule,
                CommonModule,
                BrowserModule,
                BrowserAnimationsModule,
                ReactiveFormsModule,
                MessageModule,
                MatFormFieldModule,
                MatInputModule,
                MatButtonModule,
                MatCheckboxModule,
                MatProgressSpinnerModule,
                MatRadioModule,
                MatSliderModule,
                NgbModule
            ],
            providers: [
                {
                    provide: ActivatedRoute,
                    useValue: fakeActivatedRoute
                }
            ]
        })
            .compileComponents();
    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(LoginComponent);
        component = fixture.debugElement.componentInstance;
        fixture.detectChanges();
    });

    it('should create', () => {
        expect(component).toBeTruthy();
    });

});

And the LoginModule file:

@NgModule({
  declarations: [LoginComponent],
  imports: [
    CommonModule,
    BrowserModule,
    BrowserAnimationsModule,
    FormsModule,
    ReactiveFormsModule,
    MatFormFieldModule,
    MatInputModule,
    MatButtonModule,
    MatCheckboxModule,
    MatProgressSpinnerModule,
    MatRadioModule,
    MatSliderModule,
    MessageModule,
    NgbModule.forRoot()
  ],
  providers: [],
  bootstrap: [
    LoginComponent,
    MessageComponent
  ]
})
export class LoginModule {}

What's missing?

Upvotes: 16

Views: 12374

Answers (4)

Eliezer Pe&#241;a
Eliezer Pe&#241;a

Reputation: 1

I had the same problem, solved it by importing ReactiveFormModule in configureTestingModule and removed the word 'export' from my testing host component. like:

@Component({
    template: '<form [formGroup]="myTestForm"><input formControlName="test"></form>'
})
export class HostComponent {...}

to


@Component({
    template: '<form [formGroup]="myTestForm"><input formControlName="test"></form>'
})
class HostComponent {...}

Upvotes: 0

sumod badchhape
sumod badchhape

Reputation: 964

It depends on which type of form you are using in your project. Angular provides Template-driven forms and Reactive forms. If you are using Reactive forms then, You need to import ReactiveFormsModule in your componentName.spec.ts file as

import { ReactiveFormsModule } from '@angular/forms';
beforeEach(async(() => {
TestBed.configureTestingModule({
  declarations: [ ContactUsFormComponent ],
  imports: [ReactiveFormsModule]
})
.compileComponents();}));

Otherwise, if you are using Template driven forms then, you need to import FormsModule.

Upvotes: 24

Ivan
Ivan

Reputation: 32

I think that it´s the

FormsModule

and the

.forRoot()

in the NgbModule in your testing imports.

Upvotes: 0

Rene O
Rene O

Reputation: 11

Similar to Angular2 : Can't bind to 'formGroup' since it isn't a known property of 'form'

I think you are missing to import FormsModule in your configureTestingModule

Upvotes: 0

Related Questions