Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active February 3, 2018 04:55
Show Gist options
  • Save Integralist/8400550 to your computer and use it in GitHub Desktop.
Save Integralist/8400550 to your computer and use it in GitHub Desktop.
Better Mocking using RequireJS' `undef` method to unset redefined modules
define(['require'], function(require) {
var stubbed = [];
return {
stub: function(name, implementation) {
stubbed.push(name);
requirejs.undef(name);
define(name, [], function() {
return implementation;
});
},
loadWithCurrentStubs: function(name, callback) {
requirejs.undef(name);
stubbed.push(name);
require([name], callback);
},
reset: function() {
stubbed.forEach(function(name) {
requirejs.undef(name);
});
stubbed = [];
}
};
});
beforeEach(function () {
DependencyHelper.stub('name', implementation);
DependencyHelper.stub('deviceInspector', {
getGroup: function () {},
getType: function () {}
});
DependencyHelper.loadWithCurrentStubs('module/base', function (base) {
// code
});
});
afterEach(function () {
DependencyHelper.reset();
});
@RLovelett
Copy link

Was this running against a specific version of RequireJS? When I run this against the latest version (2.1.11) I get an error: Error: Module name "module" has not been loaded yet for context: _.

Suggestions?

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