Last active
February 3, 2018 04:55
-
-
Save Integralist/8400550 to your computer and use it in GitHub Desktop.
Better Mocking using RequireJS' `undef` method to unset redefined modules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = []; | |
} | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
beforeEach(function () { | |
DependencyHelper.stub('name', implementation); | |
DependencyHelper.stub('deviceInspector', { | |
getGroup: function () {}, | |
getType: function () {} | |
}); | |
DependencyHelper.loadWithCurrentStubs('module/base', function (base) { | |
// code | |
}); | |
}); | |
afterEach(function () { | |
DependencyHelper.reset(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?