Skip to content

Commit

Permalink
fix: throw on non mocked method on class too
Browse files Browse the repository at this point in the history
  • Loading branch information
erdnaxeli committed Nov 14, 2024
1 parent a94083a commit 22b029b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/lib/proxy-class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,17 @@ describe('proxy class', () => {
expect((proxy as any).method5).toBeUndefined(); // unknown method should not be mocked
expect((proxy as any).method6).toBeUndefined(); // unknown method should not be mocked
});

it('should throw error on methods whose implementation is not mocked explicitly', async () => {
const clazz = TestClass;
const proxy = createClassProxy(clazz);

expect(() => {
proxy.method1();
}).toThrowError('Method method1 is not mocked');

expect(() => {
(proxy as any)[Symbol('foo')]();
}).toThrowError('Method Symbol(foo) is not mocked');
});
});
4 changes: 3 additions & 1 deletion src/lib/proxy-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export const createClassProxy = <T>(clazz: Class<T>): Mock<T> => {
}

if (functions.has(property as string)) {
target[property] = jest.fn();
target[property] = jest.fn().mockImplementation(() => {
throw new Error(`Method ${String(property)} is not mocked`);
});
}

return target[property];
Expand Down

0 comments on commit 22b029b

Please sign in to comment.