I have a function inside the action directory that I want to test:
import { Action, ActionProgress, ActionStatus, MagicLinkProgress } from '../../interfaces'
import { areSameActions } from '../actionsProgress'
export const findActionProgressToDisplay = (progress: MagicLinkProgress, actions: Array<Action>): ActionProgress => {
const actionProgressFound = progress.actionsProgress.find(
(actionProgress) => actionProgress.status === ActionStatus.PENDING && actions.some((action) => areSameActions(actionProgress.action, action))
)
if (!actionProgressFound) {
throw new Error('No action found to display')
}
return actionProgressFound
}
The function findActionProgressToDisplay calls another function called areSameActions which is imported from ../actionsProgress.
Here is the function that needs to be mocked :
import { Action } from '../../interfaces'
export const areSameActions = (firstAction: Action, secondAction: Action): boolean =>
firstAction.origin === secondAction.origin && firstAction.type === secondAction.type && firstAction.url === secondAction.url
Below is the jest test I attempted:
import { mockActions } from './../../mocks/campaigns/index'
import { mockActionsProgress, mockProgress } from './../../mocks/magicLinks/index'
import { findActionProgressToDisplay } from './index'
import { describe, test, expect } from '@jest/globals'
import { ActionStatus } from '../../interfaces'
import * as module from '../actionsProgress/index'
module.areSameActions = jest.fn().mockReturnValue(true)
describe('=== findActionProgressToDisplay ===', () => {
test('should return true', () => {
const progress = mockProgress({ actionsProgress: mockActionsProgress() })
const actions = mockActions()
const actionFound = findActionProgressToDisplay(progress, actions)
expect(actionFound.status).toEqual(ActionStatus.PENDING)
})
})
But I encountered this error: Cannot assign to 'areSameActions' because it is a read-only property. This makes sense since areSameActions is a const.
I also tried the following approach:
import { mockActions } from './../../mocks/campaigns/index'
import { mockActionsProgress, mockProgress } from './../../mocks/magicLinks/index'
import { findActionProgressToDisplay } from './index'
import { describe, test, expect } from '@jest/globals'
import { ActionStatus } from '../../interfaces'
import * as module from '../actionsProgress/index'
jest.spyOn(module, 'areSameActions').mockReturnValue(true)
describe('=== findActionProgressToDisplay ===', () => {
test('should return true', () => {
const progress = mockProgress({ actionsProgress: mockActionsProgress() })
const actions = mockActions()
const actionFound = findActionProgressToDisplay(progress, actions)
expect(actionFound.status).toEqual(ActionStatus.PENDING)
})
})
However, I received this error: TypeError: Cannot redefine property: areSameActions. I am unsure why this issue occurred.
Lastly, I attempted the following solution:
import { mockActions } from './../../mocks/campaigns/index'
import { mockActionsProgress, mockProgress } from './../../mocks/magicLinks/index'
import { findActionProgressToDisplay } from './index'
import { describe, test, expect } from '@jest/globals'
import { ActionStatus } from '../../interfaces'
import { areSameActions } from '../actionsProgress/index'
jest.mock('../actionsProgress/index', () => {
const original = jest.requireActual('../actionsProgress/index')
return {
...original,
areSameActions: jest.fn(),
}
})
describe('=== findActionProgressToDisplay ===', () => {
test('should return true', () => {
areSameActions.mockImplementation(() => (true))
const progress = mockProgress({ actionsProgress: mockActionsProgress() })
const actions = mockActions()
const actionFound = findActionProgressToDisplay(progress, actions)
expect(actionFound.status).toEqual(ActionStatus.PENDING)
})
})
Unfortunately, I received an error stating: Property 'mockImplementation' does not exist on type '(firstAction: Action, secondAction: Action) => boolean'. This error seems reasonable considering in the current context, areSameActions is a function and not a mock.
Could you provide guidance on how to mock / stub / spy the function areSameActions to effectively test findActionProgressToDisplay?