Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

144
Vistas
Expect mocked library function to have been called

I am mocking a function in a library (auth0), this way:

jest.mock('@auth0/auth0-react', () => ({
    useAuth0: () => {
        return {
            logout: jest.fn(),
        }
    }
}));

During my test, how can I expect the logout function to have been called?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Since you are trying to manually mock a node_module, you need to create a mock module file. Manual mocks are defined by writing a module in a mocks/ subdirectory immediately adjacent to the module. In your case the mock should be placed in the __mocks__ directory adjacent to node_modules.

root/__mocks__/@auth0/auth0-react.js

'use strict';

const handleRedirectCallback = jest.fn(() => ({ appState: {} }));
const buildLogoutUrl = jest.fn();
const buildAuthorizeUrl = jest.fn();
const checkSession = jest.fn();
const getTokenSilently = jest.fn();
const getTokenWithPopup = jest.fn();
const getUser = jest.fn();
const getIdTokenClaims = jest.fn();
const isAuthenticated = jest.fn(() => false);
const loginWithPopup = jest.fn();
const loginWithRedirect = jest.fn();
const logout = jest.fn();

export const Auth0Client = jest.fn(() => {
  return {
    buildAuthorizeUrl,
    buildLogoutUrl,
    checkSession,
    handleRedirectCallback,
    getTokenSilently,
    getTokenWithPopup,
    getUser,
    getIdTokenClaims,
    isAuthenticated,
    loginWithPopup,
    loginWithRedirect,
    logout,
  };
});

test.ts (in case you are writing test in typescript)

import { mocked } from 'ts-jest/utils';
const clientMock = mocked(new Auth0Client({ client_id: '', domain: '' }));

describe('auth0 test', () => {
   it('should check logout is called', async () => {
      await my_logout_function();
      expect(clientMock.logout).toHaveBeenCalled();
   });
});

test.js (in case you are writing tests in javascript)

const clientMock = new Auth0Client({ client_id: '', domain: '' });

describe('auth0 test', () => {
   it('should check logout is called', async () => {
      await my_logout_function();
      expect(clientMock.logout).toHaveBeenCalled();
   });
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda