Unable to mock for test
Hi,
I am trying to mock adalFetch but with no luck. I keep getting,
{ message: 'User login is required', msg: 'login required' }
here is what I have so far
jest.mock('./../config/adal-config', () => ({
authContext: () => jest.fn(),
getToken: () => jest.fn(),
adalApiFetch: () => Promise.resolve({ json: () => mockedReturnedData }),
adalFetch: () => jest.fn()
}));
Any help would be greatly appreciated.
Hi @ergunp
You're likely missing one of the functions that you need to mock, however that's just a guess
Here's the mock I've generated to be able to test. react-adal.js file in/src/__mocks__dir.
I've set the mockup Azure AD response in mockupPayload
import jwt from 'jsonwebtoken';
const mockupPayload = {
…
};
const generateToken = () => jwt.sign(mockupPayload, 'secret');
const withAdalLogin = () => null;
const adalFetch = () => null;
const runWithAdal = jest.fn((auth, callback) => callback());
class AuthenticationContext {
getCachedToken = () => generateToken();
getCachedUser = () => ({
userName: ‘[email protected]',
profile: mockupPayload,
});
}
export { AuthenticationContext, adalFetch, withAdalLogin, runWithAdal };
@J-theGit Thanks I'll give this a shot
Actually found a better way
await act(async () => {
jest.spyOn(adalConf, 'adalApiFetch').mockImplementation(() => Promise.resolve({ json: () => existingChange }));
});
I'm currently stuck on the same error message using testing-library. How did you resolve this issue?