speedskater / babel-plugin-rewire

A babel plugin adding the ability to rewire module dependencies. This enables to mock modules for testing purposes.
843 stars 90 forks source link

Unable to __Rewire__ multiple values effectively (Jest) #214

Closed jonnyparris closed 5 years ago

jonnyparris commented 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!

jonnyparris commented 5 years ago

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