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

How to use rewire to mock dependencies of nested exports #163

Open
kevindashgit opened this issue May 3, 2019 · 0 comments
Open

How to use rewire to mock dependencies of nested exports #163

kevindashgit opened this issue May 3, 2019 · 0 comments

Comments

@kevindashgit
Copy link

I created SO post about this here: https://stackoverflow.com/questions/55976172/how-to-use-rewire-to-mock-dependencies-of-nested-exports

But maybe it makes more sense to post this question here.

// a.js
var evil = require('evil');
module.exports = () => { evil.castCurse(); };
// b.js
var Evil = require('evil');
module.exports = () => { Evil.castCurse(); };
// main.js
var a = require('a.js');
var b = require('b.js');
module.exports = { a, b };

So my question here is - how can I employ a shim over both evil and Evil if I load main-spec.js as:

// main-spec.js
var rewire = require('rewire'),
    main = rewire(../../../main.js');

main.__set__('Evil', {});
main.__set__('evil', {});

Would I have to do something closer to this?

// main-spec.js
var rewire = require('rewire'),
    main = rewire(../../../main.js');

main.__get__('a').__set__('evil', { castCurse: () => { console.log('lucky a!'); }});
main.__get__('b').__set__('Evil', { castCurse: () => { console.log('lucky b!'); }});
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

1 participant