I'm not sure if directly modifying the prototype chain makes it impossible to reset/clear the mock.
An implementation a spying on the prototype chain seems to be working better for me i.e.
import { ProductsClient } from './ProductsClient'; import { ProductManager } from './ProductManager'; it('should return the product', async () => { const expectedProduct = { id: 1, name: 'football', }; jest.spyOn(ProductsClient.prototype, 'getById') .mockReturnValue(expectedProduct); const productManager = new ProductManager(); const result = await productManager.getProductToManage(1); expect(result.name).toBe('football'); // It passes! });
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I'm not sure if directly modifying the prototype chain makes it impossible to reset/clear the mock.
An implementation a spying on the prototype chain seems to be working better for me i.e.