-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jest.restoreAllMocks() for use with afterEach and beforeEach #2965
Comments
Have you tried |
const video = {
play: function () {
return true;
}
};
describe('video', () => {
afterEach(() => {
// jest.clearAllMocks(); // Does not remove mockImplementation between tests
// jest.restoreAllMocks(); // What I want to do.
});
test('plays video', () => {
const spy = jest.spyOn(video, 'play').mockImplementation(() => {
return 42;
});
const isPlaying = video.play();
expect(spy).toHaveBeenCalled();
expect(isPlaying).toBe(42);
spy.mockRestore(); // What I have to do [to completely remove the spy].
});
test('plays video', () => {
const isPlaying = video.play();
expect(spy).toHaveBeenCalled();
expect(isPlaying).toBe(true); // Fails without "mockRestore". isPlaying will be 42.
});
}); |
I recommend setting up your spy and resetting it in beforeEach every time. |
That makes sense for this simple example. But if there are several test with several spies, that would become cumbersome quickly. For now, handling them within each test seems like the best pattern available. |
@cpojer is |
I've been looking for this as well. Also, I have documented |
I just came across this issue and found out that For more information, see: #4045 |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Do you want to request a feature or report a bug?
Feature
What is the current behavior?
To restore a mock to it's original implementation, you need to add
spy.mockRestore()
within each test.What is the desired behavior?
Expect does this now: https://github.com/mjackson/expect#restorespies
Jasmine's
spyOn
behavior automatically does this between tests.The text was updated successfully, but these errors were encountered: