This is also a question on Auth0 node-auth0 library. The use case is that I am using the Auth0 create Actions API through Terraform and want to be able to writes tests against the Actions.
In this example I want to be able to test the onExecutePostLogin without using real values.
// auth0-post-login.js
exports.onExecutePostLogin = async (event, api) => {
  const userId = event.user.user_id
  const ManagementClient = require('auth0').ManagementClient
  const management = new ManagementClient({
    domain: event.secrets.domain,
    clientId: event.secrets.clientId,
    clientSecret: event.secrets.clientSecret,
    audience: `https://${event.secrets.domain}/api/v2/`,
    scope: 'read:users',
  })
  const params = { id: userId, page: 0, per_page: 50, include_totals: false }
  let userPermissions = await management.getUserPermissions(params)
  const map = require('array-map')
  const permissions = map(userPermissions, function(permission) {
    return permission.permission_name
  })
  api.accessToken.setCustomClaim('https://example.com/access', permissions.join(' '))
}
One of the main issues is that the functions like getUserPermissions is created through their utility wrapper:
utils.wrapPropertyMethod(ManagementClient, 'getUserPermissions', 'users.getPermissions');
This causes jest to have issues finding the functions.
I did something similar as stsmurf to mock the response of the Auth0 methods.
I have a file where I store my helper methods like "find a role by its name"
// helper.ts
import { ManagementClient } from 'auth0';
export const getRoleByName = async (roleName: string) => {
  const api = new ManagementClient({
    clientId: clientId,
    clientSecret: clientSecret,
    domain: domain,
  });
  const roles = await api.getRoles();
  const role = roles.find((r) => r.name == roleName);
  if (!role) throw new Error('Role not found');
  return role;
};
// helper.test.ts
import { Role } from 'auth0';
import { getRoleByName } from './helpers';
const mockGetRoles = jest.fn();
jest.mock('auth0', () => {
  return {
    ManagementClient: jest.fn().mockImplementation(() => {
      return {
        getRoles: mockGetRoles,
      };
    }),
  };
});
describe('Get role', () => {
  beforeAll(() => {
    const dummyRoles: Role[] = [
      { id: 'fake_id_1', description: 'Fake role nr 1', name: 'Fake Role 1' },
      { id: 'fake_id_2', description: 'Fake role nr 2', name: 'Fake Role 2' },
    ];
    mockGetRoles.mockImplementation(() => dummyRoles);
  });
  it('can return a role if it exists', async () => {
    const expectedResult: Role = {
      id: 'fake_id_1',
      description: 'Fake role nr 1',
      name: 'Fake Role 1',
    };
    const result = await getRoleByName('Fake Role 1');
    expect(expectedResult).toEqual(result);
  });
  it('will throw an error when a role is not found', async () => {
    await expect(getRoleByName('Fake Role 3')).rejects.toThrowError();
  });
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With