Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!