How do I add svg files via MatIconRegistry in unit tests?

can just do:

import { MatIcon } from '@angular/material/icon';
import { MatIconTestingModule } from '@angular/material/icon/testing';

describe('MyComponent', () => {

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [MyComponent, MatIcon],
      imports: [MatIconTestingModule],
    }).compileComponents();
  }));

...

});

This will generate a test icon rendering without the HTTP request.

NOTE: Newer versions of Angular, e.g., >16, may produce an error unless you remove import { MatIcon }.

Leave a Comment