#keycloak - tests units

23 messages · Page 1 of 1 (latest)

last light
#

keycloak works well in the application.
on the other hand, the unit tests with Jest I always get this error:

 Cannot find module 'keycloak-angular/services/keycloak.service' from 'src/xxxxxxxxxx/services/authentification.service.ts'
    Require stack:
      src/xxxxxxxxxx/services/authentification.service.ts
describe('xxxxxxxxxxxxxx', () => {
...
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [xxxxxxxxxxxComponent, TranslateModule.forRoot()],

      providers: [
        provideMockStore(),
        { provide: NGXLogger, useValue: LoggerMock },
        { provide: KeycloakService, useClass: MockedKeycloakService },
        AuthentificationService
      ]
    });
    fixture = TestBed.createComponent(xxxxxxxxxxxComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

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

MockedKeycloakService

import { Injectable } from '@angular/core';
import { KeycloakService } from 'keycloak-angular';

@Injectable()
export class MockedKeycloakService extends KeycloakService {
  init(): Promise<boolean> {
    return Promise.resolve(true);
  }

  getKeycloakInstance(): any {
    return {
      loadUserInfo: () => {
        let callback;
        Promise.resolve().then(() => {
          callback({
            userName: 'name'
          });
        });
        return {
          success: (fn) => (callback = fn)
        };
      }
    } as any;
  }
}

AuthentificationService

  public constructor(
    public readonly _keycloakService: KeycloakService,
...

I also tried like this :

...
      providers: [
        provideMockStore(),
        { provide: NGXLogger, useValue: LoggerMock },
        KeycloakService,
        AuthentificationService
      ]
...

why it cannot find the Keycloak service even though it is well defined in providers: [ ... ]

raven kite
#

What are the imports in AuthenticationService?

last light
raven kite
#

There u go.

#

Change it.

#

That's not how it should be done.

last light
#

with: import { KeycloakService } from './../../keycloak-angular/services/keycloak.service';

it work !

thank !

raven kite
#

No

#

That's not how it works.

#

Please look at your mock service and use the same import.

#

from 'keycloak-angular'

last light
#

yes , my mock :
import { KeycloakService } from './../../../../../../keycloak-angular/services/keycloak.service';

raven kite
#

No please

#

Use the correct path.

#

You should not do what you are trying.

last light
#

AuthentificationService
import { KeycloakService } from './../../keycloak-angular/services/keycloak.service';
public constructor(
public readonly _keycloakService: KeycloakService,
...

MockedKeycloakService
import { KeycloakService } from '../services/keycloak.service';
...

keycloak-angular
....mock
........MockedKeycloakService
....services
........keycloak.service.ts

#

heuuu ... no ?

sorry, I don't know what you were trying to make me understand. but it seems to work

raven kite
#

import { KeycloakService } from './../../keycloak-angular/services/keycloak.service';

#

This is not correct?

#

You should use

import { KeycloakService } from 'keycloak-angular';
#
#

You should not rely on the internal structure of an npm package. Instead you should import as per the docs.

#

@last light ☝️