babel-plugin-rewire-exports
babel-plugin-rewire-exports copied to clipboard
Wish: restoreAll method
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.
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.
I could share a basic implementation in a gist if you're interested.
Yes, I would be very interested.
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
.