xeraphim
xeraphim

Reputation: 4645

Angular: ERROR: ''mat-form-field' is not a known element

When executing my unit tests, I get errors like:

ERROR: ''mat-form-field' is not a known element

ERROR: ''mat-grid-tile' is not a known element

etc.

Normally you get these errors when an import is missing in the app.module.ts but I made sure, that they are available:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { NavigationviewComponent } from './navigationview/navigationview.component';
import { MainviewComponent } from './mainview/mainview.component';
import { DetailviewComponent } from './detailview/detailview.component';
import { TodolistComponent } from './todolist/todolist.component';
import { ListitemComponent } from './listitem/listitem.component';
import { ListitemdetailComponent } from './listitemdetail/listitemdetail.component';

import { DonePipe } from './DonePipe';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatDatepickerModule } from '@angular/material/datepicker';
import { MatNativeDateModule } from '@angular/material/core';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatButtonModule} from '@angular/material/button';
import {MatIconModule} from '@angular/material/icon';
import {MatBadgeModule} from '@angular/material/badge';
import {MatGridListModule} from '@angular/material/grid-list';
import {MatSnackBarModule} from '@angular/material/snack-bar';

import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

import { Configuration } from 'msal';
import {
  MsalModule,
  MsalInterceptor,
  MSAL_CONFIG,
  MSAL_CONFIG_ANGULAR,
  MsalService,
  MsalAngularConfiguration
} from '@azure/msal-angular';

import { isIE, msalAngularConfig, msalConfig } from './app-config';

function MSALConfigFactory(): Configuration {
  return msalConfig;
}

function MSALAngularConfigFactory(): MsalAngularConfiguration {
  return msalAngularConfig;
}

@NgModule({
  declarations: [
    AppComponent,
    NavigationviewComponent,
    MainviewComponent,
    DetailviewComponent,
    TodolistComponent,
    ListitemComponent,
    ListitemdetailComponent,
    DonePipe
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    BrowserAnimationsModule,
    FormsModule,
    CommonModule,
    MsalModule,
    MatDatepickerModule,
    MatNativeDateModule,
    MatInputModule,
    MatFormFieldModule,
    MatButtonModule,
    MatIconModule,
    MatBadgeModule,
    MatGridListModule,
    MatSnackBarModule
  ],
  exports: [
    MatDatepickerModule,
    MatNativeDateModule,
    MatInputModule,
    MatFormFieldModule,
    MatButtonModule,
    MatIconModule,
    MatBadgeModule,
    MatGridListModule,
    MatSnackBarModule
  ],
  providers: [
    MatFormFieldModule,
    MatDatepickerModule,
    MatNativeDateModule,
    MatButtonModule,
    {
      provide: HTTP_INTERCEPTORS,
      useClass: MsalInterceptor,
      multi: true
    },
    {
      provide: MSAL_CONFIG,
      useFactory: MSALConfigFactory
    },
    {
      provide: MSAL_CONFIG_ANGULAR,
      useFactory: MSALAngularConfigFactory
    },
    MsalService
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

Is there anything else that must be considered so that the errors are not showing up?

Thanks in advance

Upvotes: 0

Views: 1005

Answers (2)

Kamran Khatti
Kamran Khatti

Reputation: 4147

Try importing MatFormFieldModule, MatGridListModule in your .spec file to tell compiler about Mat Form and Grid elements.

Edited: beforeEach as per suggestion of Phillip :)

beforeEach(() => {
  TestBed.configureTestingModule({
  imports: [MatFormFieldModule, MatGridListModule], // <== need to import both modules.
    providers: [...],
    declarations: [...],
 });
});

Upvotes: 1

Jobelle
Jobelle

Reputation: 2834

In the Unit test file, you have to import MatFormFieldModule & MatGridListModule

Upvotes: 1

Related Questions