2022-05-31

Error: Uncaught (in promise): true in jasmine test case for Angular

I'm new to angular unit testing , was trying to fix the already existing unit test case which fails the error that I getting is Error: Uncaught (in promise): true

The code for the unit test case is as follows

  it('ngOnInit', fakeAsync(() => {
        // fixture.detectChanges();
        component.ngOnInit();
        tick();
        expect(component.dataConfig.industries.length).toEqual(3);
        expect(component.dataConfig.fonts.length).toEqual(2);
      }));

now within ngOnInit a call is made to the function which is as below

checkUserRole=()=>{
    const requiredRoles = ['admin','developer'];
     this.hasAdminRole = this.service.checkRequiredRoles(
      requiredRoles,
      false
    );
  }

and within the spec.ts file they have created the stub and provided it in the provider as below

TestBed.configureTestingModule({
      declarations: [ClientConfigComponent],
      imports: [TestingModule, ngfModule, MatDialogModule,
        MaterialModule,
        FormsModule,
        ToastrModule.forRoot(),
        BrowserAnimationsModule,
        ReactiveFormsModule,
        SharedModule
      ],
      providers: [{
        provide: MatDialogRef,
        useValue: mockDialogRef
      }, {
        provide: MAT_DIALOG_DATA, useValue: {}
      },
      { provide: HttpClient, useClass: FakeHttpService },
      {
        provide: MasterService,
        useValue: masterService
      }
      ]
    })

Stub

const masterService = {
    loadTranslationConfiguration(): any {
      return of({});
    },
    checkRequiredRoles: (requiredRoles, arg) => ({}),
    saveTag(): any {
        return of(saveTags);
    },
    
  };

Error in detail

Error: Uncaught (in promise): true
        error properties: Object({ rejection: true, promise: [object Promise], zone: Zone({ _parent: Zone({ _parent: null, _name: '<root>', _properties: Object({  }), _zoneDelegate: _ZoneDelegate({ _taskCounts: Object({ microTask: 0, macroTask: 0, eventTask: 0 }), zone: <circular reference: Object>, _parentDelegate: null, _forkZS: null, _forkDlgt: null, _forkCurrZone: null, _interceptZS: null, _interceptDlgt: null, _interceptCurrZone: null, _invokeZS: null, _invokeDlgt: null, _invokeCurrZone: null, _handleErrorZS: null, _handleErrorDlgt: null, _handleErrorCurrZone: null, _scheduleTaskZS: null, _scheduleTaskDlgt: null, _scheduleTaskCurrZone: null, _invokeTaskZS: null, _invokeTaskDlgt: null, _invokeTaskCurrZone: null, _cancelTaskZS: null, _cancelTaskDlgt: null, _cancelTaskCurrZone: null

I tried returning true from the stub of checkRequiredRoles using the of operator of the observable still it gave me same issue,I tried returning the promise the checkRequiredRoles method in service and using .then in stub it gave undefined checkRequiredRoles().then

I'm somewhat sure that error is with checkRequiredRoles function returning a boolean value and not a promise

public checkIfCurrentUserHasRequiredRoles(
    role: string[],
    RequiredRole: boolean
  ): boolean {
    
    //Some Logic

    return isTrue;
  }

What should i do to fix this issue where i'm going wrong ,thanks in advance for help



No comments:

Post a Comment