TypeError: Cannot read property 'getEcmUsername' of undefined

cancel
Showing results for 
Search instead for 
Did you mean: 
anshu_kumar
Active Member II

TypeError: Cannot read property 'getEcmUsername' of undefined

Hi Team,

Developing on ADF 2.4.0, I am using Karma(v2.0.4) Jasmine(2.6.4) to perform unit testing of the custom components by executing command 'ng test -sourcemap=false'.

I am getting error -- TypeError: Cannot read property 'getEcmUsername' of undefined

**Please find attached a screenshot of the error.

Below is the .spec.ts file

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { MatMenuModule } from '@angular/material/menu';
import { AuthenticationService, AppConfigService, AlfrescoApiService, StorageService, CookieService, LogService } from '@alfresco/adf-core';
import { HttpClientModule } from '@angular/common/http';

import { HeaderComponent } from './header.component';

describe('HeaderComponent', () => {
let component: HeaderComponent;
let fixture: ComponentFixture<HeaderComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule, MatMenuModule, HttpClientModule],
declarations: [HeaderComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
providers: [AuthenticationService, AppConfigService, AlfrescoApiService, StorageService, CookieService, LogService],
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(HeaderComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});

Please note that earlier, I was using karma(v1.7.0) and later upgraded to v2.0.2 but not luck. I get the same error.

Please help me with the solution here.

3 Replies
dvuika
Alfresco Employee

Re: TypeError: Cannot read property 'getEcmUsername' of undefined

We have hundreds of working unit tests that are also open. Please refer to the existing tests to see how corresponding services used.

anshu_kumar
Active Member II

Re: TypeError: Cannot read property 'getEcmUsername' of undefined

Hi Denys,

Any help to documentation links of existing tests would be really appreciated. Thanks!

anshu_kumar
Active Member II

Re: TypeError: Cannot read property 'getEcmUsername' of undefined

Hi,

I also tested a service that I created using Angular CLI. The service imports many ADF services that are used in it.

When i unit tested it by executing command 'ng test -sm=false' , I got couple of StaticInjectorError - No Provider...

So, I included all those services in the provider section of spec.ts file. Services included are:

beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientModule, MatSnackBarModule, HttpClientTestingModule, TranslateModule],
providers: [CustomService,
AppConfigService,
AlfrescoApiService,
StorageService,
LogService,
NotificationService,
TranslateService,
TranslationService,
UserPreferencesService
]
});
}

After all this, now I get following error

TypeError: this.customLoader.init is not a function at TranslationService.use (webpack:///C:/MSDE/anshkuma/msim-cms-new/src/node_modules/@alfresco/adf-core/esm5/adf-core.js?:2100:27) at SafeSubscriber.eval [as _next] (webpack:///C:/MSDE/anshkuma/msim-cms-new/src/node_modules/@alfresco/adf-core/esm5/adf-core.js?:2067:19) at SafeSubscriber.__tryOrSetError (webpack:///C:/MSDE/anshkuma/msim-cms-new/src/node_modules/rxjs/_esm5/Subscriber.js?:253:16) at SafeSubscriber.next (webpack:///C:/MSDE/anshkuma/msim-cms-new/src/node_modules/rxjs/_esm5/Subscriber.js?:193:27) at Subscriber._next (webpack:///C:/MSDE/anshkuma/msim-cms-new/src/node_modules/rxjs/_esm5/Subscriber.js?:132:26) at Subscriber.next (webpack:///C:/MSDE/anshkuma/msim-cms-new/src/node_modules/rxjs/_esm5/Subscriber.js?:96:18) at BehaviorSubject._subscribe (webpack:///C:/MSDE/anshkuma/msim-cms-new/src/node_modules/rxjs/_esm5/BehaviorSubject.js?:33:24) at BehaviorSubject.Observable._trySubscribe (webpack:///C:/MSDE/anshkuma/msim-cms-new/src/node_modules/rxjs/_esm5/Observable.js?:177:25) at BehaviorSubject.Subject._trySubscribe (webpack:///C:/MSDE/anshkuma/msim-cms-new/src/node_modules/rxjs/_esm5/Subject.js?:107:51) at BehaviorSubject.Observable.subscribe (webpack:///C:/MSDE/anshkuma/msim-cms-new/src/node_modules/rxjs/_esm5/Observable.js?:165:65)

I'd appreciate if you could let me know exactly how we can solve this testing issue.