Prerequisites
Before you get started with this tutorial, I'm going to presume that you already have a JavaScript project that you're wor...
For further actions, you may consider blocking this person and/or reporting abuse
Any hints on how to restore the original functionality at the end of the test? When using this technique I'm seeing that the order the tests run in now matters—each test after this one ends up using that same mock. :(
Looking at the mock function documentation - jestjs.io/docs/en/mock-function-api, you should be able to use
mockFn.mockClear()
,mockFn.mockReset()
, ormockFn.mockRestore()
depending on your needs. Hopefully this does what you need.I have tried all of these functions and still the value is either the same or undefined. Any hints?
Have you tried
afterEach(() => jest.resetAllMocks());
?If I change the getById method to
the test will still pass. This is not expected behavior I want from test
I'm not quite sure what you mean. In this example the 'ProductsManager' is the class being tested. The 'ProductsClient' is being mocked so we make assumptions about its behaviour in order to test the 'ProductsManager' in isolation. It's assumed that the 'ProductsClient' would be tested separately.
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.
i get
ReferenceError: regeneratorRuntime is not defined
Very clever to mock the method through the
prototype
. I was struggling for hours, thank you!Is there a reason why using
and not .resolves?
thank you! I'm wondering why it's not written on the jest docs that we have to override the method through prototype.