Я вызываю внешний скрипт в моем приложении.
Я тестирую с использованием jasmine / karma, тесты успешны в выводе chrome, однако в терминале я получаю
Chrome 75.0.3770 (Mac OS X 10.12.6) ERROR
An error was thrown in afterAll
[object ErrorEvent] thrown
PhantomJS 2.1.1 (Mac OS X 0.0.0)
Я знаю, что это при вызове скрипта, потому что при изменении или пропуске ошибки исчезают
SPE C:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { NavigationEnd } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { of } from 'rxjs/observable/of';
import { MainComponent } from './main.component';
import { HeaderComponent } from '@app/main/header/header.component';
import { FooterComponent } from '@app/main/footer/footer.component';
import { CookieComponent } from '@app/shared/components/cookie/cookie.component';
import { HttpClientTestingModule } from '@angular/common/http/testing';
fdescribe('MainComponent', () => {
let component: MainComponent;
let fixture: ComponentFixture<MainComponent>;
class MockRouterNavigationEnd {
public events = of(new NavigationEnd(0, '/someurl', '/someurl'));
public url = '/someurl';
}
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule, HttpClientTestingModule],
declarations: [ MainComponent, HeaderComponent, FooterComponent, CookieComponent ],
providers: []
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MainComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
fit('should initiate salesforce webchat', () => {
component['router'] = <any> new MockRouterNavigationEnd();
component['loadOptileadScript'](component['brand']);
});
});
ФУНКЦИЯ КОМПОНЕНТА:
loadOptileadScript(param: String) {
this.subs = this.router.events
.pipe(filter((event: NavigationEnd) => event instanceof NavigationEnd))
.subscribe(() => {
let scriptName: String;
const scriptNode = document.createElement('script');
if (param === param.one) { scriptName = 'privmotor'; }
if (param === param.two) { scriptName = 'churchillmotor'; }
if (param === param.three) { scriptName = 'dlmotor'; }
scriptNode.type = 'text/javascript';
scriptNode.async = true;
scriptNode.src = `https://dyn-img.com/i/${scriptName}.js`;
document.getElementsByTagName('head')[0].appendChild(scriptNode);
});
Как я уже сказал, все тесты работают нормально, просто получите ошибку от фантома js