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

前端 未结 6 2040
不思量自难忘°
不思量自难忘° 2021-02-05 10:16

I\'m currently implementing \'unit testing\' in my angular application. However, if I run them, I receive multiple warnings/errors similar to this one: \'Error retrieving

6条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-05 10:47

    There's also another way how to avoid this problem. Spectator (which is btw really awesome tool for Angular unit testing) has handy shallow flag, which basically prevents all child components (including mat-icon) to be compiled but handled as simple HTML tag instead.

    const createComponent = createComponentFactory({
      component: MyComponent,
      imports: [
        ...
      ],
      mocks: [
        ...
      ],
      providers: [
        ...
      ],
      shallow: true // <-- ignore child components
    });
    

    With this flag enabled, you don't need to import MatIconModule anymore, also you reduce the test runtime, because Angular doesn't compile extra components while running the test. This allows you to really focus on the component itself while writing unit tests, without worrying about the stuff you don't need anyway.

    It's a win-win if you ask me :)

提交回复
热议问题