angularkarma-jasminegulp-karma

Angular2 Jasmine Test Image Source


I have a component with an image in template

 <div class="logo"><img src="../images/logo.png"/></div>

When running karma task it throws such error

Uncaught Error: Cannot find module "../images/logo.png"

To mention that app renders the image fine , only karma is complaining.

Any advice will be appreciated.


Solution

  • You could try something like this:

    it('should render the logo', async(() => {
      const fixture = TestBed.createComponent(AppComponent);
      fixture.detectChanges();
      const compiled = fixture.debugElement.nativeElement;
      expect(compiled.querySelector('div.logo>img').src).toContain('/images/logo.png');
    }));