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

Wish: restoreAll method #8

Open
apepper opened this issue Feb 28, 2018 · 3 comments
Open

Wish: restoreAll method #8

apepper opened this issue Feb 28, 2018 · 3 comments

Comments

@apepper
Copy link

apepper commented Feb 28, 2018

First all all thank you for your great plugin. It already helps a lot 🚀!

One thing, that I'm missing is a "global" restore. Currently after each rewire import/call I need to "clean up" afterwards by explicitly calling restore for that es6 class. If one forgets to do so, the rewire will also be used in the next spec. If one uses mocks a lot this can be cumbersome.

I'm thinking of something like this in setup.js of a jasmine test:

import restoreAll from 'rewire-exports';

afterEach(() => {
  restoreAll();
});

restoreAll() should restore all rewrites in all es6 modules used during the tests.

@asapach
Copy link
Owner

asapach commented Feb 28, 2018

In our team we've created an abstraction layer on top of the plugin that looks something like this:

import * as foo from './foo.js';

injector.rewire(foo, bar);
...
injector.restore();

Inside injector.rewire() method essentially calls foo.rewire() function (notice that it's imported as a namespace) and restore() calls foo.restore(). It keeps track of all the rewired modules and is essentially equivalent to what you're proposing. That would be very hard to do on the plugin level though, so I'd rather leave it to user-space.

I could share a basic implementation in a gist if you're interested.

@apepper
Copy link
Author

apepper commented Mar 1, 2018

I could share a basic implementation in a gist if you're interested.

Yes, I would be very interested.

@asapach
Copy link
Owner

asapach commented Mar 1, 2018

Here you go: https://gist.github.com/asapach/9f4ee23583f130e9a16b540f31cbfd93
I didn't include the support for named exports just to keep it simple, but it's trivial to add by using module[`rewire$${exportName}`] instead of module.rewire.

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

No branches or pull requests

2 participants