Skip to content

Instantly share code, notes, and snippets.

@Overdrivr
Last active Aug 2, 2016
Embed
What would you like to do?
Mockery useCleanCache gotcha
module.exports = function hello() {
console.log("Byebye");
}
module.exports = function hello() {
console.log("Hello");
}
var mockery = require('mockery-next');
var foo = require('./foo');
foo();
mockery.registerSubstitute('./foo', "./bar");
mockery.enable({
useCleanCache: true,
warnOnUnregistered: false
});
// foo = require('./foo');
foo();
mockery.disable();
foo();

With line 12 commented

Hello
Hello
Hello

With line 12 uncommented

Hello
Byebye
Byebye

TL;DR

useCleanCache is no magic option. Unless a specific call to require is made again, the module will still point to its former version rather than throwing an error.

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