I have a DeliveryRepository.ts like this:
export class DeliveryRepository {
constructor() {}
readonly function1() = async(): Promise<{ delivery?: Delivery }> => {
// do stuff 1
}
readonly function2 = async(): Promise<void> => {
// do stuff 2
}
readonly function3() = async(): Promise<{ delivery?: Delivery }> => {
// do stuff 3
}
}
And in my DeliveryService.ts:
import { DeliveryRepository } from '../DeliveryRepository'
export class DeliveryService {
constructor() {}
readonly postDelivery = async() : Promise<{ delivery?: Delivery }> => {
const deliveryRepository = new DeliveryRepository()
deliveryRepository.function1()
deliveryRepository.function2()
deliveryRepository.function3()
// ...do rest of service
}
}
and in my DeliveryService.test.ts, I wrote a test for postDelivery function like below:
describe('postDelivery', () => {
test('postDelivery function', async () => {
jest.mock('../DeliveryRepository', () => jest.fn()
.mockImplementation(() => {
return {
function1: (): any => {
return { delivery: undefined }
}
}
})
.mockImplementation(() => {
return {
function2: (): any => {
return null
}
}
})
.mockImplementation(() => {
return {
function3: (): any => {
return { delivery: undefined }
}
}
})
}
}
My problem here is when I run the test, it always throws TypeError: deliveryRepository.function1 is not a function error, if I comment out function1 call in service file, error turned to TypeError: deliveryRepository.function2 is not a function
I don't know what happened with my code, if my service call only one function from repository, the test is passed. I guess that this mockImplementation cannot call multiple functions from one repository, may I wrong, but if you have any idea, please comment to me. Thank you