如何使用重布线来模拟嵌套导出的依赖关系

时间:2019-05-03 19:41:26

标签: javascript node.js jasmine jasmine2.0

// 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 };

所以我的问题是-如果我将main-spec.js加载为:

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

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

我是否需要做些其他事情?

// 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!'); }});

0 个答案:

没有答案