Jest react keycloak client instance is not defined error

Viewed 32

In my react application I have a function which is using keycloak instance like in code below

function fetchUrlWithAuthentication(url) {
  const headers = new Headers();
  headers.set('Authorization', `Bearer ${keycloakClient.token}`);
  return fetch(url, {
    headers,
  });
}

in Jest file i'm getting error: keycloakClient is not defined

I tried to define in beforeEach keycloak instance but it didnt help, any suggestions how to fix this error ?

  beforeEach(() => {
    const keycloakClient = Keycloak({
      url: 'https://www.test.de/auth',
      realm: 'realm',
      clientId: 'clientId',
    });
    originalOpen = window.open;
    window.open = jest.fn().mockName('window.open');
  });
1 Answers

because your keycloakClient is not accessible from outside of your beforeEach scope. You have to declare your keycloackClient variable outside with a let and then in beforeEach, keycloaclCLient = Keycloack(...)

Related