proxyquire
Advanced tools
Comparing version 1.8.0 to 2.0.0
@@ -1,72 +0,70 @@ | ||
"use strict"; | ||
'use strict' | ||
var assert = require('assert') | ||
, stats = require('./samples/stats') | ||
, proxyquire = require('../..') | ||
, file = '/some/path/test.ext' | ||
, foo | ||
, fooCut | ||
, fooWild | ||
, cutBarStub = { bar: function () { return 'barber'; } } | ||
, wildBarStub = { bar: function () { return 'barbar'; } } | ||
; | ||
foo = proxyquire('./samples/foo', { }); | ||
fooCut = proxyquire('./samples/foo', { './bar': cutBarStub }); | ||
fooWild = proxyquire('./samples/foo', { './bar': wildBarStub }); | ||
var stats = require('./samples/stats') | ||
var proxyquire = require('../..') | ||
var file = '/some/path/test.ext' | ||
var foo | ||
var fooCut | ||
var fooWild | ||
var cutBarStub = { bar: function () { return 'barber' } } | ||
var wildBarStub = { bar: function () { return 'barbar' } } | ||
assert.equal(stats.fooRequires(), 3); | ||
foo = proxyquire('./samples/foo', { }) | ||
fooCut = proxyquire('./samples/foo', { './bar': cutBarStub }) | ||
fooWild = proxyquire('./samples/foo', { './bar': wildBarStub }) | ||
assert.equal(foo.bigBar() , 'BAR'); | ||
assert.equal(fooCut.bigBar() , 'BARBER'); | ||
assert.equal(fooWild.bigBar() , 'BARBAR'); | ||
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'); | ||
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'); | ||
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'; }; | ||
cutBarStub.bar = function () { return 'friseur' } | ||
cutBarStub.rab = function () { return 'rabarber' } | ||
assert.equal(fooCut.bigBar(), 'FRISEUR'); | ||
assert.equal(fooCut.bigRab(), '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.bar = undefined | ||
assert.equal(fooCut.bigBar(), 'BAR') | ||
cutBarStub.rab = undefined; | ||
assert.throws(fooCut.bigRab); | ||
cutBarStub.rab = undefined | ||
assert.throws(fooCut.bigRab) | ||
// turn off callThru feature via noCallThru | ||
// not turned off | ||
foo = proxyquire('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file; } | ||
} | ||
}); | ||
foo = proxyquire('./samples/foo', { | ||
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'); | ||
assert.equal(foo.bigExt(file), 'EXTERMINATE, EXTERMINATE THE /SOME/PATH/TEST.EXT') | ||
assert.equal(foo.bigBas(file), 'TEST.EXT') | ||
// turned off | ||
foo = proxyquire('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file; } | ||
, '@noCallThru': true | ||
} | ||
}); | ||
foo = proxyquire('./samples/foo', { | ||
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); | ||
assert.equal(foo.bigExt(file), 'EXTERMINATE, EXTERMINATE THE /SOME/PATH/TEST.EXT') | ||
assert.throws(foo.bigBas) | ||
@@ -78,9 +76,9 @@ // turned off globally | ||
.noCallThru() | ||
.load('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file; } | ||
} | ||
}); | ||
.load('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file } | ||
} | ||
}) | ||
assert.throws(foo.bigBas); | ||
assert.throws(foo.bigBas) | ||
@@ -91,10 +89,10 @@ // turned back on per module | ||
.noCallThru() | ||
.load('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file; } | ||
, '@noCallThru': false | ||
} | ||
}); | ||
.load('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file }, | ||
'@noCallThru': false | ||
} | ||
}) | ||
assert.equal(foo.bigBas(file), 'TEST.EXT'); | ||
assert.equal(foo.bigBas(file), 'TEST.EXT') | ||
@@ -105,9 +103,9 @@ // turned back on globally | ||
.callThru() | ||
.load('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file; } | ||
} | ||
}); | ||
.load('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file } | ||
} | ||
}) | ||
assert.equal(foo.bigBas(file), 'TEST.EXT'); | ||
assert.equal(foo.bigBas(file), 'TEST.EXT') | ||
@@ -118,11 +116,11 @@ // turned back off per module | ||
.callThru() | ||
.load('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file; } | ||
, '@noCallThru': true | ||
} | ||
}); | ||
.load('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file }, | ||
'@noCallThru': true | ||
} | ||
}) | ||
assert.throws(foo.bigBas); | ||
assert.throws(foo.bigBas) | ||
console.log('*** All Asserts passed ***'); | ||
console.log('*** All Asserts passed ***') |
function bar () { | ||
return 'bar'; | ||
return 'bar' | ||
} | ||
function rab () { | ||
return 'rab'; | ||
return 'rab' | ||
} | ||
module.exports = { bar : bar, rab: rab }; | ||
module.exports = { bar: bar, rab: rab } |
var stats = require('./stats') | ||
, bar = require('./bar') | ||
, path = require('path') | ||
; | ||
var bar = require('./bar') | ||
var path = require('path') | ||
stats.incFooRequires(); | ||
stats.incFooRequires() | ||
function bigBar () { | ||
function bigBar () { | ||
// inline require | ||
return require('./bar').bar().toUpperCase(); | ||
return require('./bar').bar().toUpperCase() | ||
} | ||
@@ -15,18 +14,18 @@ | ||
// module wide require | ||
return bar.rab().toUpperCase(); | ||
return bar.rab().toUpperCase() | ||
} | ||
function bigExt (file) { | ||
return path.extname(file).toUpperCase(); | ||
return path.extname(file).toUpperCase() | ||
} | ||
function bigBas (file) { | ||
return path.basename(file).toUpperCase(); | ||
return path.basename(file).toUpperCase() | ||
} | ||
module.exports = { | ||
bigBar: bigBar | ||
, bigRab: bigRab | ||
, bigExt: bigExt | ||
, bigBas: bigBas | ||
}; | ||
bigBar: bigBar, | ||
bigRab: bigRab, | ||
bigExt: bigExt, | ||
bigBas: bigBas | ||
} |
@@ -1,5 +0,5 @@ | ||
var fooRequires = 0; | ||
var fooRequires = 0 | ||
module.exports = { | ||
fooRequires: function () { return fooRequires; } | ||
, incFooRequires: function () { fooRequires++; } | ||
}; | ||
fooRequires: function () { return fooRequires }, | ||
incFooRequires: function () { fooRequires++ } | ||
} |
@@ -1,16 +0,17 @@ | ||
"use strict"; | ||
'use strict' | ||
require('../example-utils').listModuleAndTests(__dirname + '/foo.js', __filename); | ||
var path = require('path') | ||
require('../example-utils').listModuleAndTests(path.resolve(__dirname, '/foo.js'), __filename) | ||
// Overriding callbacks that would normally be async will cause them to call back immediately | ||
// Thus allowing you to run synchronous tests against async APIs. | ||
var proxyquire = require('../..') | ||
, assert = require('assert') | ||
, readdirError = new Error('some error') | ||
, fsStub = { } | ||
, calledBack | ||
; | ||
var proxyquire = require('../..') | ||
var assert = require('assert') | ||
var readdirError = new Error('some error') | ||
var fsStub = { } | ||
var calledBack | ||
var foo = proxyquire('./foo', { fs: fsStub }); | ||
var foo = proxyquire('./foo', { fs: fsStub }) | ||
@@ -20,15 +21,15 @@ /* | ||
*/ | ||
fsStub.readdir = function (dir, cb) { cb(null, [ 'file1', 'file2' ]); }; | ||
fsStub.readdir = function (dir, cb) { cb(null, [ 'file1', 'file2' ]) } | ||
calledBack = false; | ||
calledBack = false | ||
foo.filesAllCaps('./somedir', function (err, files) { | ||
assert.equal(err, null); | ||
assert.equal(files[0], 'FILE1'); | ||
assert.equal(files[1], 'FILE2'); | ||
calledBack = true; | ||
}); | ||
assert.equal(err, null) | ||
assert.equal(files[0], 'FILE1') | ||
assert.equal(files[1], 'FILE2') | ||
calledBack = true | ||
}) | ||
// fs.readdir and thus filesAllCaps calls back before we get here, which means the code ran synchronously | ||
assert(calledBack); | ||
assert(calledBack) | ||
@@ -38,8 +39,8 @@ /* | ||
*/ | ||
fsStub.readdir = function (dir, cb) { cb(readdirError); }; | ||
fsStub.readdir = function (dir, cb) { cb(readdirError) } | ||
foo.filesAllCaps('./somedir', function (err, files) { | ||
assert.equal(err, readdirError); | ||
}); | ||
assert.equal(err, readdirError) | ||
}) | ||
console.log('*** All asserts passed ***'); | ||
console.log('*** All asserts passed ***') |
@@ -1,11 +0,13 @@ | ||
var fs = require('fs'); | ||
var fs = require('fs') | ||
module.exports.filesAllCaps = function (dir, cb) { | ||
module.exports.filesAllCaps = function (dir, cb) { | ||
fs.readdir(dir, function (err, files) { | ||
if (err) cb(err); | ||
else cb ( | ||
if (err) cb(err) | ||
else { | ||
cb( | ||
null | ||
, files.map(function (f) { return f.toUpperCase(); }) | ||
); | ||
}); | ||
}; | ||
, files.map(function (f) { return f.toUpperCase() }) | ||
) | ||
} | ||
}) | ||
} |
@@ -1,19 +0,17 @@ | ||
var fs = require('fs'); | ||
var fs = require('fs') | ||
module.exports.listModuleAndTests = function (module, tests) { | ||
console.log( | ||
'\n**********\n' + | ||
'* Module:*\n' + | ||
'* Module:*\n' + | ||
'**********\n\n' + | ||
fs.readFileSync(module).toString() | ||
); | ||
) | ||
console.log( | ||
'**********\n' + | ||
'* Tests: *\n' + | ||
'* Tests: *\n' + | ||
'**********\n' + | ||
fs.readFileSync(tests).toString() | ||
); | ||
}; | ||
fs.readFileSync(tests).toString() | ||
) | ||
} |
@@ -1,24 +0,26 @@ | ||
'use strict'; | ||
require('../example-utils').listModuleAndTests(__dirname + '/foo.js', __filename); | ||
'use strict' | ||
var path = require('path') | ||
require('../example-utils').listModuleAndTests(path.resolve(__dirname, '/foo.js'), __filename) | ||
var proxyquire = require('../..') | ||
, assert = require('assert') | ||
, foo | ||
; | ||
var assert = require('assert') | ||
var foo | ||
// no overrides yet, so path.extname behaves normally | ||
foo = proxyquire('./foo', {}); | ||
assert.equal(foo.extnameAllCaps('file.txt'), '.TXT'); | ||
foo = proxyquire('./foo', {}) | ||
assert.equal(foo.extnameAllCaps('file.txt'), '.TXT') | ||
// override path.extname | ||
foo = proxyquire('./foo', { | ||
path: { extname: function (file) { return 'Exterminate, exterminate the ' + file; } } | ||
}); | ||
path: { extname: function (file) { return 'Exterminate, exterminate the ' + file } } | ||
}) | ||
// path.extname now behaves as we told it to | ||
assert.equal(foo.extnameAllCaps('file.txt'), 'EXTERMINATE, EXTERMINATE THE FILE.TXT'); | ||
assert.equal(foo.extnameAllCaps('file.txt'), 'EXTERMINATE, EXTERMINATE THE FILE.TXT') | ||
// path.basename on the other hand still functions as before | ||
assert.equal(foo.basenameAllCaps('/a/b/file.txt'), 'FILE.TXT'); | ||
assert.equal(foo.basenameAllCaps('/a/b/file.txt'), 'FILE.TXT') | ||
console.log('*** All asserts passed ***'); | ||
console.log('*** All asserts passed ***') |
@@ -1,9 +0,9 @@ | ||
var path = require('path'); | ||
var path = require('path') | ||
module.exports.extnameAllCaps = function (file) { | ||
return path.extname(file).toUpperCase(); | ||
}; | ||
module.exports.extnameAllCaps = function (file) { | ||
return path.extname(file).toUpperCase() | ||
} | ||
module.exports.basenameAllCaps = function (file) { | ||
return path.basename(file).toUpperCase(); | ||
}; | ||
module.exports.basenameAllCaps = function (file) { | ||
return path.basename(file).toUpperCase() | ||
} |
@@ -1,22 +0,24 @@ | ||
"use strict"; | ||
'use strict' | ||
require('../example-utils').listModuleAndTests(__dirname + '/foo.js', __filename); | ||
var path = require('path') | ||
var proxyquire = require('../..') | ||
, assert = require('assert') | ||
, pathStub = { }; | ||
require('../example-utils').listModuleAndTests(path.resolve(__dirname, '/foo.js'), __filename) | ||
var proxyquire = require('../..') | ||
var assert = require('assert') | ||
var pathStub = { } | ||
// when not overridden, path.extname behaves normally | ||
var foo = proxyquire('./foo', { 'path': pathStub }); | ||
assert.equal(foo.extnameAllCaps('file.txt'), '.TXT'); | ||
var foo = proxyquire('./foo', { 'path': pathStub }) | ||
assert.equal(foo.extnameAllCaps('file.txt'), '.TXT') | ||
// override path.extname | ||
pathStub.extname = function (file) { return 'Exterminate, exterminate the ' + file; }; | ||
pathStub.extname = function (file) { return 'Exterminate, exterminate the ' + file } | ||
// path.extname now behaves as we told it to | ||
assert.equal(foo.extnameAllCaps('file.txt'), 'EXTERMINATE, EXTERMINATE THE FILE.TXT'); | ||
assert.equal(foo.extnameAllCaps('file.txt'), 'EXTERMINATE, EXTERMINATE THE FILE.TXT') | ||
// path.basename and all other path module methods still function as before | ||
assert.equal(foo.basenameAllCaps('/a/b/file.txt'), 'FILE.TXT'); | ||
assert.equal(foo.basenameAllCaps('/a/b/file.txt'), 'FILE.TXT') | ||
console.log('*** All asserts passed ***'); | ||
console.log('*** All asserts passed ***') |
@@ -1,5 +0,5 @@ | ||
"use strict"; | ||
/*jshint asi:true*/ | ||
/*global describe before beforeEach after afterEach it */ | ||
'use strict' | ||
/* global describe before after it */ | ||
/* | ||
@@ -11,9 +11,8 @@ * These examples demonstrate how to use proxyquire with Sinon.JS (<http://sinonjs.org/>). | ||
var proxyquire = require('../..') | ||
, sinon = require('sinon') | ||
, assert = require('assert') | ||
, fs = require('fs') | ||
, path = require('path') | ||
, foo | ||
; | ||
var proxyquire = require('../..') | ||
var sinon = require('sinon') | ||
var assert = require('assert') | ||
var fs = require('fs') | ||
var path = require('path') | ||
var foo | ||
@@ -23,17 +22,17 @@ // Stubbing return values | ||
var extnameStub | ||
, file = 'somefile'; | ||
var file = 'somefile' | ||
before(function () { | ||
extnameStub = sinon.stub(path, 'extname'); | ||
foo = proxyquire('./foo', { path: { extname: extnameStub } } ); | ||
extnameStub = sinon.stub(path, 'extname') | ||
foo = proxyquire('./foo', { path: { extname: extnameStub } }) | ||
extnameStub.withArgs(file).returns('.markdown'); | ||
extnameStub.withArgs(file).returns('.markdown') | ||
}) | ||
after(function () { | ||
path.extname.restore(); | ||
}); | ||
path.extname.restore() | ||
}) | ||
it('extnameAllCaps returns ".MARKDOWN"', function () { | ||
assert.equal(foo.extnameAllCaps(file), '.MARKDOWN'); | ||
assert.equal(foo.extnameAllCaps(file), '.MARKDOWN') | ||
}) | ||
@@ -44,22 +43,22 @@ }) | ||
describe('when fs.readdir calls back with ["file1", "file2"]', function () { | ||
var readdirStub; | ||
var readdirStub | ||
before(function () { | ||
readdirStub = sinon.stub(fs, 'readdir'); | ||
foo = proxyquire('./foo', { fs: { readdir: readdirStub } } ); | ||
readdirStub = sinon.stub(fs, 'readdir') | ||
foo = proxyquire('./foo', { fs: { readdir: readdirStub } }) | ||
readdirStub.withArgs('../simple').yields(null, [ 'file1', 'file2' ]); | ||
}) | ||
readdirStub.withArgs('../simple').yields(null, [ 'file1', 'file2' ]) | ||
}) | ||
after(function () { | ||
fs.readdir.restore(); | ||
}); | ||
fs.readdir.restore() | ||
}) | ||
it('filesAllCaps calls back with ["FILE1", "FILE2"]', function (done) { | ||
foo.filesAllCaps('../simple', function (err, files) { | ||
assert.equal(err, null); | ||
assert.equal(files[0], 'FILE1'); | ||
assert.equal(files[1], 'FILE2'); | ||
done(); | ||
}); | ||
assert.equal(err, null) | ||
assert.equal(files[0], 'FILE1') | ||
assert.equal(files[1], 'FILE2') | ||
done() | ||
}) | ||
}) | ||
@@ -69,23 +68,23 @@ }) | ||
describe('when fs.readdir returns an error', function () { | ||
var readdirError | ||
, readdirStub; | ||
var readdirError, | ||
readdirStub | ||
before(function () { | ||
readdirStub = sinon.stub(fs, 'readdir'); | ||
foo = proxyquire('./foo', { fs: { readdir: readdirStub } } ); | ||
readdirStub = sinon.stub(fs, 'readdir') | ||
foo = proxyquire('./foo', { fs: { readdir: readdirStub } }) | ||
readdirError = new Error('some error'); | ||
readdirStub.withArgs('../simple').yields(readdirError, null); | ||
}) | ||
readdirError = new Error('some error') | ||
readdirStub.withArgs('../simple').yields(readdirError, null) | ||
}) | ||
after(function () { | ||
fs.readdir.restore(); | ||
}); | ||
fs.readdir.restore() | ||
}) | ||
it('filesAllCaps calls back with that error', function (done) { | ||
foo.filesAllCaps('../simple', function (err, files) { | ||
assert.equal(err, readdirError); | ||
assert.equal(files, null); | ||
done(); | ||
}); | ||
assert.equal(err, readdirError) | ||
assert.equal(files, null) | ||
done() | ||
}) | ||
}) | ||
@@ -96,21 +95,21 @@ }) | ||
describe('when calling filesAllCaps with "../simple"', function () { | ||
var readdirSpy; | ||
var readdirSpy | ||
before(function () { | ||
readdirSpy = sinon.spy(fs, 'readdir'); | ||
foo = proxyquire('./foo', { fs: { readdir: readdirSpy } } ); | ||
readdirSpy = sinon.spy(fs, 'readdir') | ||
foo = proxyquire('./foo', { fs: { readdir: readdirSpy } }) | ||
}) | ||
after(function () { | ||
fs.readdir.restore(); | ||
}); | ||
fs.readdir.restore() | ||
}) | ||
it('calls fs.readdir with "../simple"', function (done) { | ||
foo.filesAllCaps('../simple', function (err, files) { | ||
assert(fs.readdir.calledOnce); | ||
assert.equal(fs.readdir.getCall(0).args[0], '../simple'); | ||
done(); | ||
}); | ||
assert.ifError(err) | ||
assert(fs.readdir.calledOnce) | ||
assert.equal(fs.readdir.getCall(0).args[0], '../simple') | ||
done() | ||
}) | ||
}) | ||
}) |
var fs = require('fs') | ||
, path = require('path'); | ||
var path = require('path') | ||
module.exports.filesAllCaps = function (dir, cb) { | ||
module.exports.filesAllCaps = function (dir, cb) { | ||
fs.readdir(dir, function (err, files) { | ||
if (err) cb(err); | ||
else cb ( | ||
if (err) cb(err) | ||
else { | ||
cb( | ||
null | ||
, files.map(function (f) { return f.toUpperCase(); }) | ||
); | ||
}); | ||
}; | ||
, files.map(function (f) { return f.toUpperCase() }) | ||
) | ||
} | ||
}) | ||
} | ||
module.exports.extnameAllCaps = function (file) { | ||
return path.extname(file).toUpperCase(); | ||
}; | ||
module.exports.extnameAllCaps = function (file) { | ||
return path.extname(file).toUpperCase() | ||
} |
14
index.js
@@ -1,11 +0,11 @@ | ||
'use strict'; | ||
'use strict' | ||
var Proxyquire = require('./lib/proxyquire'); | ||
var Proxyquire = require('./lib/proxyquire') | ||
// delete this module from the cache to force re-require in order to allow resolving test module via parent.module | ||
delete require.cache[require.resolve(__filename)]; | ||
delete require.cache[require.resolve(__filename)] | ||
module.exports = new Proxyquire(module.parent); | ||
module.exports.compat = function() { | ||
throw new Error("Proxyquire compat mode has been removed. Please update your code to use the new API or pin the version in your package.json file to ~0.6"); | ||
}; | ||
module.exports = new Proxyquire(module.parent) | ||
module.exports.compat = function () { | ||
throw new Error('Proxyquire compat mode has been removed. Please update your code to use the new API or pin the version in your package.json file to ~0.6') | ||
} |
@@ -5,10 +5,11 @@ var is = {}; | ||
is[name] = function (obj) { | ||
return Object.prototype.toString.call(obj) == '[object ' + name + ']'; | ||
}; | ||
}); | ||
return Object.prototype.toString.call(obj) === '[object ' + name + ']' | ||
} | ||
}) | ||
is.Object = function (obj) { | ||
return obj === new Object(obj); | ||
}; | ||
/* eslint no-new-object: "off" */ | ||
return obj === new Object(obj) | ||
} | ||
module.exports = is; | ||
module.exports = is |
@@ -1,13 +0,13 @@ | ||
'use strict'; | ||
'use strict' | ||
var util = require('util'); | ||
var util = require('util') | ||
function ProxyquireError(msg) { | ||
this.name = 'ProxyquireError'; | ||
Error.captureStackTrace(this, ProxyquireError); | ||
this.message = msg || 'An error occurred inside proxyquire.'; | ||
function ProxyquireError (msg) { | ||
this.name = 'ProxyquireError' | ||
Error.captureStackTrace(this, ProxyquireError) | ||
this.message = msg || 'An error occurred inside proxyquire.' | ||
} | ||
util.inherits(ProxyquireError, Error); | ||
util.inherits(ProxyquireError, Error) | ||
module.exports = ProxyquireError; | ||
module.exports = ProxyquireError |
@@ -1,50 +0,43 @@ | ||
'use strict'; | ||
/*jshint laxbreak:true, loopfunc:true*/ | ||
'use strict' | ||
/* jshint laxbreak:true, loopfunc:true */ | ||
var Module = require('module') | ||
var path = require('path') | ||
, Module = require('module') | ||
, resolve = require('resolve') | ||
, dirname = require('path').dirname | ||
, ProxyquireError = require('./proxyquire-error') | ||
, is = require('./is') | ||
, assert = require('assert') | ||
, fillMissingKeys = require('fill-keys') | ||
, moduleNotFoundError = require('module-not-found-error') | ||
, hasOwnProperty = Object.prototype.hasOwnProperty | ||
; | ||
var resolve = require('resolve') | ||
var ProxyquireError = require('./proxyquire-error') | ||
var is = require('./is') | ||
var assert = require('assert') | ||
var fillMissingKeys = require('fill-keys') | ||
var moduleNotFoundError = require('module-not-found-error') | ||
var hasOwnProperty = Object.prototype.hasOwnProperty | ||
function validateArguments(request, stubs) { | ||
var msg = (function getMessage() { | ||
if (!request) | ||
return 'Missing argument: "request". Need it to resolve desired module.'; | ||
function validateArguments (request, stubs) { | ||
var msg = (function getMessage () { | ||
if (!request) { return 'Missing argument: "request". Need it to resolve desired module.' } | ||
if (!stubs) | ||
return 'Missing argument: "stubs". If no stubbing is needed, use regular require instead.'; | ||
if (!stubs) { return 'Missing argument: "stubs". If no stubbing is needed, use regular require instead.' } | ||
if (!is.String(request)) | ||
return 'Invalid argument: "request". Needs to be a requirable string that is the module to load.'; | ||
if (!is.String(request)) { return 'Invalid argument: "request". Needs to be a requirable string that is the module to load.' } | ||
if (!is.Object(stubs)) | ||
return 'Invalid argument: "stubs". Needs to be an object containing overrides e.g., {"path": { extname: function () { ... } } }.'; | ||
})(); | ||
if (!is.Object(stubs)) { return 'Invalid argument: "stubs". Needs to be an object containing overrides e.g., {"path": { extname: function () { ... } } }.' } | ||
})() | ||
if (msg) throw new ProxyquireError(msg); | ||
if (msg) throw new ProxyquireError(msg) | ||
} | ||
function Proxyquire(parent) { | ||
function Proxyquire (parent) { | ||
var self = this | ||
, fn = self.load.bind(self) | ||
, proto = Proxyquire.prototype | ||
; | ||
var fn = self.load.bind(self) | ||
var proto = Proxyquire.prototype | ||
this._parent = parent; | ||
this._preserveCache = true; | ||
this._parent = parent | ||
this._preserveCache = true | ||
Object.keys(proto) | ||
.forEach(function (key) { | ||
if (is.Function(proto[key])) fn[key] = self[key].bind(self); | ||
}); | ||
if (is.Function(proto[key])) fn[key] = self[key].bind(self) | ||
}) | ||
self.fn = fn; | ||
return fn; | ||
self.fn = fn | ||
return fn | ||
} | ||
@@ -61,5 +54,5 @@ | ||
Proxyquire.prototype.noCallThru = function () { | ||
this._noCallThru = true; | ||
return this.fn; | ||
}; | ||
this._noCallThru = true | ||
return this.fn | ||
} | ||
@@ -75,8 +68,8 @@ /** | ||
Proxyquire.prototype.callThru = function () { | ||
this._noCallThru = false; | ||
return this.fn; | ||
}; | ||
this._noCallThru = false | ||
return this.fn | ||
} | ||
/** | ||
* Will make proxyquire remove the requested modules from the `require.cache` in order to force | ||
* Will make proxyquire remove the requested modules from the `require.cache` in order to force | ||
* them to be reloaded the next time they are proxyquired. | ||
@@ -90,9 +83,9 @@ * This behavior differs from the way nodejs `require` works, but for some tests this maybe useful. | ||
*/ | ||
Proxyquire.prototype.noPreserveCache = function() { | ||
this._preserveCache = false; | ||
return this.fn; | ||
}; | ||
Proxyquire.prototype.noPreserveCache = function () { | ||
this._preserveCache = false | ||
return this.fn | ||
} | ||
/** | ||
* Restores proxyquire caching behavior to match the one of nodejs `require` | ||
* Restores proxyquire caching behavior to match the one of nodejs `require` | ||
* | ||
@@ -104,6 +97,6 @@ * @name preserveCache | ||
*/ | ||
Proxyquire.prototype.preserveCache = function() { | ||
this._preserveCache = true; | ||
return this.fn; | ||
}; | ||
Proxyquire.prototype.preserveCache = function () { | ||
this._preserveCache = true | ||
return this.fn | ||
} | ||
@@ -117,21 +110,21 @@ /** | ||
Proxyquire.prototype.load = function (request, stubs) { | ||
validateArguments(request, stubs); | ||
validateArguments(request, stubs) | ||
// Find out if any of the passed stubs are global overrides | ||
for (var key in stubs) { | ||
var stub = stubs[key]; | ||
var stub = stubs[key] | ||
if (stub === null) continue; | ||
if (stub === null) continue | ||
if (typeof stub === 'undefined') { | ||
throw new ProxyquireError('Invalid stub: "' + key + '" cannot be undefined'); | ||
throw new ProxyquireError('Invalid stub: "' + key + '" cannot be undefined') | ||
} | ||
if (hasOwnProperty.call(stub, '@global')) { | ||
this._containsGlobal = true; | ||
this._containsGlobal = true | ||
} | ||
if (hasOwnProperty.call(stub, '@runtimeGlobal')) { | ||
this._containsGlobal = true; | ||
this._containsRuntimeGlobal = true; | ||
this._containsGlobal = true | ||
this._containsRuntimeGlobal = true | ||
} | ||
@@ -141,25 +134,59 @@ } | ||
// Ignore the module cache when return the requested module | ||
return this._withoutCache(this._parent, stubs, request, this._parent.require.bind(this._parent, request)); | ||
}; | ||
return this._withoutCache(this._parent, stubs, request, this._parent.require.bind(this._parent, request)) | ||
} | ||
// Resolves a stub relative to a module. | ||
// `baseModule` is the module we're resolving from. `pathToResolve` is the | ||
// module we want to resolve (i.e. the string passed to `require()`). | ||
Proxyquire.prototype._resolveModule = function (baseModule, pathToResolve) { | ||
try { | ||
return resolve.sync(pathToResolve, { | ||
basedir: path.dirname(baseModule), | ||
extensions: Object.keys(require.extensions), | ||
paths: Module.globalPaths | ||
}) | ||
} catch (err) { | ||
// If this is not a relative path (e.g. "foo" as opposed to "./foo"), and | ||
// we couldn't resolve it, then we just let the path through unchanged. | ||
// It's safe to do this, because if two different modules require "foo", | ||
// they both expect to get back the same thing. | ||
if (pathToResolve[0] !== '.') { | ||
return pathToResolve | ||
} | ||
// If `pathToResolve` is relative, then it is *not* safe to return it, | ||
// since a file in one directory that requires "./foo" expects to get | ||
// back a different module than one that requires "./foo" from another | ||
// directory. However, if !this._preserveCache, then we don't want to | ||
// throw, since we can resolve modules that don't exist. Resolve as | ||
// best we can. | ||
if (!this._preserveCache) { | ||
return path.resolve(path.dirname(baseModule), pathToResolve) | ||
} | ||
throw err | ||
} | ||
} | ||
// This replaces a module's require function | ||
Proxyquire.prototype._require = function(module, stubs, path) { | ||
assert(typeof path === 'string', 'path must be a string'); | ||
assert(path, 'missing path'); | ||
Proxyquire.prototype._require = function (module, stubs, path) { | ||
assert(typeof path === 'string', 'path must be a string') | ||
assert(path, 'missing path') | ||
if (hasOwnProperty.call(stubs, path)) { | ||
var stub = stubs[path]; | ||
var resolvedPath = this._resolveModule(module.filename, path) | ||
if (hasOwnProperty.call(stubs, resolvedPath)) { | ||
var stub = stubs[resolvedPath] | ||
if (stub === null) { | ||
// Mimic the module-not-found exception thrown by node.js. | ||
throw moduleNotFoundError(path); | ||
throw moduleNotFoundError(path) | ||
} | ||
if (hasOwnProperty.call(stub, '@noCallThru') ? !stub['@noCallThru'] : !this._noCallThru) { | ||
fillMissingKeys(stub, Module._load(path, module)); | ||
fillMissingKeys(stub, Module._load(path, module)) | ||
} | ||
// We are top level or this stub is marked as global | ||
if (module.parent == this._parent || hasOwnProperty.call(stub, '@global') || hasOwnProperty.call(stub, '@runtimeGlobal')) { | ||
return stub; | ||
if (module.parent === this._parent || hasOwnProperty.call(stub, '@global') || hasOwnProperty.call(stub, '@runtimeGlobal')) { | ||
return stub | ||
} | ||
@@ -170,46 +197,44 @@ } | ||
if (this._containsRuntimeGlobal) { | ||
return this._withoutCache(module, stubs, path, Module._load.bind(Module, path, module)); | ||
return this._withoutCache(module, stubs, path, Module._load.bind(Module, path, module)) | ||
} else { | ||
return Module._load(path, module); | ||
return Module._load(path, module) | ||
} | ||
}; | ||
} | ||
Proxyquire.prototype._withoutCache = function(module, stubs, path, func) { | ||
Proxyquire.prototype._withoutCache = function (module, stubs, path, func) { | ||
// Temporarily disable the cache - either per-module or globally if we have global stubs | ||
var restoreCache = this._disableCache(module, path); | ||
var restoreCache = this._disableCache(module, path) | ||
var resolvedPath = Module._resolveFilename(path, module) | ||
// Resolve all stubs to absolute paths. | ||
stubs = Object.keys(stubs) | ||
.reduce(function (result, stubPath) { | ||
var resolvedStubPath = this._resolveModule(resolvedPath, stubPath) | ||
result[resolvedStubPath] = stubs[stubPath] | ||
return result | ||
}.bind(this), {}) | ||
// Override all require extension handlers | ||
var restoreExtensionHandlers = this._overrideExtensionHandlers(module, stubs); | ||
var restoreExtensionHandlers = this._overrideExtensionHandlers(module, stubs) | ||
try { | ||
// Execute the function that needs the module cache disabled | ||
return func(); | ||
return func() | ||
} finally { | ||
// Restore the cache if we are preserving it | ||
if (this._preserveCache) { | ||
restoreCache(); | ||
restoreCache() | ||
} else { | ||
var id = Module._resolveFilename(path, module); | ||
var stubIds = Object.keys(stubs).map(function (stubPath) { | ||
try { | ||
return resolve.sync(stubPath, { | ||
basedir: dirname(id), | ||
extensions: Object.keys(require.extensions), | ||
paths: Module.globalPaths | ||
}) | ||
} catch (_) {} | ||
}); | ||
var ids = [id].concat(stubIds.filter(Boolean)); | ||
var ids = [resolvedPath].concat(Object.keys(stubs).filter(Boolean)) | ||
ids.forEach(function (id) { | ||
delete require.cache[id]; | ||
}); | ||
delete require.cache[id] | ||
}) | ||
} | ||
// Finally restore the original extension handlers | ||
restoreExtensionHandlers(); | ||
restoreExtensionHandlers() | ||
} | ||
}; | ||
} | ||
Proxyquire.prototype._disableCache = function(module, path) { | ||
Proxyquire.prototype._disableCache = function (module, path) { | ||
if (this._containsGlobal) { | ||
@@ -219,12 +244,12 @@ // empty the require cache because if we are stubbing C but requiring A, | ||
// and we'll never get the chance to return our stub | ||
return this._disableGlobalCache(); | ||
return this._disableGlobalCache() | ||
} | ||
// Temporarily delete the SUT from the require cache | ||
return this._disableModuleCache(path, module); | ||
}; | ||
return this._disableModuleCache(path, module) | ||
} | ||
Proxyquire.prototype._disableGlobalCache = function() { | ||
var cache = require.cache; | ||
require.cache = Module._cache = {}; | ||
Proxyquire.prototype._disableGlobalCache = function () { | ||
var cache = require.cache | ||
require.cache = Module._cache = {} | ||
@@ -237,3 +262,3 @@ for (var id in cache) { | ||
if (/\.node$/.test(id)) { | ||
require.cache[id] = cache[id]; | ||
require.cache[id] = cache[id] | ||
} | ||
@@ -243,3 +268,3 @@ } | ||
// Return a function that will undo what we just did | ||
return function() { | ||
return function () { | ||
// Keep native modules which were added to the cache in the meantime. | ||
@@ -252,48 +277,50 @@ for (var id in require.cache) { | ||
require.cache = Module._cache = cache; | ||
}; | ||
}; | ||
require.cache = Module._cache = cache | ||
} | ||
} | ||
Proxyquire.prototype._disableModuleCache = function(path, module) { | ||
Proxyquire.prototype._disableModuleCache = function (path, module) { | ||
// Find the ID (location) of the SUT, relative to the parent | ||
var id = Module._resolveFilename(path, module); | ||
var id = Module._resolveFilename(path, module) | ||
var cached = Module._cache[id]; | ||
delete Module._cache[id]; | ||
var cached = Module._cache[id] | ||
delete Module._cache[id] | ||
// Return a function that will undo what we just did | ||
return function() { | ||
return function () { | ||
if (cached) { | ||
Module._cache[id] = cached; | ||
Module._cache[id] = cached | ||
} | ||
}; | ||
}; | ||
} | ||
} | ||
Proxyquire.prototype._overrideExtensionHandlers = function(module, stubs) { | ||
var originalExtensions = {}; | ||
var self = this; | ||
Proxyquire.prototype._overrideExtensionHandlers = function (module, resolvedStubs) { | ||
/* eslint node/no-deprecated-api: [error, {ignoreGlobalItems: ["require.extensions"]}] */ | ||
Object.keys(require.extensions).forEach(function(extension) { | ||
var originalExtensions = {} | ||
var self = this | ||
Object.keys(require.extensions).forEach(function (extension) { | ||
// Store the original so we can restore it later | ||
if (!originalExtensions[extension]) { | ||
originalExtensions[extension] = require.extensions[extension]; | ||
originalExtensions[extension] = require.extensions[extension] | ||
} | ||
// Override the default handler for the requested file extension | ||
require.extensions[extension] = function(module, filename) { | ||
require.extensions[extension] = function (module, filename) { | ||
// Override the require method for this module | ||
module.require = self._require.bind(self, module, stubs); | ||
module.require = self._require.bind(self, module, resolvedStubs) | ||
return originalExtensions[extension](module, filename); | ||
}; | ||
}); | ||
return originalExtensions[extension](module, filename) | ||
} | ||
}) | ||
// Return a function that will undo what we just did | ||
return function() { | ||
Object.keys(originalExtensions).forEach(function(extension) { | ||
require.extensions[extension] = originalExtensions[extension]; | ||
}); | ||
}; | ||
}; | ||
return function () { | ||
Object.keys(originalExtensions).forEach(function (extension) { | ||
require.extensions[extension] = originalExtensions[extension] | ||
}) | ||
} | ||
} | ||
module.exports = Proxyquire; | ||
module.exports = Proxyquire |
{ | ||
"name": "proxyquire", | ||
"version": "1.8.0", | ||
"version": "2.0.0", | ||
"description": "Proxies nodejs require in order to allow overriding dependencies during testing.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "standard && mocha" | ||
}, | ||
@@ -27,6 +27,7 @@ "repository": { | ||
"devDependencies": { | ||
"mocha": "~3.1", | ||
"mocha": "^3.5.3", | ||
"native-hello-world": "^1.0.0", | ||
"should": "~3.3", | ||
"sinon": "~1.9" | ||
"sinon": "~1.9", | ||
"standard": "^11.0.0" | ||
}, | ||
@@ -33,0 +34,0 @@ "dependencies": { |
@@ -295,3 +295,3 @@ # proxyquire [![Build Status](https://secure.travis-ci.org/thlorenz/proxyquire.svg)](http://travis-ci.org/thlorenz/proxyquire) | ||
// foo.js | ||
var bar = require('bar'); | ||
var bar = require('./bar'); | ||
@@ -303,3 +303,3 @@ module.exports = function() { | ||
// bar.js | ||
var baz = require('baz'); | ||
var baz = require('./baz'); | ||
@@ -319,3 +319,3 @@ module.exports = function() { | ||
var stubs = { | ||
'baz': { | ||
'./baz': { | ||
method: function(val) { | ||
@@ -330,3 +330,3 @@ console.info('goodbye'); | ||
var foo = proxyquire('foo', stubs); | ||
var foo = proxyquire('./foo', stubs); | ||
foo(); // 'goodbye' is printed to stdout | ||
@@ -333,0 +333,0 @@ ``` |
@@ -1,14 +0,14 @@ | ||
/*jshint asi:true*/ | ||
/*global describe, before, beforeEach, it */ | ||
'use strict'; | ||
'use strict' | ||
/* global describe, it */ | ||
var assert = require('assert') | ||
, realFoo = require('./samples/foo'); | ||
var realFoo = require('./samples/foo') | ||
var stubs = { | ||
path:{ | ||
extname:function () {}, | ||
basename:function () {} | ||
path: { | ||
extname: function () {}, | ||
basename: function () {} | ||
} | ||
}; | ||
} | ||
@@ -20,22 +20,22 @@ describe('api', function () { | ||
it('proxyquire can load', function () { | ||
var proxiedFoo = proxyquire.load('./samples/foo', stubs); | ||
var proxiedFoo = proxyquire.load('./samples/foo', stubs) | ||
assert.equal(typeof proxiedFoo, 'object'); | ||
assert.notStrictEqual(realFoo, proxiedFoo); | ||
}); | ||
assert.equal(typeof proxiedFoo, 'object') | ||
assert.notStrictEqual(realFoo, proxiedFoo) | ||
}) | ||
it('proxyquire can callThru and then load', function () { | ||
var proxiedFoo = proxyquire.callThru().load('./samples/foo', stubs); | ||
var proxiedFoo = proxyquire.callThru().load('./samples/foo', stubs) | ||
assert.equal(typeof proxiedFoo, 'object'); | ||
assert.notStrictEqual(realFoo, proxiedFoo); | ||
}); | ||
assert.equal(typeof proxiedFoo, 'object') | ||
assert.notStrictEqual(realFoo, proxiedFoo) | ||
}) | ||
it('proxyquire can noCallThru and then load', function () { | ||
var proxiedFoo = proxyquire.noCallThru().load('./samples/foo', stubs); | ||
var proxiedFoo = proxyquire.noCallThru().load('./samples/foo', stubs) | ||
assert.equal(typeof proxiedFoo, 'object'); | ||
assert.notStrictEqual(realFoo, proxiedFoo); | ||
}); | ||
}); | ||
}); | ||
assert.equal(typeof proxiedFoo, 'object') | ||
assert.notStrictEqual(realFoo, proxiedFoo) | ||
}) | ||
}) | ||
}) |
@@ -1,19 +0,15 @@ | ||
/*jshint asi:true*/ | ||
/*global describe, before, beforeEach, it */ | ||
'use strict' | ||
"use strict"; | ||
/* global describe, it */ | ||
var assert = require('assert') | ||
, proxyquire = require('..') | ||
; | ||
var proxyquire = require('..') | ||
describe('Illegal parameters to resolve give meaningful errors', function () { | ||
var bar = { bar: function () { return 'bar'; } } | ||
, exception | ||
; | ||
var bar = { bar: function () { return 'bar' } } | ||
function throws(action, regex) { | ||
function throws (action, regex) { | ||
assert.throws(action, function (err) { | ||
return err.name === 'ProxyquireError' && regex.test(err.message) && regex.test(err.toString()); | ||
}); | ||
return err.name === 'ProxyquireError' && regex.test(err.message) && regex.test(err.toString()) | ||
}) | ||
} | ||
@@ -23,7 +19,7 @@ | ||
function act () { | ||
proxyquire(null, {}); | ||
proxyquire(null, {}) | ||
} | ||
it('throws an exception explaining that a request path must be provided', function () { | ||
throws(act, /missing argument: "request"/i); | ||
throws(act, /missing argument: "request"/i) | ||
}) | ||
@@ -33,9 +29,8 @@ }) | ||
describe('when I pass an object as a request', function () { | ||
function act () { | ||
proxyquire({ }, { './bar': bar }); | ||
proxyquire({ }, { './bar': bar }) | ||
} | ||
it('throws an exception explaining that request needs to be a requirable string', function () { | ||
throws(act, /invalid argument: "request".+needs to be a requirable string/i); | ||
throws(act, /invalid argument: "request".+needs to be a requirable string/i) | ||
}) | ||
@@ -46,9 +41,8 @@ }) | ||
function act () { | ||
proxyquire('./samples/foo'); | ||
proxyquire('./samples/foo') | ||
} | ||
it('throws an exception explaining that resolve without stubs makes no sense', function () { | ||
throws(act, /missing argument: "stubs".+use regular require instead/i); | ||
throws(act, /missing argument: "stubs".+use regular require instead/i) | ||
}) | ||
}) | ||
@@ -58,7 +52,7 @@ | ||
function act () { | ||
proxyquire('./samples/foo', 'stubs'); | ||
proxyquire('./samples/foo', 'stubs') | ||
} | ||
it('throws an exception explaining that stubs need to be an object', function () { | ||
throws(act, /invalid argument: "stubs".+needs to be an object/i); | ||
throws(act, /invalid argument: "stubs".+needs to be an object/i) | ||
}) | ||
@@ -65,0 +59,0 @@ }) |
@@ -1,92 +0,91 @@ | ||
/*jshint asi:true*/ | ||
/*global describe, before, beforeEach, it */ | ||
'use strict' | ||
"use strict"; | ||
/* global describe, it */ | ||
var assert = require('assert'); | ||
var assert = require('assert') | ||
describe("Proxyquire", function() { | ||
describe('load()', function() { | ||
it('defaults to preserving the cache', function() { | ||
var original = require('./samples/foo'); | ||
original.state = 'cached'; | ||
describe('Proxyquire', function () { | ||
describe('load()', function () { | ||
it('defaults to preserving the cache', function () { | ||
var original = require('./samples/foo') | ||
original.state = 'cached' | ||
var proxyquire = require('..'); | ||
var proxyFoo = proxyquire('./samples/foo', { 'path': { } }); | ||
var proxyquire = require('..') | ||
proxyquire('./samples/foo', { 'path': { } }) | ||
var foo = require('./samples/foo'); | ||
assert.equal('cached', foo.state); | ||
assert.equal(foo, original); | ||
}); | ||
}); | ||
var foo = require('./samples/foo') | ||
assert.equal('cached', foo.state) | ||
assert.equal(foo, original) | ||
}) | ||
}) | ||
describe('preserveCache()', function() { | ||
it('returns a reference to itself, so it can be chained', function() { | ||
var proxyquire = require('..'); | ||
assert.equal(proxyquire.preserveCache(), proxyquire); | ||
}); | ||
describe('preserveCache()', function () { | ||
it('returns a reference to itself, so it can be chained', function () { | ||
var proxyquire = require('..') | ||
assert.equal(proxyquire.preserveCache(), proxyquire) | ||
}) | ||
it('has Proxyquire restore the cache for the module', function() { | ||
var original = require('./samples/foo'); | ||
original.state = 'cached'; | ||
it('has Proxyquire restore the cache for the module', function () { | ||
var original = require('./samples/foo') | ||
original.state = 'cached' | ||
var proxyquire = require('..'); | ||
proxyquire.preserveCache(); | ||
proxyquire.load('./samples/foo', { 'path': { } }); | ||
var proxyquire = require('..') | ||
proxyquire.preserveCache() | ||
proxyquire.load('./samples/foo', { 'path': { } }) | ||
var foo = require('./samples/foo'); | ||
assert.equal('cached', foo.state); | ||
assert.equal(foo, original); | ||
}); | ||
var foo = require('./samples/foo') | ||
assert.equal('cached', foo.state) | ||
assert.equal(foo, original) | ||
}) | ||
it('allows Singletons to function properly', function() { | ||
var original = require('./samples/foo-singleton').getInstance(); | ||
it('allows Singletons to function properly', function () { | ||
var original = require('./samples/foo-singleton').getInstance() | ||
var proxyquire = require('..'); | ||
proxyquire.preserveCache(); | ||
proxyquire.load('./samples/foo-singleton', { 'path': { } }).getInstance(); | ||
var proxyquire = require('..') | ||
proxyquire.preserveCache() | ||
proxyquire.load('./samples/foo-singleton', { 'path': { } }).getInstance() | ||
var fooSingleton = require('./samples/foo-singleton').getInstance(); | ||
assert.equal(fooSingleton, original); | ||
}); | ||
}); | ||
var fooSingleton = require('./samples/foo-singleton').getInstance() | ||
assert.equal(fooSingleton, original) | ||
}) | ||
}) | ||
describe('noPreserveCache()', function() { | ||
it('returns a reference to itself, so it can be chained', function() { | ||
var proxyquire = require('..'); | ||
assert.equal(proxyquire.noPreserveCache(), proxyquire); | ||
}); | ||
describe('noPreserveCache()', function () { | ||
it('returns a reference to itself, so it can be chained', function () { | ||
var proxyquire = require('..') | ||
assert.equal(proxyquire.noPreserveCache(), proxyquire) | ||
}) | ||
it('forces subsequent requires to reload the proxied module', function() { | ||
var original = require('./samples/foo'); | ||
original.state = 'cached'; | ||
it('forces subsequent requires to reload the proxied module', function () { | ||
var original = require('./samples/foo') | ||
original.state = 'cached' | ||
var proxyquire = require('..'); | ||
proxyquire.load('./samples/foo', { 'path': { } }); | ||
var proxyquire = require('..') | ||
proxyquire.load('./samples/foo', { 'path': { } }) | ||
var cacheFoo = require('./samples/foo'); | ||
assert.equal('cached', cacheFoo.state); | ||
assert.equal(cacheFoo, original); | ||
var cacheFoo = require('./samples/foo') | ||
assert.equal('cached', cacheFoo.state) | ||
assert.equal(cacheFoo, original) | ||
proxyquire.noPreserveCache(); | ||
proxyquire.load('./samples/foo', { 'path': { } }); | ||
var foo = require('./samples/foo'); | ||
assert.equal('', foo.state); | ||
assert.notEqual(foo, original); | ||
}); | ||
proxyquire.noPreserveCache() | ||
proxyquire.load('./samples/foo', { 'path': { } }) | ||
var foo = require('./samples/foo') | ||
assert.equal('', foo.state) | ||
assert.notEqual(foo, original) | ||
}) | ||
it('deletes the require.cache for the module being stubbed', function() { | ||
var proxyquire = require('..').noPreserveCache(); | ||
it('deletes the require.cache for the module being stubbed', function () { | ||
var proxyquire = require('..').noPreserveCache() | ||
var foo = proxyquire.load('./samples/foo', { 'path': { } }); | ||
assert.equal(undefined, require.cache[require.resolve('./samples/foo')]); | ||
}); | ||
proxyquire.load('./samples/foo', { 'path': { } }) | ||
assert.equal(undefined, require.cache[require.resolve('./samples/foo')]) | ||
}) | ||
it('deletes the require.cache for the stubs', function() { | ||
var proxyquire = require('..').noPreserveCache(); | ||
it('deletes the require.cache for the stubs', function () { | ||
var proxyquire = require('..').noPreserveCache() | ||
var bar = {}; | ||
var foo = proxyquire.load('./samples/cache/foo', { './bar': bar }); | ||
bar.f.g = function () { return 'a' }; | ||
bar.h = function () { return 'a' }; | ||
var bar = {} | ||
var foo = proxyquire.load('./samples/cache/foo', { './bar': bar }) | ||
bar.f.g = function () { return 'a' } | ||
bar.h = function () { return 'a' } | ||
@@ -96,18 +95,18 @@ assert.equal(foo.bar.f.g(), 'a') | ||
foo = proxyquire.load('./samples/cache/foo', { './bar': {} }); | ||
foo = proxyquire.load('./samples/cache/foo', { './bar': {} }) | ||
assert.equal(foo.bar.h(), 'h') | ||
assert.equal(foo.bar.f.g(), 'g') | ||
assert.equal(undefined, require.cache[require.resolve('./samples/cache/foo')]); | ||
assert.equal(undefined, require.cache[require.resolve('./samples/cache/bar')]); | ||
}); | ||
assert.equal(undefined, require.cache[require.resolve('./samples/cache/foo')]) | ||
assert.equal(undefined, require.cache[require.resolve('./samples/cache/bar')]) | ||
}) | ||
it('silences errors when stub lookups fail', function() { | ||
var proxyquire = require('..').noPreserveCache(); | ||
it('silences errors when stub lookups fail', function () { | ||
var proxyquire = require('..').noPreserveCache() | ||
assert.doesNotThrow(function () { | ||
proxyquire.load('./samples/cache/foo', { './does-not-exist': {} }); | ||
proxyquire.load('./samples/cache/foo', { './does-not-exist': {} }) | ||
}) | ||
}); | ||
}); | ||
}); | ||
}) | ||
}) | ||
}) |
@@ -1,12 +0,11 @@ | ||
'use strict'; | ||
/*jshint asi:true */ | ||
/*global describe, before, beforeEach, it */ | ||
'use strict' | ||
/* jshint asi:true */ | ||
/* global describe, it */ | ||
var proxyquire = require('..'); | ||
var proxyquire = require('..') | ||
describe('when I try to use compat mode', function () { | ||
it('should let me know that I need to fix my code or downgrade', function () { | ||
proxyquire.compat.should.throw(/compat mode has been removed/); | ||
}); | ||
}); | ||
proxyquire.compat.should.throw(/compat mode has been removed/) | ||
}) | ||
}) |
@@ -1,33 +0,33 @@ | ||
'use strict'; | ||
/*jshint asi:true */ | ||
/*global describe, before, beforeEach, it */ | ||
'use strict' | ||
/* global describe, before, it */ | ||
var proxyquire = require('..').noCallThru() | ||
describe('when I require stubs with different extensions', function () { | ||
var res; | ||
var res | ||
before(function () { | ||
res = proxyquire('./samples/extensions', { | ||
fs : 'fs.export' | ||
, fn : function () { return 'fn.result' } | ||
, '/fs.json': 'fs.json.export' | ||
, '/fn.node': 'fn.node.export' | ||
res = proxyquire('./samples/extensions', { | ||
fs: 'fs.export', | ||
fn: function () { return 'fn.result' }, | ||
'/fs.json': 'fs.json.export', | ||
'/fn.node': 'fn.node.export' | ||
}) | ||
}) | ||
it('intercepts [] object', function () { | ||
res.fs.should.equal('fs.export') | ||
res.fs.should.equal('fs.export') | ||
}) | ||
it('intercepts [] function', function () { | ||
res.fn().should.equal('fn.result'); | ||
res.fn().should.equal('fn.result') | ||
}) | ||
it('intercepts [.json] object', function () { | ||
res['/fs.json'].should.equal('fs.json.export') | ||
res['/fs.json'].should.equal('fs.json.export') | ||
}) | ||
it('intercepts [.node] object', function () { | ||
res['/fn.node'].should.equal('fn.node.export') | ||
res['/fn.node'].should.equal('fn.node.export') | ||
}) | ||
}) |
@@ -1,9 +0,9 @@ | ||
/*jshint asi:true*/ | ||
/*global describe, before, beforeEach, it */ | ||
'use strict'; | ||
'use strict' | ||
/* global describe, it */ | ||
var assert = require('assert') | ||
, realFoo = require('./samples/global/foo'); | ||
var realFoo = require('./samples/global/foo') | ||
var proxyquire = require('..'); | ||
var proxyquire = require('..') | ||
@@ -14,14 +14,14 @@ describe('global flags set', function () { | ||
'./baz': { | ||
method: function() { | ||
return true; | ||
method: function () { | ||
return true | ||
}, | ||
'@global': true | ||
} | ||
}; | ||
} | ||
var proxiedFoo = proxyquire('./samples/global/foo', stubs); | ||
var proxiedFoo = proxyquire('./samples/global/foo', stubs) | ||
assert.equal(realFoo(), false); | ||
assert.equal(proxiedFoo(), true); | ||
}); | ||
assert.equal(realFoo(), false) | ||
assert.equal(proxiedFoo(), true) | ||
}) | ||
@@ -31,14 +31,14 @@ it('should override require globally even when require\'s execution is deferred', function () { | ||
'./baz': { | ||
method: function() { | ||
return true; | ||
method: function () { | ||
return true | ||
}, | ||
'@runtimeGlobal': true | ||
} | ||
}; | ||
} | ||
var proxiedFoo = proxyquire('./samples/global/foo-deferred', stubs); | ||
var proxiedFoo = proxyquire('./samples/global/foo-deferred', stubs) | ||
assert.equal(realFoo(), false); | ||
assert.equal(proxiedFoo(), true); | ||
}); | ||
assert.equal(realFoo(), false) | ||
assert.equal(proxiedFoo(), true) | ||
}) | ||
@@ -50,3 +50,3 @@ it('should not throw when a native module is required a second time', function () { | ||
} | ||
}; | ||
} | ||
@@ -56,3 +56,3 @@ proxyquire('native-hello-world', stubs) | ||
}) | ||
}); | ||
}) | ||
@@ -63,13 +63,13 @@ describe('global flags not set', function () { | ||
'./baz': { | ||
method: function() { | ||
return true; | ||
method: function () { | ||
return true | ||
} | ||
} | ||
}; | ||
} | ||
var proxiedFoo = proxyquire('./samples/global/foo', stubs); | ||
var proxiedFoo = proxyquire('./samples/global/foo', stubs) | ||
assert.equal(realFoo(), false); | ||
assert.equal(proxiedFoo(), false); | ||
}); | ||
assert.equal(realFoo(), false) | ||
assert.equal(proxiedFoo(), false) | ||
}) | ||
@@ -79,13 +79,13 @@ it('should not override require globally even when require\'s execution is deferred', function () { | ||
'./baz': { | ||
method: function() { | ||
return true; | ||
method: function () { | ||
return true | ||
} | ||
} | ||
}; | ||
} | ||
var proxiedFoo = proxyquire('./samples/global/foo-deferred', stubs); | ||
var proxiedFoo = proxyquire('./samples/global/foo-deferred', stubs) | ||
assert.equal(realFoo(), false); | ||
assert.equal(proxiedFoo(), false); | ||
}); | ||
}); | ||
assert.equal(realFoo(), false) | ||
assert.equal(proxiedFoo(), false) | ||
}) | ||
}) |
@@ -1,8 +0,7 @@ | ||
/*jshint asi:true*/ | ||
/*global describe, before, beforeEach, it */ | ||
"use strict"; | ||
/* jshint asi:true */ | ||
/* global describe, before, it */ | ||
'use strict' | ||
var assert = require('assert') | ||
, proxyquire = require('..') | ||
; | ||
var proxyquire = require('..') | ||
@@ -12,18 +11,17 @@ describe('Multiple requires of same module don\'t affect each other', function () { | ||
var foo1 | ||
, foo2 | ||
, bar1 = { bar: function () { return 'bar1'; } } | ||
, bar2 = { bar: function () { return 'bar2'; } } | ||
; | ||
var foo2 | ||
var bar1 = { bar: function () { return 'bar1' } } | ||
var bar2 = { bar: function () { return 'bar2' } } | ||
before(function () { | ||
foo1 = proxyquire('./samples/foo', { './bar': bar1 }); | ||
foo2 = proxyquire('./samples/foo', { './bar': bar2 }); | ||
foo1 = proxyquire('./samples/foo', { './bar': bar1 }) | ||
foo2 = proxyquire('./samples/foo', { './bar': bar2 }) | ||
}) | ||
it('foo1.bigBar() == "BAR1"', function () { | ||
assert.equal(foo1.bigBar(), 'BAR1'); | ||
assert.equal(foo1.bigBar(), 'BAR1') | ||
}) | ||
it('foo2.bigBar() == "BAR2"', function () { | ||
assert.equal(foo2.bigBar(), 'BAR2'); | ||
assert.equal(foo2.bigBar(), 'BAR2') | ||
}) | ||
@@ -33,15 +31,14 @@ | ||
before(function () { | ||
bar1.bar = function () { return 'barone'; }; | ||
bar1.bar = function () { return 'barone' } | ||
}) | ||
it('foo1.bigBar() == "BARONE"', function () { | ||
assert.equal(foo1.bigBar(), 'BARONE'); | ||
assert.equal(foo1.bigBar(), 'BARONE') | ||
}) | ||
it('foo2.bigBar() == "BAR2"', function () { | ||
assert.equal(foo2.bigBar(), 'BAR2'); | ||
assert.equal(foo2.bigBar(), 'BAR2') | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,26 +0,24 @@ | ||
/*jshint asi:true*/ | ||
/*global describe, before, it */ | ||
"use strict"; | ||
'use strict' | ||
/* global describe, before, it */ | ||
var assert = require('assert') | ||
, proxyquire = require('..') | ||
, stats = require('./samples/stats') | ||
; | ||
var proxyquire = require('..') | ||
var stats = require('./samples/stats') | ||
describe('Given foo requires the boof, foonum and foobool modules and boof is a string, foonum is a Number and foobool is a bool', function () { | ||
var file = '/folder/test.ext' | ||
, foo | ||
, boofber = 'a_string' | ||
, foonumber = 4 | ||
, fooboolber = false | ||
; | ||
var foo | ||
var boofber = 'a_string' | ||
var foonumber = 4 | ||
var fooboolber = false | ||
var fooarray = ['x', 'y', 'z'] | ||
describe('When I resolve foo with boofber stub as boof.', function () { | ||
before(function () { | ||
stats.reset(); | ||
foo = proxyquire('./samples/foo', { './boof':boofber}) | ||
stats.reset() | ||
foo = proxyquire('./samples/foo', {'./boof': boofber}) | ||
}) | ||
it('foo is required 1 times', function () { | ||
assert.equal(stats.fooRequires(), 1); | ||
assert.equal(stats.fooRequires(), 1) | ||
}) | ||
@@ -30,3 +28,3 @@ | ||
it('foo.boof == boofber', function () { | ||
assert.equal(foo.boof, boofber); | ||
assert.equal(foo.boof, boofber) | ||
}) | ||
@@ -38,8 +36,8 @@ }) | ||
before(function () { | ||
stats.reset(); | ||
foo = proxyquire('./samples/foo', { './foonum':foonumber}) | ||
stats.reset() | ||
foo = proxyquire('./samples/foo', {'./foonum': foonumber}) | ||
}) | ||
it('foo is required 1 times', function () { | ||
assert.equal(stats.fooRequires(), 1); | ||
assert.equal(stats.fooRequires(), 1) | ||
}) | ||
@@ -49,3 +47,3 @@ | ||
it('foo.foonum == foonumber', function () { | ||
assert.equal(foo.foonum, foonumber); | ||
assert.equal(foo.foonum, foonumber) | ||
}) | ||
@@ -57,8 +55,8 @@ }) | ||
before(function () { | ||
stats.reset(); | ||
foo = proxyquire('./samples/foo', { './foobool':fooboolber}) | ||
stats.reset() | ||
foo = proxyquire('./samples/foo', {'./foobool': fooboolber}) | ||
}) | ||
it('foo is required 1 times', function () { | ||
assert.equal(stats.fooRequires(), 1); | ||
assert.equal(stats.fooRequires(), 1) | ||
}) | ||
@@ -68,3 +66,3 @@ | ||
it('foo.foobool == fooboolber', function () { | ||
assert.equal(foo.foobool, fooboolber); | ||
assert.equal(foo.foobool, fooboolber) | ||
}) | ||
@@ -74,2 +72,54 @@ }) | ||
describe('When I resolve foo with ./fooarray stub as fooarray.', function () { | ||
before(function () { | ||
stats.reset() | ||
foo = proxyquire('./samples/foo', {'./fooarray': fooarray}) | ||
}) | ||
it('foo is required 1 times', function () { | ||
assert.equal(stats.fooRequires(), 1) | ||
}) | ||
describe('foo\'s fooarray is fooarray', function () { | ||
it('foo.fooarray is fooarray', function () { | ||
assert.deepEqual(foo.fooarray, ['x', 'y', 'z']) | ||
}) | ||
}) | ||
}) | ||
describe('When I resolve foo with ./fooarray stub as empty.', function () { | ||
before(function () { | ||
stats.reset() | ||
foo = proxyquire('./samples/foo', {'./fooarray': []}) | ||
}) | ||
it('foo is required 1 times', function () { | ||
assert.equal(stats.fooRequires(), 1) | ||
}) | ||
describe('foo\'s fooarray is the original', function () { | ||
it('foo.fooarray is empty', function () { | ||
assert.deepEqual(foo.fooarray, ['a', 'b', 'c']) | ||
}) | ||
}) | ||
}) | ||
describe('When I resolve foo with ./fooarray stub as empty with @noCallThru.', function () { | ||
before(function () { | ||
stats.reset() | ||
var empty = [] | ||
Object.defineProperty(empty, '@noCallThru', {value: true}) | ||
foo = proxyquire('./samples/foo', {'./fooarray': empty}) | ||
}) | ||
it('foo is required 1 times', function () { | ||
assert.equal(stats.fooRequires(), 1) | ||
}) | ||
describe('foo\'s fooarray is empty', function () { | ||
it('foo.fooarray is empty', function () { | ||
assert.deepEqual(foo.fooarray, []) | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -1,17 +0,16 @@ | ||
'use strict'; | ||
/*jshint asi:true*/ | ||
/*global describe, before, beforeEach, it */ | ||
'use strict' | ||
/* global describe, it */ | ||
var assert = require('assert') | ||
, proxyquire = require('..') | ||
, path = require('path') | ||
, fooPath = path.join(__dirname, './samples/notexisting/foo.js') | ||
var proxyquire = require('..') | ||
var path = require('path') | ||
var fooPath = path.join(__dirname, './samples/notexisting/foo.js') | ||
describe('When resolving foo that requires stubbed /not/existing/bar.json', function () { | ||
describe('When resolving foo that requires stubbed /not/existing/bar.json', function () { | ||
it('throws an error', function () { | ||
assert.throws(function () { | ||
proxyquire(fooPath, { | ||
'/not/existing/bar.json': { config: 'bar\'s config' } | ||
}) | ||
'/not/existing/bar.json': { config: 'bar\'s config' } | ||
}) | ||
}) | ||
@@ -21,11 +20,11 @@ }) | ||
describe('When resolving foo that requires stubbed /not/existing/bar.json with noCallThru', function () { | ||
var foo; | ||
describe('When resolving foo that requires stubbed /not/existing/bar.json with noCallThru', function () { | ||
var foo | ||
it('resolves foo with stubbed bar', function () { | ||
foo = proxyquire(fooPath, { | ||
'/not/existing/bar.json': { config: 'bar\'s config', '@noCallThru': true } | ||
}) | ||
'/not/existing/bar.json': { config: 'bar\'s config', '@noCallThru': true } | ||
}) | ||
assert.equal(foo.config, 'bar\'s config') | ||
}) | ||
}) |
@@ -1,9 +0,9 @@ | ||
'use strict'; | ||
/*jshint asi:true*/ | ||
/*global describe, before, beforeEach, it */ | ||
'use strict' | ||
/* jshint asi:true */ | ||
/* global describe, it */ | ||
var assert = require('assert') | ||
, proxyquire = require('..') | ||
, path = require('path') | ||
, fooPath = path.join(__dirname, './samples/foo.js') | ||
var proxyquire = require('..') | ||
var path = require('path') | ||
var fooPath = path.join(__dirname, './samples/foo.js') | ||
@@ -21,5 +21,4 @@ describe('When resolving foo that requires nulled file package', function () { | ||
var foo = proxyquire(fooPath, { crypto: null }) | ||
assert.equal(foo.bigCrypto(), 'caught'); | ||
assert.equal(foo.bigCrypto(), 'caught') | ||
}) | ||
}) | ||
@@ -1,38 +0,32 @@ | ||
'use strict'; | ||
/*jshint asi:true*/ | ||
/*global describe, before, beforeEach, it */ | ||
'use strict' | ||
/* jshint asi:true */ | ||
/* global describe, before, it */ | ||
var assert = require('assert') | ||
, proxyquire = require('..') | ||
; | ||
var proxyquire = require('..') | ||
describe('When resolving foo that requires bar and stubbed baz where bar requires unstubbed baz', function () { | ||
var bazStub | ||
, foo | ||
, baz | ||
; | ||
describe('When resolving foo that requires bar and stubbed baz where bar requires unstubbed baz', function () { | ||
var bazStub, | ||
foo, | ||
baz | ||
before(function () { | ||
bazStub = { | ||
testexport: 'stubbed' | ||
}; | ||
} | ||
foo = proxyquire('./samples/sub-dependencies/foo', { | ||
'./baz': bazStub | ||
}); | ||
}) | ||
baz = require('./samples/sub-dependencies/baz'); | ||
baz = require('./samples/sub-dependencies/baz') | ||
}) | ||
it('does not stub baz in bar', function () { | ||
assert.equal(foo.bar.baz.testexport, 'test'); | ||
assert.equal(foo.bar.baz.testexport, 'test') | ||
}) | ||
it('does not affect a normal baz import', function () { | ||
assert.equal(baz.testexport, 'test'); | ||
assert.equal(baz.testexport, 'test') | ||
}) | ||
}); | ||
}) |
@@ -1,26 +0,24 @@ | ||
/*jshint asi:true*/ | ||
/*global describe, before, beforeEach, it */ | ||
"use strict"; | ||
/* jshint asi:true */ | ||
/* global describe, before, beforeEach, it */ | ||
'use strict' | ||
var assert = require('assert') | ||
, proxyquire = require('..') | ||
, stats = require('./samples/stats') | ||
; | ||
var proxyquire = require('..') | ||
var stats = require('./samples/stats') | ||
describe('Given foo requires the bar and path modules and bar.bar() returns "bar"', function () { | ||
var file = '/folder/test.ext' | ||
, foo | ||
, foober | ||
, barber = { bar:function () { return 'barber'; } } | ||
; | ||
var foo | ||
var foober | ||
var barber = { bar: function () { return 'barber' } } | ||
describe('When I resolve foo with no overrides to bar as foo and resolve foo with barber stub as foober.', function () { | ||
before(function () { | ||
stats.reset(); | ||
foo = proxyquire('./samples/foo', { './bar':{ /* no overrides */ } }); | ||
foober = proxyquire('./samples/foo', { './bar':barber }); | ||
stats.reset() | ||
foo = proxyquire('./samples/foo', { './bar': { /* no overrides */ } }) | ||
foober = proxyquire('./samples/foo', { './bar': barber }) | ||
}) | ||
it('foo is required 2 times', function () { | ||
assert.equal(stats.fooRequires(), 2); | ||
assert.equal(stats.fooRequires(), 2) | ||
}) | ||
@@ -30,3 +28,3 @@ | ||
it('foo.bigBar() == "BAR"', function () { | ||
assert.equal(foo.bigBar(), 'BAR'); | ||
assert.equal(foo.bigBar(), 'BAR') | ||
}) | ||
@@ -36,48 +34,42 @@ }) | ||
describe('only stubbed modules have overrides in foober', function () { | ||
it('foober.bigBar() == "BARBER"', function () { | ||
assert.equal(foober.bigBar(), 'BARBER'); | ||
assert.equal(foober.bigBar(), 'BARBER') | ||
}) | ||
it('foober.bigExt("/folder/test.ext") == ".EXT"', function () { | ||
assert.equal(foober.bigExt(file), '.EXT'); | ||
assert.equal(foober.bigExt(file), '.EXT') | ||
}) | ||
it('foober.bigBas("/folder/test.ext") == "TEST.EXT"', function () { | ||
assert.equal(foober.bigBas(file), 'TEST.EXT'); | ||
assert.equal(foober.bigBas(file), 'TEST.EXT') | ||
}) | ||
}) | ||
describe('when I override keys of stubs after resolve', function () { | ||
before(function () { | ||
barber.bar = function () { return 'friseur'; } | ||
barber.rab = function () { return 'rabarber'; } | ||
}); | ||
barber.bar = function () { return 'friseur' } | ||
barber.rab = function () { return 'rabarber' } | ||
}) | ||
it('overrides behavior when module is required inside function call', function () { | ||
assert.equal(foober.bigBar(), 'FRISEUR'); | ||
assert.equal(foober.bigBar(), 'FRISEUR') | ||
}) | ||
it('overrides behavior when module is required on top of file', function () { | ||
assert.equal(foober.bigRab(), 'RABARBER'); | ||
assert.equal(foober.bigRab(), 'RABARBER') | ||
}) | ||
describe('and then delete overrides of stubs after resolve', function () { | ||
beforeEach(function () { | ||
delete barber.bar; | ||
delete barber.rab; | ||
delete barber.bar | ||
delete barber.rab | ||
}) | ||
it('reverts to original behavior when module is required inside function call', function () { | ||
assert.equal(foober.bigBar(), 'BAR'); | ||
assert.equal(foober.bigBar(), 'BAR') | ||
}) | ||
it('doesn\'t properly revert to original behavior when module is required on top of file ', function () { | ||
assert.throws(foober.bigRab); | ||
assert.throws(foober.bigRab) | ||
}) | ||
}) | ||
@@ -89,18 +81,17 @@ }) | ||
describe('and path.extname(file) is stubbed to return "override " + file', function () { | ||
describe('and callThru was not changed globally or for path module', function () { | ||
before(function () { | ||
foo = proxyquire('./samples/foo', { | ||
path:{ | ||
extname:function (file) { return 'override ' + file; } | ||
path: { | ||
extname: function (file) { return 'override ' + file } | ||
} | ||
}); | ||
}) | ||
}) | ||
it('foo.bigExt(file) == "OVERRIDE /FOLDER/TEST.EXT"', function () { | ||
assert.equal(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT'); | ||
assert.equal(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT') | ||
}) | ||
it('foo.bigBas(file) == "TEST.EXT"', function () { | ||
assert.equal(foo.bigBas(file), 'TEST.EXT'); | ||
assert.equal(foo.bigBas(file), 'TEST.EXT') | ||
}) | ||
@@ -112,22 +103,21 @@ }) | ||
foo = proxyquire('./samples/foo', { | ||
path:{ | ||
extname:function (file) { return 'override ' + file; }, '@noCallThru':true | ||
path: { | ||
extname: function (file) { return 'override ' + file }, '@noCallThru': true | ||
} | ||
}); | ||
}) | ||
}) | ||
it('foo.bigExt(file) == "OVERRIDE /FOLDER/TEST.EXT"', function () { | ||
assert.equal(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT'); | ||
assert.equal(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT') | ||
}) | ||
it('foo.bigBas(file) throws', function () { | ||
assert.throws(foo.bigBas); | ||
assert.throws(foo.bigBas) | ||
}) | ||
}) | ||
describe('and callThru was turned off globally', function () { | ||
var $proxyquire; | ||
var $proxyquire | ||
before(function () { | ||
$proxyquire = proxyquire.noCallThru(); | ||
$proxyquire = proxyquire.noCallThru() | ||
}) | ||
@@ -138,14 +128,14 @@ | ||
foo = $proxyquire('./samples/foo', { | ||
path:{ | ||
extname:function (file) { return 'override ' + file; } | ||
path: { | ||
extname: function (file) { return 'override ' + file } | ||
} | ||
}); | ||
}) | ||
}) | ||
it('foo.bigExt(file) == "OVERRIDE /FOLDER/TEST.EXT"', function () { | ||
assert.equal(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT'); | ||
assert.equal(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT') | ||
}) | ||
it('foo.bigBas(file) throws', function () { | ||
assert.throws(foo.bigBas); | ||
assert.throws(foo.bigBas) | ||
}) | ||
@@ -157,14 +147,14 @@ }) | ||
foo = $proxyquire('./samples/foo', { | ||
path:{ | ||
extname:function (file) { return 'override ' + file; }, '@noCallThru':false | ||
path: { | ||
extname: function (file) { return 'override ' + file }, '@noCallThru': false | ||
} | ||
}); | ||
}) | ||
}) | ||
it('foo.bigExt(file) == "OVERRIDE /FOLDER/TEST.EXT"', function () { | ||
assert.equal(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT'); | ||
assert.equal(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT') | ||
}) | ||
it('foo.bigBas(file) == "TEST.EXT"', function () { | ||
assert.equal(foo.bigBas(file), 'TEST.EXT'); | ||
assert.equal(foo.bigBas(file), 'TEST.EXT') | ||
}) | ||
@@ -178,14 +168,14 @@ }) | ||
.load('./samples/foo', { | ||
path:{ | ||
extname:function (file) { return 'override ' + file; } | ||
path: { | ||
extname: function (file) { return 'override ' + file } | ||
} | ||
}); | ||
}) | ||
}) | ||
it('foo.bigExt(file) == "OVERRIDE /FOLDER/TEST.EXT"', function () { | ||
assert.equal(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT'); | ||
assert.equal(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT') | ||
}) | ||
it('foo.bigBas(file) == "TEST.EXT"', function () { | ||
assert.equal(foo.bigBas(file), 'TEST.EXT'); | ||
assert.equal(foo.bigBas(file), 'TEST.EXT') | ||
}) | ||
@@ -197,4 +187,1 @@ }) | ||
}) | ||
function bar () { | ||
return 'bar'; | ||
return 'bar' | ||
} | ||
function rab () { | ||
return 'rab'; | ||
return 'rab' | ||
} | ||
module.exports = { bar : bar, rab: rab }; | ||
module.exports = { bar: bar, rab: rab } |
@@ -1,1 +0,1 @@ | ||
module.exports = 'boof' | ||
module.exports = 'boof' |
@@ -10,2 +10,2 @@ module.exports = { | ||
} | ||
}; | ||
} |
@@ -1,3 +0,3 @@ | ||
var bar = require('./bar'); | ||
var bar = require('./bar') | ||
module.exports = {bar: bar}; | ||
module.exports = {bar: bar} |
module.exports = { | ||
fs: require('fs') | ||
, fn: require('fn') | ||
, '/fs.json': require('/fs.json') | ||
, '/fn.node': require('/fn.node') | ||
}; | ||
fs: require('fs'), | ||
fn: require('fn'), | ||
'/fs.json': require('/fs.json'), | ||
'/fn.node': require('/fn.node') | ||
} |
@@ -1,12 +0,10 @@ | ||
var path = require('path'); | ||
function FooSingleton () { } | ||
function FooSingleton() { } | ||
var instance = null | ||
exports.getInstance = function () { | ||
if (instance === null) { | ||
instance = new FooSingleton() | ||
} | ||
var instance = null; | ||
exports.getInstance = function() { | ||
if (instance === null) { | ||
instance = new FooSingleton(); | ||
} | ||
return instance; | ||
}; | ||
return instance | ||
} |
var stats = require('./stats') | ||
, bar = require('./bar') | ||
, boof = require('./boof') | ||
, foonum = require('./foonum') | ||
, foobool = require('./foobool') | ||
, path = require('path') | ||
, crypto | ||
; | ||
var bar = require('./bar') | ||
var boof = require('./boof') | ||
var foonum = require('./foonum') | ||
var foobool = require('./foobool') | ||
var fooarray = require('./fooarray') | ||
var path = require('path') | ||
var crypto | ||
// Require if present. | ||
try { crypto = require('crypto'); } catch (e) { crypto = 'caught'; } | ||
try { crypto = require('crypto') } catch (e) { crypto = 'caught' } | ||
stats.incFooRequires(); | ||
stats.incFooRequires() | ||
function bigBar () { | ||
// inline require | ||
return require('./bar').bar().toUpperCase(); | ||
return require('./bar').bar().toUpperCase() | ||
} | ||
@@ -22,27 +22,28 @@ | ||
// module wide require | ||
return bar.rab().toUpperCase(); | ||
return bar.rab().toUpperCase() | ||
} | ||
function bigExt (file) { | ||
return path.extname(file).toUpperCase(); | ||
return path.extname(file).toUpperCase() | ||
} | ||
function bigBas (file) { | ||
return path.basename(file).toUpperCase(); | ||
return path.basename(file).toUpperCase() | ||
} | ||
function bigCrypto () { | ||
return crypto; | ||
return crypto | ||
} | ||
module.exports = { | ||
bigBar: bigBar | ||
, bigRab: bigRab | ||
, bigExt: bigExt | ||
, bigBas: bigBas | ||
, boof: boof | ||
, foonum: foonum | ||
, foobool: foobool | ||
, bigCrypto: bigCrypto | ||
, state: '' | ||
}; | ||
bigBar: bigBar, | ||
bigRab: bigRab, | ||
bigExt: bigExt, | ||
bigBas: bigBas, | ||
boof: boof, | ||
foonum: foonum, | ||
foobool: foobool, | ||
fooarray: fooarray, | ||
bigCrypto: bigCrypto, | ||
state: '' | ||
} |
@@ -1,1 +0,1 @@ | ||
module.exports = true | ||
module.exports = true |
@@ -1,1 +0,1 @@ | ||
module.exports = 3 | ||
module.exports = 3 |
@@ -1,5 +0,5 @@ | ||
var baz = require('./baz'); | ||
var baz = require('./baz') | ||
module.exports = function() { | ||
return baz.method(); | ||
module.exports = function () { | ||
return baz.method() | ||
} |
module.exports = { | ||
method: function() { | ||
return false; | ||
method: function () { | ||
return false | ||
} | ||
} |
module.exports = function() { | ||
var bar = require('./bar'); | ||
module.exports = function () { | ||
var bar = require('./bar') | ||
return bar(); | ||
return bar() | ||
} |
@@ -1,5 +0,5 @@ | ||
var bar = require('./bar'); | ||
var bar = require('./bar') | ||
module.exports = function() { | ||
return bar(); | ||
module.exports = function () { | ||
return bar() | ||
} |
@@ -1,5 +0,5 @@ | ||
var bar = require('/not/existing/bar.json'); | ||
var bar = require('/not/existing/bar.json') | ||
module.exports = { | ||
config: bar.config | ||
}; | ||
} |
@@ -1,6 +0,6 @@ | ||
var fooRequires = 0; | ||
var fooRequires = 0 | ||
module.exports = { | ||
fooRequires: function () { return fooRequires; } | ||
, incFooRequires: function () { fooRequires++; } | ||
, reset: function () { fooRequires = 0; } | ||
}; | ||
fooRequires: function () { return fooRequires }, | ||
incFooRequires: function () { fooRequires++ }, | ||
reset: function () { fooRequires = 0 } | ||
} |
@@ -1,5 +0,5 @@ | ||
var baz = require('./baz'); | ||
var baz = require('./baz') | ||
module.exports = { | ||
baz: baz | ||
}; | ||
} |
module.exports = { | ||
testexport: 'test' | ||
}; | ||
} |
@@ -1,3 +0,3 @@ | ||
var baz = require('./baz'); | ||
var bar = require('./bar'); | ||
var baz = require('./baz') | ||
var bar = require('./bar') | ||
@@ -7,2 +7,2 @@ module.exports = { | ||
bar: bar | ||
}; | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
64350
58
1382
5
1