marchaos / jest-mock-extended

Type safe mocking extensions for Jest https://www.npmjs.com/package/jest-mock-extended
MIT License
845 stars 59 forks source link

Deep mock & Mock Implementations #6

Closed marchaos closed 4 years ago

marchaos commented 4 years ago

Some API Additions to support deep mocks and mock implementations. Note that this should not affect any existing usages of the API

Deep Mocks

test('can deep mock members', () => {
   const mockObj = mockDeep<Test1>();
   mockObj.deepProp.getNumber.calledWith(1).mockReturnValue(4);
   expect(mockObj.deepProp.getNumber(1)).toBe(4);
});

Mock Implementations

const mockObj = mock<Test1>({
      id: 61
});
expect(mockObj.id).toBe(61);

Deep Mocks with mock implementation

const mockObj = mockDeep<Test1>({
     deepProp: {
         getNumber: (num: number) => {
              return 76;
         }
     }
});
expect(mockObj.deepProp.getNumber(123)).toBe(76);