Skip to content

Instantly share code, notes, and snippets.

@ullmark
Last active December 17, 2015 13:39
Show Gist options
  • Save ullmark/5618429 to your computer and use it in GitHub Desktop.
Save ullmark/5618429 to your computer and use it in GitHub Desktop.
describe("MyTest", function() {
var Subject;
beforeEach(function(done) {
this.stub({ "stubbed_module": {});
this.require(["module_to_test"], function(s) {
Subject = s;
done();
});
});
it("test it");
})
beforeEach(function() {
this.req = null;
this.map = {};
this.stubs = {};
this.stub = function(stubs) {
var self = this;
for (var key in stubs) {
var stubModule = "stub_" + key;
this.map[key] = stubModule;
this.stubs[stubModule] = stubs[key];
if (require.defined(stubModule)) {
require.undef(stubModule);
}
define(stubModule, function() {
return self.stubs[stubModule];
});
}
};
// ### require
// this function provides a local "require" that is unique
// for each test. Meaning that you can provides
this.require = function() {
var self = this;
var deps = arguments[0];
var callback = arguments[1];
if (!this.req) {
// if require hasn't been used for this test
// context yet, create it.
var context = Math.floor(Math.random() * 1000000);
this.req = require.config({
baseUrl: '/lib/app',
context: context,
map: {
'*': self.map
}
});
}
this.req(deps, function() {
var args = arguments;
callback.apply(self, arguments);
});
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment