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

Rasshu

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?

sumod badchhape

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular 9 - Can't bind to 'formGroup' since it isn't a known property of 'form'

Angular 6: Can't bind to 'formGroup' since it isn't a known property of 'form'?

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

Angular 10 - can't bind to 'formGroup' since it isn't a known property of 'form'

Form builder angular Can't bind to 'formGroup' since it isn't a known property of 'form'

Angular 16 Standalone component, "Can't bind to 'formGroup' since it isn't a known property of 'form'"

Angular 4 Can't bind to 'formGroup' since it isn't a known property of 'form'

Can't bind to 'formGroup' since it isn't a known property of 'form' in Angular

Can't bind to 'formGroup' since it isn't a known property of 'form' in Angular 8

error NG8002: Can't bind to 'formGroup' since it isn't a known property of 'form'. In Angular 9

Angular error message: Can't bind to 'formGroup' since it isn't a known property of 'form'

Angular Ivy: Can't bind to 'formGroup' since it isn't a known property of 'form'. However ReactiveFormsModule and FormsModule is imported

Angular unit test error - Can't bind to 'formGroup' since it isn't a known property of 'form'

Angular 16 - can't bind to 'formGroup' since it isn't a known property of 'form'

Angular 15+ - Can't bind to 'formGroup' since it isn't a known property of 'form' even after adding in modules

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

Can't bind to 'FormGroup' since it isn't a known property of 'form' ----

Can't bind to 'FormGroup' since it isn't a known property of 'form'. ("

Can't bind to 'formGroup' since it isn't a known property of 'form' whilst using SharedModule

Can't bind to 'formGroup' since it isn't a known property of 'form'. ReactiveFormsModule already imported

Can't bind to 'formGroup' since it isn't a known property of 'form'. error in ionic v5.2.2

Template parse errors: Can't bind to 'formGroup' since it isn't a known property of 'form'

Can't bind to 'formGroup' since it isn't a known property of 'form' in ionic

Can't bind to 'formGroup' since it isn't a known property of 'form' for second component

error NG8002: Can't bind to 'formGroup' since it isn't a known property of 'form'

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

Testing transform in angular 7 unit test

Angular 7 testing with inheritance and global injector

Angular 7 testing public methods dependent on private ones

TOP Ranking

HotTag

Archive