Skip to content

Instantly share code, notes, and snippets.

@Overdrivr
Last active August 2, 2016 13:38
Show Gist options
  • Save Overdrivr/e1f96930ef4f1431202a2612e181611f to your computer and use it in GitHub Desktop.
Save Overdrivr/e1f96930ef4f1431202a2612e181611f to your computer and use it in GitHub Desktop.
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