Closed jonnyparris closed 5 years ago
Hello, can you spot what's wrong with the setup below that's preventing my second mock function from being called?
Code to be tested (tasks.js):
export const deleteFlaggedCandidateAccounts = async (app) => { const timer = moment() const users = app.service('users') const events = app.service('events') const usersForDeletion = await findFlaggedCandidates({ events, users }) if (usersForDeletion.length > 0) { const subCollectionNames = ['jobActivities', 'interviews', 'matches', 'offers'] const subCollections = subCollectionNames.map(serviceName => { return { name: serviceName, service: app.service(serviceName) } }) const profiles = app.service('profiles') const images = app.service('images') const videos = app.service('videos') usersForDeletion.forEach(({ _id: id, defaultObjects }) => { defaultObjects.forEach(({ target: { _id: profileId }, type }) => { if (type === CANDIDATE_DEFAULT_OBJECT_TYPE) { logger.debug(`Redacting data & deleting account details for user: ${id} with profileId: ${profileId}`) deleteCandidateAccount({ users, profiles, images, videos, subCollections, id, profileId }) } }) }) } }
My jest test:
import { CANDIDATE_DEFAULT_OBJECT_TYPE } from './tasks' import TasksModule from './tasks' import { deleteFlaggedCandidateAccounts, __RewireAPI__ as _TasksRewireAPI } from './tasks' const mockApp = { service: jest.fn() } const mockFlaggedUsers = [ { _id: 'userID101', defaultObjects: [{ target: { _id: 'profileID101' }, type: CANDIDATE_DEFAULT_OBJECT_TYPE }] } ] describe('deleteCandidateAccounts', () => { it('should just bloody work', async () => { const mockDelete = jest.fn() TasksModule.__Rewire__('findFlaggedCandidates', () => mockFlaggedUsers) TasksModule.__Rewire__('deleteCandidateAccount', () => mockDelete) deleteFlaggedCandidateAccounts(mockApp) expect(mockApp.service).toHaveBeenCalled() // PASS expect(mockDelete).toHaveBeenCalled() // FAIL :( no idea why __rewire_reset_all__() }) })
If I set the findFlaggedCandidates as a constant in the method so that I'm only mocking deleteCandidateAccount then the test passes fine hence my suspicion and the issue title. Any help/pointers much appreciated!
findFlaggedCandidates
deleteCandidateAccount
PEBKAC, my tests were running before the Promise had resolved. Just needed to await the deleteFlaggedCandidateAccounts there. 😅
await
deleteFlaggedCandidateAccounts
Hello, can you spot what's wrong with the setup below that's preventing my second mock function from being called?
Code to be tested (tasks.js):
My jest test:
If I set the
findFlaggedCandidates
as a constant in the method so that I'm only mockingdeleteCandidateAccount
then the test passes fine hence my suspicion and the issue title. Any help/pointers much appreciated!