feat(utils): add jestMock.ts

This commit is contained in:
Ryan 2020-09-01 19:05:14 +08:00 committed by jeremywu
parent 2bd6cfe608
commit e41142f49b

View File

@ -0,0 +1,13 @@
export const jestMock = (object: any, method: string, implementation: () => any, accessType?: 'get' | 'set') => {
let mockSpy = null
if(!accessType){
mockSpy = jest.spyOn(object, method)
.mockImplementation(implementation)
}
if(accessType === 'get' || accessType === 'set'){
// must use `as 'get'` or `as 'set'` or `as 'any'`.
mockSpy = jest.spyOn(object, method, accessType as 'get')
.mockImplementation(implementation)
}
return mockSpy
}