0
In my project I’m updating to Node 18 and also converting from commonjs to esm.
When I run the Jest tests I’m getting this error
TypeError: request.mockResolvedValue is not a function
In the following file but as you see the jest.mock()
is on top of the scope as per documentation and also what I saw in other similar questions/answers.
I’m unable to find a solution to this issue
import { jest } from '@jest/globals';
import request from 'graphql-request';
import {
logActivityPhoneCallRescheduled,
logActivityPhoneCallScheduled,
logActivityPhoneCallUnscheduled,
} from './candidateService';
jest.mock('graphql-request');
beforeEach(() => {
jest.clearAllMocks();
});
test('scheduled', async () => {
request.mockResolvedValue({ addCandidateActivity: { id: 835 } });
const now = Date.now();
const input = {
candidateId: 'TBX-001',
eventId: 305,
userId: 'y29d13lkcas91',
userIsStaff: true,
scheduledFor: new Date(now + 14 * 24 * 60 * 60 * 1e3).toISOString(),
createdAt: new Date(now - 905 * 1e3).toISOString(),
};
const expectedActivity = {
candidateId: 'TBX-001',
type: 'SCHEDULE',
action: 'scheduled',
newValue: '305',
additionalPayload: {
type: 'phonecall',
scheduled_at: input.scheduledFor,
},
userId: 'y29d13lkcas91',
userIsStaff: true,
createdAt: input.createdAt,
};
const result = await logActivityPhoneCallScheduled(input);
expect(result).toBe(835);
expect(request).toHaveBeenCalledTimes(1);
expect(request.mock.calls[0][0]).toBe('https://candidate-service:5000');
expect(request.mock.calls[0][1]).toContain('addCandidateActivity');
expect(request.mock.calls[0][2]).toEqual({ activity: expectedActivity });
});
test('re-scheduled', async () => {
request.mockResolvedValue({ addCandidateActivity: { id: 836 } });
const now = Date.now();
const input = {
candidateId: 'TBX-002',
eventId: 107,
userId: 'chqef273',
userIsStaff: true,
scheduledFrom: new Date(now + 12 * 24 * 60 * 60 * 1e3).toISOString(),
scheduledFor: new Date(now + 14 * 24 * 60 * 60 * 1e3).toISOString(),
createdAt: new Date(now - 815 * 1e3).toISOString(),
};
const expectedActivity = {
candidateId: 'TBX-002',
type: 'SCHEDULE',
action: 'rescheduled',
newValue: '107',
additionalPayload: {
type: 'phonecall',
scheduled_at: input.scheduledFor,
old_scheduled_at: input.scheduledFrom,
},
userId: 'chqef273',
userIsStaff: true,
createdAt: input.createdAt,
};
const result = await logActivityPhoneCallRescheduled(input);
expect(result).toBe(836);
expect(request).toHaveBeenCalledTimes(1);
expect(request.mock.calls[0][0]).toBe('https://candidate-service:5000');
expect(request.mock.calls[0][1]).toContain('addCandidateActivity');
expect(request.mock.calls[0][2]).toEqual({ activity: expectedActivity });
});
test('un-scheduled', async () => {
request.mockResolvedValue({ addCandidateActivity: { id: 837 } });
const now = Date.now();
const input = {
eventId: 53,
candidateId: 'TBX-003',
userId: 'dc427',
userIsStaff: false,
scheduledFor: new Date(now + 8792 * 1e3).toISOString(),
};
const expectedActivity = {
candidateId: 'TBX-003',
type: 'SCHEDULE',
action: 'unscheduled',
newValue: '53',
additionalPayload: {
type: 'phonecall',
scheduled_at: input.scheduledFor,
},
userId: 'dc427',
userIsStaff: false,
};
const result = await logActivityPhoneCallUnscheduled(input);
expect(result).toBe(837);
expect(request).toHaveBeenCalledTimes(1);
expect(request.mock.calls[0][0]).toBe('https://candidate-service:5000');
expect(request.mock.calls[0][1]).toContain('addCandidateActivity');
expect(request.mock.calls[0][2]).toEqual({ activity: expectedActivity });
});