Skip to content
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

Closed
bartlewis opened this issue Feb 21, 2017 · 8 comments
Closed

jest.restoreAllMocks() for use with afterEach and beforeEach #2965

bartlewis opened this issue Feb 21, 2017 · 8 comments

Comments

@bartlewis
Copy link

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?

afterEach(() => {
  jest.restoreAllMocks();
});

Expect does this now: https://github.com/mjackson/expect#restorespies

Jasmine's spyOn behavior automatically does this between tests.

@bartlewis bartlewis changed the title jest.restoreAllMocks() for use with afterEach and befoeEach jest.restoreAllMocks() for use with afterEach and beforeEach Feb 21, 2017
@thymikee
Copy link
Collaborator

Have you tried jest.clearAllMocks?

@bartlewis
Copy link
Author

bartlewis commented Feb 21, 2017

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.
  });
});

@cpojer
Copy link
Member

cpojer commented Feb 22, 2017

I recommend setting up your spy and resetting it in beforeEach every time.

@bartlewis
Copy link
Author

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.

@antonsamper
Copy link

@cpojer is jest.restoreAllMocks() a planned feature?

@mareksuscak
Copy link
Contributor

I've been looking for this as well. Also, I have documented mockRestore which has not been documented in the API docs before. That might have been the source of all confusion although a function that does that with all mocks would be more than welcome.

@philipp-spiess
Copy link
Contributor

I just came across this issue and found out that jest.restoreAllMocks() is now implemented. Might help someone in the future :)

For more information, see: #4045

@github-actions
Copy link

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants