From e41142f49bb931d4ce32d5875f0e83259bea8fe2 Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 1 Sep 2020 19:05:14 +0800 Subject: [PATCH] feat(utils): add jestMock.ts --- packages/utils/jestMock.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 packages/utils/jestMock.ts diff --git a/packages/utils/jestMock.ts b/packages/utils/jestMock.ts new file mode 100644 index 0000000000..6e9bea2d44 --- /dev/null +++ b/packages/utils/jestMock.ts @@ -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 +}