Search code examples
angularangular6angular-test

ng test gives Component should create


I have written my first angular application in Angular 6.

I have not written any test yet but there are some default test files created automatically while generating components and services.

When I run the auto-generated tests using

ng test

It gives too many errors. One error out of them is like

ChangeAvatarModalComponent should create

Failed: Template parse errors:
There is no directive with "exportAs" set to "ngForm" ("
<div class="modal-body">

  <form [formGroup]="changeAvatarForm" id="avatar-form" [ERROR ->]#formDir="ngForm" (submit)="onSubmit()">
  <div class="row">
    <div class="col-md-12">
"): ng:///DynamicTestModule/ChangeAvatarModalComponent.html@8:56
Can't bind to 'formGroup' since it isn't a known property of 'form'. ("
<div class="modal-body">

I have an account module which have ChangeAvatarModalComponent.

I have following lines inside account.module.ts

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    RouterModule.forChild(AccountRoutes),
    NgbModule
  ],
  declarations: [
    ChangeAvatarModalComponent
  ],
  entryComponents: [
    ChangeAvatarModalComponent
  ]
})
export class AccountModule { }

and also FormsModule and ReactiveFormsModule are imported in app.module.ts

There are many such errors in the log generated.

Edit 2: change-avatar-modal.component.spec.ts

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { ChangeAvatarModalComponent } from './change-avatar-modal.component';

describe('ChangeAvatarModalComponent', () => {
  let component: ChangeAvatarModalComponent;
  let fixture: ComponentFixture<ChangeAvatarModalComponent>;

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

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

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

Solution

  • You are not showing you .spec.ts file in the code you have given.

    The reason you are getting the problems with forms is because in your spec file you need to import the relevant modules too like this:

    describe('ExampleComponent', () => {
      let component: ExampleComponent
      let fixture: ComponentFixture<ExampleComponent>
    
      beforeEach(async(() => {
        TestBed.configureTestingModule({
          imports: [
            RouterTestingModule,
            TranslateModule.forRoot({
              loader: {provide: TranslateLoader, useClass: TranslateFakeLoader}
            }),
            HttpClientModule,
            HttpClientTestingModule,
            FormsModule,
            SfFormModule,
            ReactiveFormsModule,
            NguiAutoCompleteModule,
            NgxMyDatePickerModule,
            NgxPermissionsModule.forRoot(),
            PipeModule,
            StoreModule.forRoot({}),
            LayoutsModule
          ],
          declarations: [
            ExampleComponent
          ],
          providers: [
            {provide: APP_BASE_HREF, useValue: '/'},
            {provide: ToastrService, MockToastrService},
            ActionsSubject,
            SimService
          ]
        }).compileComponents()
      }))
    
      beforeEach(() => {
        fixture = TestBed.createComponent(ExampleComponent)
        component = fixture.componentInstance
        fixture.detectChanges()
      })
    
      it('should create', () => {
        expect(component).toBeTruthy()
      })
    })
    

    In your case you need to import FormsModule and/or ReactiveFormsModule in your spec file and prob other stuff too.

    To reduce the number of imports you may be able to get away with just importing your own modules into the spec file - e.g. AccountModule and/or AppModule - since these already import the forms stuff.