I want to test this very simple function with jest and ts-jest.
I am using es modules.
export const tobetested = async () => {
const targetUri = await getFunctionUrl('presaveJob');
console.log(targetUri);
};
Function getFunctionUrl
is placed on the same file and it’s
export async function getFunctionUrl(name: string, location = 'us-central1') {
if (!auth) {
auth = new GoogleAuth({
scopes: 'https://www.googleapis.com/auth/cloud-platform',
});
}
const projectId = await auth.getProjectId();
const url =
'https://cloudfunctions.googleapis.com/v2beta/' +
`projects/${projectId}/locations/${location}/functions/${name}`;
const client = await auth.getClient();
const res = await client.request({ url });
// @ts-ignore
const uri = res.data?.serviceConfig?.uri;
if (!uri) {
throw new Error(`Unable to retrieve uri for function at ${url}`);
}
return uri;
}
Then in the test file
import { test, jest } from '@jest/globals';
import * as queues from '../queues';
test('Returns correct value', async () => {
jest.spyOn(queues, 'getFunctionUrl').mockResolvedValue('sample string');
await queues.tobetested();
});
The problem is that from the output i see it’s called the real function and not the mocked one.
Where am I wrong?
Thanks