Skip to content
Browse files

added api tests to samples and a .jshintrc

  • Loading branch information...
1 parent 90f1982 commit 3c816ab6381a13401e6ee6f6163b9310ac78da8c Thorsten Lorenz committed
Showing with 198 additions and 3 deletions.
  1. +6 −0 .jshintrc
  2. +124 −0 examples/api/api-test.js
  3. +10 −0 examples/api/samples/bar.js
  4. +31 −0 examples/api/samples/foo.js
  5. +5 −0 examples/api/samples/stats.js
  6. +22 −3 proxyquire.js
View
6 .jshintrc
@@ -0,0 +1,6 @@
+{
+ "laxcomma" : true
+ , "sub" : true
+ , "onecase" : true
+ , "node" : true
+}
View
124 examples/api/api-test.js
@@ -0,0 +1,124 @@
+"use strict";
+
+var assert = require('assert')
+ , stats = require('./samples/stats')
+ , proxyquire = require('../../proxyquire')
+ , file = '/some/path/test.ext'
+ , foo
+ , fooCut
+ , fooWild
+ , cutBarStub = { bar: function () { return 'barber'; } }
+ , wildBarStub = { bar: function () { return 'barbar'; } }
+ ;
+
+foo = proxyquire.resolve('./samples/foo', __dirname);
+fooCut = proxyquire.resolve('./samples/foo', __dirname, { './bar': cutBarStub });
+fooWild = proxyquire.resolve('./samples/foo', __dirname, { './bar': wildBarStub });
+
+assert.equal(stats.fooRequires(), 3);
+
+assert.equal(foo.bigBar() , 'BAR');
+assert.equal(fooCut.bigBar() , 'BARBER');
+assert.equal(fooWild.bigBar() , 'BARBAR');
+
+// non overriden keys call thru by default
+assert.equal(foo.bigRab() , 'RAB');
+assert.equal(fooCut.bigRab() , 'RAB');
+
+// non overridden module path untouched
+assert.equal(foo.bigExt(file) , '.EXT');
+assert.equal(fooCut.bigExt(file) , '.EXT');
+assert.equal(fooWild.bigExt(file) , '.EXT');
+assert.equal(foo.bigBas(file) , 'TEST.EXT');
+assert.equal(fooCut.bigBas(file) , 'TEST.EXT');
+assert.equal(fooWild.bigBas(file) , 'TEST.EXT');
+
+// overriding keys after require works for both inline and non inline requires
+cutBarStub.bar = function () { return 'friseur'; };
+cutBarStub.rab = function () { return 'rabarber'; };
+
+assert.equal(fooCut.bigBar(), 'FRISEUR');
+assert.equal(fooCut.bigRab(), 'RABARBER');
+
+// autofilling keys on delete only works for inline requires
+cutBarStub.bar = undefined;
+assert.equal(fooCut.bigBar(), 'BAR');
+
+cutBarStub.rab = undefined;
+assert.throws(fooCut.bigRab);
+
+
+// turn off callThru feature via noCallThru
+// not turned off
+foo = proxyquire
+ .resolve('./samples/foo', __dirname, {
+ path: {
+ extname: function (file) { return 'Exterminate, exterminate the ' + file; }
+ }
+ });
+
+assert.equal(foo.bigExt(file), 'EXTERMINATE, EXTERMINATE THE /SOME/PATH/TEST.EXT');
+assert.equal(foo.bigBas(file), 'TEST.EXT');
+
+// turned off
+foo = proxyquire
+ .resolve('./samples/foo', __dirname, {
+ path: {
+ extname: function (file) { return 'Exterminate, exterminate the ' + file; }
+ , '@noCallThru': true
+ }
+ });
+
+assert.equal(foo.bigExt(file), 'EXTERMINATE, EXTERMINATE THE /SOME/PATH/TEST.EXT');
+assert.throws(foo.bigBas);
+
+// turned off globally
+// not turned back on per module
+
+foo = proxyquire
+ .noCallThru()
+ .resolve('./samples/foo', __dirname, {
+ path: {
+ extname: function (file) { return 'Exterminate, exterminate the ' + file; }
+ }
+ });
+
+assert.throws(foo.bigBas);
+
+// turned back on per module
+
+foo = proxyquire
+ .noCallThru()
+ .resolve('./samples/foo', __dirname, {
+ path: {
+ extname: function (file) { return 'Exterminate, exterminate the ' + file; }
+ , '@noCallThru': false
+ }
+ });
+
+assert.equal(foo.bigBas(file), 'TEST.EXT');
+
+// turned back on globally
+
+foo = proxyquire
+ .noCallThru(false)
+ .resolve('./samples/foo', __dirname, {
+ path: {
+ extname: function (file) { return 'Exterminate, exterminate the ' + file; }
+ }
+ });
+
+assert.equal(foo.bigBas(file), 'TEST.EXT');
+
+// turned back off per module
+
+foo = proxyquire
+ .noCallThru(false)
+ .resolve('./samples/foo', __dirname, {
+ path: {
+ extname: function (file) { return 'Exterminate, exterminate the ' + file; }
+ , '@noCallThru': true
+ }
+ });
+
+assert.throws(foo.bigBas);
View
10 examples/api/samples/bar.js
@@ -0,0 +1,10 @@
+function bar () {
+ return 'bar';
+}
+
+function rab () {
+ return 'rab';
+}
+
+module.exports = { bar : bar, rab: rab };
+
View
31 examples/api/samples/foo.js
@@ -0,0 +1,31 @@
+var stats = require('./stats')
+ , bar = require('./bar')
+ , path = require('path')
+ ;
+
+stats.incFooRequires();
+
+function bigBar () {
+ // inline require
+ return require('./bar').bar().toUpperCase();
+}
+
+function bigRab () {
+ // module wide require
+ return bar.rab().toUpperCase();
+}
+
+function bigExt (file) {
+ return path.extname(file).toUpperCase();
+}
+
+function bigBas (file) {
+ return path.basename(file).toUpperCase();
+}
+
+module.exports = {
+ bigBar: bigBar
+ , bigRab: bigRab
+ , bigExt: bigExt
+ , bigBas: bigBas
+};
View
5 examples/api/samples/stats.js
@@ -0,0 +1,5 @@
+var fooRequires = 0;
+module.exports = {
+ fooRequires: function () { return fooRequires; }
+ , incFooRequires: function () { fooRequires++; }
+};
View
25 proxyquire.js
@@ -5,6 +5,7 @@ var path = require('path')
, registeredStubs = { }
, stubkey = 0
, tmpDir = getTmpDir()
+ , callThru = true
;
@@ -72,6 +73,12 @@ function setTmpDir(tmpdir) {
tmpDir = tmpdir;
}
+function noCallThru(no) {
+ // default to true when 'no' is not supplied
+ callThru = no === false;
+ return module.exports;
+}
+
function proxyquire (mdl, proxy__filename, original__dirname) {
var mdlResolve = isRelativePath(mdl) ? path.join(original__dirname, mdl) : mdl
, original = require(mdlResolve)
@@ -108,7 +115,17 @@ function resolve (mdl, test__dirname, stubs) {
, dependency
;
- if (stubs) registeredStubs[resolvedProxy] = stubs;
+ if (stubs) {
+ // Adjust no call thru settings for each stubbed module if it was overridden globally
+ if (!callThru) {
+ Object.keys(stubs).forEach(function (key) {
+ // allow turning call thru back on per module by setting it to false
+ if (stubs[key]['@noCallThru'] !== false) stubs[key]['@noCallThru'] = true;
+ });
+ }
+
+ registeredStubs[resolvedProxy] = stubs;
+ }
fs.writeFileSync(resolvedProxy, mdlProxyCode);
@@ -127,6 +144,8 @@ function resolve (mdl, test__dirname, stubs) {
}
module.exports = {
- resolve: resolve
- , require: proxyquire
+ resolve : resolve
+ , require : proxyquire
+ , tmpDir : setTmpDir
+ , noCallThru : noCallThru
};

0 comments on commit 3c816ab

Please sign in to comment.
Something went wrong with that request. Please try again.