Skip to content

Commit

Permalink
Call require("mock-modules").register in every mockable module.
Browse files Browse the repository at this point in the history
Mocking happens only when config.constants.__MOCK__ is true.
  • Loading branch information
benjamn committed Jul 15, 2013
1 parent c6c4657 commit 37014e1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
20 changes: 19 additions & 1 deletion bin/jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,23 @@ require("commoner").resolve(function(id) {

// Constant propagation means removing any obviously dead code after
// replacing constant expressions with literal (boolean) values.
return propagate(constants, source);
source = propagate(constants, source);

if (constants.__MOCK__) {
// Make sure there is exactly one newline at the end of the module.
source = source.replace(/\s+$/m, "\n");

return context.getProvidedP().then(function(idToPath) {
if (id !== "mock-modules" &&
id !== "mocks" &&
idToPath.hasOwnProperty("mock-modules")) {
return source + '\nrequire("mock-modules").register(' +
JSON.stringify(id) + ', module);\n';
}

return source;
});
}

return source;
});
1 change: 1 addition & 0 deletions grunt/config/jsx/debug.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"debug": true,
"constants": {
"__MOCK__": true,
"__DEV__": true
}
}
8 changes: 6 additions & 2 deletions src/test/mock-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
* @providesModule mock-modules
*/

var global = Function("return this")();
require('test/mock-timers').installMockTimers(global);
exports.register = function(id, module) {
// TODO
};

exports.dumpCache = function() {
require("mocks").clear();
Expand All @@ -31,3 +32,6 @@ exports.dontMock = function() {
exports.mock = function() {
return exports;
};

var global = Function("return this")();
require('test/mock-timers').installMockTimers(global);

0 comments on commit 37014e1

Please sign in to comment.