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

Allow all rewires to be reverted via rewire.reset() #71

Closed
amay opened this issue Jul 23, 2015 · 4 comments
Closed

Allow all rewires to be reverted via rewire.reset() #71

amay opened this issue Jul 23, 2015 · 4 comments

Comments

@amay
Copy link

amay commented Jul 23, 2015

When testing in mocha, it would be nice to have a way to reset all of the injections that have been setup via __set__. Maybe something like this:

// set this up so that it will be run after ever spec
afterEach(function() {
  rewire.reset();
});

The motivation behind this is that we ran into an issue where specs would break when run solo, but not when run together, and part of the problem was that __set__ is not reverted after a single test run. We are aware of how you can manually revert by storing a reference to the function returned by __set__ and calling it after the test, but the approach mentioned above would make it a lot less error prone.

I took a crack at trying to implement this, but I lost my way when I dug in and realized we are using eval of an IIFE function to define __set__. I couldn't think of a good way of making __set__ aware of some context in the rewire module where the revert functions could be stored such that reset could just invoke all those reverts. Any tips on how we might achieve this?

amay referenced this issue in substantial/rewire Jul 23, 2015
@jhnns
Copy link
Owner

jhnns commented Jul 27, 2015

If you want to bring your module back to the initial state, I think it's the best to just rewire() it again. This will create a fresh instance of the module.

@amay
Copy link
Author

amay commented Aug 3, 2015

Fair enough. I'll close this.

@amay amay closed this as completed Aug 3, 2015
@srlowe
Copy link

srlowe commented May 16, 2019

I also would love to have a global rewire.reset(); calling rewuire() again is not always possible, depending on code standards.

@sheltonsuen
Copy link

I also would love to have a global rewire.reset(); calling rewuire() again is not always possible, depending on code standards.

I would like to use it within afterEach for all but seems like what I can do is revert it one by one 😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants