Closed ColCh closed 9 years ago
Any ideas how revert is now working?
Hm, wrote small workaroud for a while:
var rewireReplace = function (module, replaces) {
var reverts = {};
Object.keys(replaces).forEach(function (key) {
reverts[key] = module.__get__(key);
module.__set__(key, replaces[key]);
});
return function revert () {
Object.keys(reverts).forEach(function (key) {
module.__set__(key, reverts[key]);
});
};
};
Usage:
var revertRewire;
before(function () {
revertRewire = rewireReplace(modelsActions, {
'Product.findAll': fakeFindAll
});
});
after(function () {
revertRewire();
});
Should be fixed with 2.1.5 :grinning:
Thanks.
Partially :)
Works:
var revertRewire;
before(function () {
revertRewire = modelsActions.__set__('Product.findAll', fakeFindAll);
});
after(function () {
revertRewire();
});
Also this way it works too (I think it's a nice way to inject mocks):
var revertRewire;
before(function () {
revertRewire = modelsActions.__set__({
Product: {
findAll: fakeFindAll
}
});
});
after(function () {
revertRewire();
});
And this - nope:
var revertRewire;
before(function () {
revertRewire = modelsActions.__set__({
'Product.findAll': fakeFindAll
});
});
after(function () {
revertRewire();
});
So, I really like this way
revertRewire = modelsActions.__set__({
Product: {
findAll: fakeFindAll
}
revertRewire = modelsActions.__set__({
'Product.findAll': fakeFindAll
});
This syntax was never intended to work :grin:. But it is possible to implement, so I added it with v2.2.0
Thank you! :+1:
I have small test case... I want to replace some methon on global variable which is Sequelize model:
But it throws:
If I get variable and then set it back, everything works like expected.
It's a bug or I'm using it wrong?