Permalink
Browse files

Added global vars (issue #3)

Also fixes issue #4 as a side effect, as suspected
  • Loading branch information...
1 parent 965fa97 commit cf11c3ae2ea78a07c2bd5fac7a7b2c238af6a09d @nathanmacinnes committed Feb 2, 2012
Showing with 43 additions and 1 deletion.
  1. +15 −0 lib/injectr.js
  2. +1 −1 package.json
  3. +1 −0 test/other-globals.js
  4. +9 −0 test/pretend-scripts/globals.js
  5. +17 −0 test/tests.js
View
@@ -16,6 +16,21 @@ module.exports = function (file, mocks, all) {
// include the exports variable, so user can use this as an alternative to
// module.exports
context.exports = context.module.exports;
+
+ // include timer variables
+ context.setTimeout = setTimeout;
+ context.clearTimeout = clearTimeout;
+ context.setInterval = setInterval;
+ context.clearInterval = clearInterval;
+
+ // include other globals, which also helps with errors
+ context.console = console;
+ context.process = process;
+ context.global = global;
+
+ // sort out the __filename and __dirname
+ context.__filename = path.resolve(file);
+ context.__dirname = path.dirname(context.__filename);
// mocks is an optional parameter
// not including it makes injectr like a sandbox wrapper
View
@@ -1,6 +1,6 @@
{
"name" : "injectr",
- "version" : "0.2.0",
+ "version" : "0.2.1",
"description" : "Finally, a solution to node.js dependency injection",
"keywords" : [
"dependency",
View
@@ -0,0 +1 @@
+exports.st = setTimeout.toString();
@@ -0,0 +1,9 @@
+exports.setTimeout = setTimeout;
+exports.clearTimeout = clearTimeout;
+exports.setInterval = setInterval;
+exports.clearInterval = clearInterval;
+exports.console = console;
+exports.process = process;
+exports.global = global;
+exports.filename = __filename;
+exports.dirname = __dirname;
View
@@ -39,3 +39,20 @@ test("can get local variables", function () {
"exported values now available under .module.exports");
equal(exportTest.localVar, 'local');
});
+test("has the appropriate global variables", function () {
+ var globals = injectr('./test/pretend-scripts/globals.js');
+ equal(globals.setTimeout, setTimeout);
+ equal(globals.clearTimeout, clearTimeout);
+ equal(globals.setInterval, setInterval);
+ equal(globals.clearInterval, clearInterval);
+ equal(globals.console, console);
+ equal(globals.process.argv, process.argv);
+ equal(globals.process.pid, process.pid);
+ equal(globals.global.urlParams, global.urlParams);
+ equal(globals.global.isLocal, global.isLocal);
+});
+test("has __filename and __dirname values", function () {
+ var globals = injectr('./test/pretend-scripts/globals.js');
+ equal(globals.dirname, __dirname + '/pretend-scripts');
+ equal(globals.filename, __dirname + '/pretend-scripts/globals.js');
+});

0 comments on commit cf11c3a

Please sign in to comment.