Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added global vars (issue #3)

Also fixes issue #4 as a side effect, as suspected
  • Loading branch information...
commit cf11c3ae2ea78a07c2bd5fac7a7b2c238af6a09d 1 parent 965fa97
@nathanmacinnes authored
View
15 lib/injectr.js
@@ -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
2  package.json
@@ -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
1  test/other-globals.js
@@ -0,0 +1 @@
+exports.st = setTimeout.toString();
View
9 test/pretend-scripts/globals.js
@@ -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
17 test/tests.js
@@ -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');
+});
Please sign in to comment.
Something went wrong with that request. Please try again.