I'm trying to test a custom hook, that fetches some token from the API.
The ../api
is my own module. Both files are in the same directory
// useTokenHook.tsx
import { getToken } from '../api';
const someVariable = useSelector();
useEffect(() => {
const asyncWrapper = async () => {
const token = await getToken()
// do some stuff with token
}
asyncWrapper();
}, [someVariable]);
I'm trying to test it using jest and RTL
// useTokenHook.test.tsx
const getTokenMock = jest.fn();
jest.mock('../api', () => ({
getToken: () => getTokenMock,
}));
describe('x', () => {
it('should work', () => {
renderHook(() => { useTokenHook(); },{ wrapper });
expect(getTokenMock).toBeCalledTimes(1);
});
});
I get the error:
Expected number of calls: 1
Received number of calls: 0
Without the mock, I can see that the original getToken method gets called. I would assume the mock should also be called, but at least jest doesn't say so.
I tried adding async
to it()
callback but the result is the same
Based on RAllen answer I came up with the following:
// useTokenHook.test.tsx
import * as api from '../api';
jest.mock("../api");
describe('x', () => {
it('should work', () => {
const getTokenSpy = jest.spyOn(api, 'getToken')
renderHook(() => { useTokenHook(); },{ wrapper });
expect(getTokenSpy).toBeCalledTimes(1);
});
});
Which solves my problem