Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to __Rewire__ multiple values effectively (Jest) #214

Closed
jonnyparris opened this issue Jul 9, 2018 · 1 comment
Closed

Unable to __Rewire__ multiple values effectively (Jest) #214

jonnyparris opened this issue Jul 9, 2018 · 1 comment

Comments

@jonnyparris
Copy link

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!

@jonnyparris
Copy link
Author

PEBKAC, my tests were running before the Promise had resolved. Just needed to await the deleteFlaggedCandidateAccounts there.
😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant