2

I am setting up a test class for my auth service which usings oidc-client.

When attempting to mock the function signinRedirect of oidc-client, I am getting hoisting issues and the following error is being thrown. I understand the problem but don't know how to tackle it.

The module factory of ``jest.mock()`` is not allowed to reference any out-of-scope variables. Invalid variable access: myFunc

const myFunc = jest.fn();

jest.mock('oidc-client', () => ({
    UserManager: () => ({
        signinRedirect: myFunc
    })
}));

describe('oAuth', () => {
    it('should call signin redirect when login is called', () => {
        login();

        expect(myFunc).toHaveBeenCalled();
    });
});
coding1223322
  • 461
  • 11
  • 26

1 Answers1

2

Try changing the name of myFunc to mockMyFunc.

If your mock variables are lazy you can prefix them with mock. I took this information from a little section of an error description I got a few minutes ago:

Note: This is a precaution to guard against uninitialized mock variables. If it is ensured that the mock is required lazily, variable names prefixed with mock (case insensitive) are permitted.

Daniel Muñoz
  • 1,374
  • 12
  • 18