Last active
December 17, 2015 13:39
-
-
Save ullmark/5618429 to your computer and use it in GitHub Desktop.
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
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"); | |
}) |
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() { | |
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