From cb725be3826eef02a0f9d6febca69db30bed0783 Mon Sep 17 00:00:00 2001 From: Thomas Reggi Date: Thu, 19 Apr 2018 00:58:28 -0400 Subject: [PATCH 01/10] readme: fix simple-get example Closes #199 --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5470fe3..15742af 100644 --- a/README.md +++ b/README.md @@ -68,11 +68,13 @@ module.exports = function fetch (callback) { ``` ```js -var proxyquire = require('proxyquire'); +var proxyquire = require('proxyquire').noCallThru(); +var assert = require('assert'); + var fetch = proxyquire('./get', { 'simple-get': function (url, callback) { process.nextTick(function () { - callback(null, fakeResponse) + callback(null, { statusCode: 200 }) }) } }); From 3da0603d1bcb88a1086e8fc721151eefb96c7d17 Mon Sep 17 00:00:00 2001 From: Lou Bichard Date: Thu, 14 Mar 2019 16:01:59 +0000 Subject: [PATCH 02/10] Add file path reference to readme (#237) --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 15742af..24ecb31 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,8 @@ assert.equal(foo.basenameAllCaps('/a/b/file.txt'), 'FILE.TXT'); You can also replace functions directly: +**get.js:** + ```js var get = require('simple-get'); var assert = require('assert'); @@ -67,6 +69,8 @@ module.exports = function fetch (callback) { }; ``` +**get.test.js:** + ```js var proxyquire = require('proxyquire').noCallThru(); var assert = require('assert'); From 12606d1bad1cec34653061f66fa7c8516f6480ce Mon Sep 17 00:00:00 2001 From: Max Nanasy Date: Sat, 1 Jun 2019 12:38:16 -0700 Subject: [PATCH 03/10] README.md: Fix typo (#241) A sentence says to use `require.preserveCache` above a code example that uses `proxyquire.preserveCache`, so I changed the sentence to match the code --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 24ecb31..f478509 100644 --- a/README.md +++ b/README.md @@ -252,7 +252,7 @@ assert.notEqual(foo1, foo2); assert.notEqual(foo1, foo3); ``` -`require.preserveCache` allows you to restore the behavior to match nodejs's `require` again. +`proxyquire.preserveCache` allows you to restore the behavior to match nodejs's `require` again. ```js proxyquire.preserveCache(); From 3ca62af56e4683f31791b64baab2000ad21e2491 Mon Sep 17 00:00:00 2001 From: Thorsten Lorenz Date: Sat, 22 Jun 2019 18:41:58 -0500 Subject: [PATCH 04/10] funding: adding github funding spec --- .github/FUNDING.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..b4cdfea --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,2 @@ +github: thlorenz +patreon: thlorenz From 3905975874c41480e995a017dab091fa2d545290 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Del=20=C3=81guila?= Date: Fri, 12 Jul 2019 10:55:44 -0600 Subject: [PATCH 05/10] Update dependencies (#243) Notably updates resolve, improving perf --- README.md | 14 +++--- examples/api/api-test.js | 40 ++++++++--------- examples/async/foo-tests.js | 10 ++--- examples/simple/foo.inlineoverride.test.js | 6 +-- examples/simple/foo.test.js | 8 ++-- examples/sinon/foo-tests.js | 16 +++---- package.json | 10 ++--- test/proxyquire-api.js | 6 +-- test/proxyquire-cache.js | 50 +++++++++++----------- test/proxyquire-global.js | 18 ++++---- test/proxyquire-independence.js | 8 ++-- test/proxyquire-non-object.js | 38 ++++++++-------- test/proxyquire-notexisting.js | 4 +- test/proxyquire-relative-paths.js | 4 +- test/proxyquire-remove.js | 2 +- test/proxyquire-sub-dependencies.js | 4 +- test/proxyquire.js | 32 +++++++------- test/samples/cache/foo.js | 2 +- 18 files changed, 136 insertions(+), 136 deletions(-) diff --git a/README.md b/README.md index f478509..ff464d4 100644 --- a/README.md +++ b/README.md @@ -44,16 +44,16 @@ var proxyquire = require('proxyquire') // when no overrides are specified, path.extname behaves normally var foo = proxyquire('./foo', { 'path': pathStub }); -assert.equal(foo.extnameAllCaps('file.txt'), '.TXT'); +assert.strictEqual(foo.extnameAllCaps('file.txt'), '.TXT'); // override path.extname 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.strictEqual(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.strictEqual(foo.basenameAllCaps('/a/b/file.txt'), 'FILE.TXT'); ``` You can also replace functions directly: @@ -248,8 +248,8 @@ var foo2 = proxyquire('./foo', stubs); var foo3 = require('./foo'); // foo1, foo2 and foo3 are different instances of the same module -assert.notEqual(foo1, foo2); -assert.notEqual(foo1, foo3); +assert.notStrictEqual(foo1, foo2); +assert.notStrictEqual(foo1, foo3); ``` `proxyquire.preserveCache` allows you to restore the behavior to match nodejs's `require` again. @@ -262,8 +262,8 @@ var foo2 = proxyquire('./foo', stubs); var foo3 = require('./foo'); // foo1, foo2 and foo3 are the same instance -assert.equal(foo1, foo2); -assert.equal(foo1, foo3); +assert.strictEqual(foo1, foo2); +assert.strictEqual(foo1, foo3); ``` diff --git a/examples/api/api-test.js b/examples/api/api-test.js index 577c965..9ddabd4 100644 --- a/examples/api/api-test.js +++ b/examples/api/api-test.js @@ -14,34 +14,34 @@ foo = proxyquire('./samples/foo', { }) fooCut = proxyquire('./samples/foo', { './bar': cutBarStub }) fooWild = proxyquire('./samples/foo', { './bar': wildBarStub }) -assert.equal(stats.fooRequires(), 3) +assert.strictEqual(stats.fooRequires(), 3) -assert.equal(foo.bigBar(), 'BAR') -assert.equal(fooCut.bigBar(), 'BARBER') -assert.equal(fooWild.bigBar(), 'BARBAR') +assert.strictEqual(foo.bigBar(), 'BAR') +assert.strictEqual(fooCut.bigBar(), 'BARBER') +assert.strictEqual(fooWild.bigBar(), 'BARBAR') // non overriden keys call thru by default -assert.equal(foo.bigRab(), 'RAB') -assert.equal(fooCut.bigRab(), 'RAB') +assert.strictEqual(foo.bigRab(), 'RAB') +assert.strictEqual(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.strictEqual(foo.bigExt(file), '.EXT') +assert.strictEqual(fooCut.bigExt(file), '.EXT') +assert.strictEqual(fooWild.bigExt(file), '.EXT') +assert.strictEqual(foo.bigBas(file), 'TEST.EXT') +assert.strictEqual(fooCut.bigBas(file), 'TEST.EXT') +assert.strictEqual(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') +assert.strictEqual(fooCut.bigBar(), 'FRISEUR') +assert.strictEqual(fooCut.bigRab(), 'RABARBER') // autofilling keys on delete only works for inline requires cutBarStub.bar = undefined -assert.equal(fooCut.bigBar(), 'BAR') +assert.strictEqual(fooCut.bigBar(), 'BAR') cutBarStub.rab = undefined assert.throws(fooCut.bigRab) @@ -54,8 +54,8 @@ foo = proxyquire('./samples/foo', { } }) -assert.equal(foo.bigExt(file), 'EXTERMINATE, EXTERMINATE THE /SOME/PATH/TEST.EXT') -assert.equal(foo.bigBas(file), 'TEST.EXT') +assert.strictEqual(foo.bigExt(file), 'EXTERMINATE, EXTERMINATE THE /SOME/PATH/TEST.EXT') +assert.strictEqual(foo.bigBas(file), 'TEST.EXT') // turned off foo = proxyquire('./samples/foo', { @@ -65,7 +65,7 @@ foo = proxyquire('./samples/foo', { } }) -assert.equal(foo.bigExt(file), 'EXTERMINATE, EXTERMINATE THE /SOME/PATH/TEST.EXT') +assert.strictEqual(foo.bigExt(file), 'EXTERMINATE, EXTERMINATE THE /SOME/PATH/TEST.EXT') assert.throws(foo.bigBas) // turned off globally @@ -92,7 +92,7 @@ foo = proxyquire } }) -assert.equal(foo.bigBas(file), 'TEST.EXT') +assert.strictEqual(foo.bigBas(file), 'TEST.EXT') // turned back on globally @@ -104,7 +104,7 @@ foo = proxyquire } }) -assert.equal(foo.bigBas(file), 'TEST.EXT') +assert.strictEqual(foo.bigBas(file), 'TEST.EXT') // turned back off per module diff --git a/examples/async/foo-tests.js b/examples/async/foo-tests.js index 67ebd5f..b632cab 100644 --- a/examples/async/foo-tests.js +++ b/examples/async/foo-tests.js @@ -18,13 +18,13 @@ var foo = proxyquire('./foo', { fs: fsStub }) /* * Test caps locking of returned files */ -fsStub.readdir = function (dir, cb) { cb(null, [ 'file1', 'file2' ]) } +fsStub.readdir = function (dir, cb) { cb(null, ['file1', 'file2']) } calledBack = false foo.filesAllCaps('./somedir', function (err, files) { - assert.equal(err, null) - assert.equal(files[0], 'FILE1') - assert.equal(files[1], 'FILE2') + assert.strictEqual(err, null) + assert.strictEqual(files[0], 'FILE1') + assert.strictEqual(files[1], 'FILE2') calledBack = true }) @@ -38,7 +38,7 @@ assert(calledBack) fsStub.readdir = function (dir, cb) { cb(readdirError) } foo.filesAllCaps('./somedir', function (err, files) { - assert.equal(err, readdirError) + assert.strictEqual(err, readdirError) }) console.log('*** All asserts passed ***') diff --git a/examples/simple/foo.inlineoverride.test.js b/examples/simple/foo.inlineoverride.test.js index 33c034b..f0d56d4 100644 --- a/examples/simple/foo.inlineoverride.test.js +++ b/examples/simple/foo.inlineoverride.test.js @@ -10,7 +10,7 @@ var foo // no overrides yet, so path.extname behaves normally foo = proxyquire('./foo', {}) -assert.equal(foo.extnameAllCaps('file.txt'), '.TXT') +assert.strictEqual(foo.extnameAllCaps('file.txt'), '.TXT') // override path.extname foo = proxyquire('./foo', { @@ -18,9 +18,9 @@ foo = proxyquire('./foo', { }) // path.extname now behaves as we told it to -assert.equal(foo.extnameAllCaps('file.txt'), 'EXTERMINATE, EXTERMINATE THE FILE.TXT') +assert.strictEqual(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.strictEqual(foo.basenameAllCaps('/a/b/file.txt'), 'FILE.TXT') console.log('*** All asserts passed ***') diff --git a/examples/simple/foo.test.js b/examples/simple/foo.test.js index fa43707..a103e88 100644 --- a/examples/simple/foo.test.js +++ b/examples/simple/foo.test.js @@ -9,16 +9,16 @@ 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.strictEqual(foo.extnameAllCaps('file.txt'), '.TXT') // override path.extname 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.strictEqual(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.strictEqual(foo.basenameAllCaps('/a/b/file.txt'), 'FILE.TXT') console.log('*** All asserts passed ***') diff --git a/examples/sinon/foo-tests.js b/examples/sinon/foo-tests.js index 79a9847..9169518 100644 --- a/examples/sinon/foo-tests.js +++ b/examples/sinon/foo-tests.js @@ -30,7 +30,7 @@ describe('when path.extname(file) returns ".markdown"', function () { }) it('extnameAllCaps returns ".MARKDOWN"', function () { - assert.equal(foo.extnameAllCaps(file), '.MARKDOWN') + assert.strictEqual(foo.extnameAllCaps(file), '.MARKDOWN') }) }) @@ -42,7 +42,7 @@ describe('when fs.readdir calls back with ["file1", "file2"]', function () { 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 () { @@ -51,9 +51,9 @@ describe('when fs.readdir calls back with ["file1", "file2"]', function () { 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') + assert.strictEqual(err, null) + assert.strictEqual(files[0], 'FILE1') + assert.strictEqual(files[1], 'FILE2') done() }) }) @@ -77,8 +77,8 @@ describe('when fs.readdir returns an error', function () { it('filesAllCaps calls back with that error', function (done) { foo.filesAllCaps('../simple', function (err, files) { - assert.equal(err, readdirError) - assert.equal(files, null) + assert.strictEqual(err, readdirError) + assert.strictEqual(files, null) done() }) }) @@ -101,7 +101,7 @@ describe('when calling filesAllCaps with "../simple"', function () { foo.filesAllCaps('../simple', function (err, files) { assert.ifError(err) assert(fs.readdir.calledOnce) - assert.equal(fs.readdir.getCall(0).args[0], '../simple') + assert.strictEqual(fs.readdir.getCall(0).args[0], '../simple') done() }) }) diff --git a/package.json b/package.json index 8fec26b..5fc9e09 100644 --- a/package.json +++ b/package.json @@ -26,14 +26,14 @@ "devDependencies": { "mocha": "^5.2.0", "native-hello-world": "^1.0.0", - "should": "~13.2", - "sinon": "~6.1", - "standard": "^11.0.0" + "should": "^13.2.3", + "sinon": "^7.3.2", + "standard": "^13.0.1" }, "dependencies": { "fill-keys": "^1.0.2", - "module-not-found-error": "^1.0.0", - "resolve": "~1.8.1" + "module-not-found-error": "^1.0.1", + "resolve": "^1.11.1" }, "standard": { "env": [ diff --git a/test/proxyquire-api.js b/test/proxyquire-api.js index df9408a..6676698 100644 --- a/test/proxyquire-api.js +++ b/test/proxyquire-api.js @@ -17,21 +17,21 @@ describe('api', function () { it('proxyquire can load', function () { var proxiedFoo = proxyquire.load('./samples/foo', stubs) - assert.equal(typeof proxiedFoo, 'object') + assert.strictEqual(typeof proxiedFoo, 'object') assert.notStrictEqual(realFoo, proxiedFoo) }) it('proxyquire can callThru and then load', function () { var proxiedFoo = proxyquire.callThru().load('./samples/foo', stubs) - assert.equal(typeof proxiedFoo, 'object') + assert.strictEqual(typeof proxiedFoo, 'object') assert.notStrictEqual(realFoo, proxiedFoo) }) it('proxyquire can noCallThru and then load', function () { var proxiedFoo = proxyquire.noCallThru().load('./samples/foo', stubs) - assert.equal(typeof proxiedFoo, 'object') + assert.strictEqual(typeof proxiedFoo, 'object') assert.notStrictEqual(realFoo, proxiedFoo) }) }) diff --git a/test/proxyquire-cache.js b/test/proxyquire-cache.js index a53eb68..62e65b2 100644 --- a/test/proxyquire-cache.js +++ b/test/proxyquire-cache.js @@ -9,11 +9,11 @@ describe('Proxyquire', function () { original.state = 'cached' var proxyquire = require('..') - proxyquire('./samples/foo', { 'path': { } }) + proxyquire('./samples/foo', { path: { } }) var foo = require('./samples/foo') - assert.equal('cached', foo.state) - assert.equal(foo, original) + assert.strictEqual('cached', foo.state) + assert.strictEqual(foo, original) }) it('does not pollute the cache when module is proxyquired before it is loaded', function () { @@ -22,14 +22,14 @@ describe('Proxyquire', function () { proxyquire('./samples/no-call-thru-test', { './required': false }) var original = require('./samples/no-call-thru-test') - assert.equal(original.original, true) + assert.strictEqual(original.original, true) }) }) describe('preserveCache()', function () { it('returns a reference to itself, so it can be chained', function () { var proxyquire = require('..') - assert.equal(proxyquire.preserveCache(), proxyquire) + assert.strictEqual(proxyquire.preserveCache(), proxyquire) }) it('has Proxyquire restore the cache for the module', function () { @@ -38,11 +38,11 @@ describe('Proxyquire', function () { var proxyquire = require('..') proxyquire.preserveCache() - proxyquire.load('./samples/foo', { 'path': { } }) + proxyquire.load('./samples/foo', { path: { } }) var foo = require('./samples/foo') - assert.equal('cached', foo.state) - assert.equal(foo, original) + assert.strictEqual('cached', foo.state) + assert.strictEqual(foo, original) }) it('allows Singletons to function properly', function () { @@ -50,17 +50,17 @@ describe('Proxyquire', function () { var proxyquire = require('..') proxyquire.preserveCache() - proxyquire.load('./samples/foo-singleton', { 'path': { } }).getInstance() + proxyquire.load('./samples/foo-singleton', { path: { } }).getInstance() var fooSingleton = require('./samples/foo-singleton').getInstance() - assert.equal(fooSingleton, original) + assert.strictEqual(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) + assert.strictEqual(proxyquire.noPreserveCache(), proxyquire) }) it('forces subsequent requires to reload the proxied module', function () { @@ -68,24 +68,24 @@ describe('Proxyquire', function () { original.state = 'cached' var proxyquire = require('..') - proxyquire.load('./samples/foo', { 'path': { } }) + proxyquire.load('./samples/foo', { path: { } }) var cacheFoo = require('./samples/foo') - assert.equal('cached', cacheFoo.state) - assert.equal(cacheFoo, original) + assert.strictEqual('cached', cacheFoo.state) + assert.strictEqual(cacheFoo, original) proxyquire.noPreserveCache() - proxyquire.load('./samples/foo', { 'path': { } }) + proxyquire.load('./samples/foo', { path: { } }) var foo = require('./samples/foo') - assert.equal('', foo.state) - assert.notEqual(foo, original) + assert.strictEqual('', foo.state) + assert.notStrictEqual(foo, original) }) it('deletes the require.cache for the module being stubbed', function () { var proxyquire = require('..').noPreserveCache() - proxyquire.load('./samples/foo', { 'path': { } }) - assert.equal(undefined, require.cache[require.resolve('./samples/foo')]) + proxyquire.load('./samples/foo', { path: { } }) + assert.strictEqual(undefined, require.cache[require.resolve('./samples/foo')]) }) it('deletes the require.cache for the stubs', function () { @@ -96,15 +96,15 @@ describe('Proxyquire', function () { bar.f.g = function () { return 'a' } bar.h = function () { return 'a' } - assert.equal(foo.bar.f.g(), 'a') - assert.equal(foo.bar.h(), 'a') + assert.strictEqual(foo.bar.f.g(), 'a') + assert.strictEqual(foo.bar.h(), 'a') foo = proxyquire.load('./samples/cache/foo', { './bar': {} }) - assert.equal(foo.bar.h(), 'h') - assert.equal(foo.bar.f.g(), 'g') + assert.strictEqual(foo.bar.h(), 'h') + assert.strictEqual(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.strictEqual(undefined, require.cache[require.resolve('./samples/cache/foo')]) + assert.strictEqual(undefined, require.cache[require.resolve('./samples/cache/bar')]) }) it('silences errors when stub lookups fail', function () { diff --git a/test/proxyquire-global.js b/test/proxyquire-global.js index c16a450..6fcd466 100644 --- a/test/proxyquire-global.js +++ b/test/proxyquire-global.js @@ -18,8 +18,8 @@ describe('global flags set', function () { var proxiedFoo = proxyquire('./samples/global/foo', stubs) - assert.equal(realFoo(), false) - assert.equal(proxiedFoo(), true) + assert.strictEqual(realFoo(), false) + assert.strictEqual(proxiedFoo(), true) }) it('should override require globally even when require\'s execution is deferred', function () { @@ -34,13 +34,13 @@ describe('global flags set', function () { var proxiedFoo = proxyquire('./samples/global/foo-deferred', stubs) - assert.equal(realFoo(), false) - assert.equal(proxiedFoo(), true) + assert.strictEqual(realFoo(), false) + assert.strictEqual(proxiedFoo(), true) }) it('should not throw when a native module is required a second time', function () { var stubs = { - 'foo': { + foo: { '@global': true } } @@ -62,8 +62,8 @@ describe('global flags not set', function () { var proxiedFoo = proxyquire('./samples/global/foo', stubs) - assert.equal(realFoo(), false) - assert.equal(proxiedFoo(), false) + assert.strictEqual(realFoo(), false) + assert.strictEqual(proxiedFoo(), false) }) it('should not override require globally even when require\'s execution is deferred', function () { @@ -77,7 +77,7 @@ describe('global flags not set', function () { var proxiedFoo = proxyquire('./samples/global/foo-deferred', stubs) - assert.equal(realFoo(), false) - assert.equal(proxiedFoo(), false) + assert.strictEqual(realFoo(), false) + assert.strictEqual(proxiedFoo(), false) }) }) diff --git a/test/proxyquire-independence.js b/test/proxyquire-independence.js index 7dc5485..22ab6c4 100644 --- a/test/proxyquire-independence.js +++ b/test/proxyquire-independence.js @@ -17,11 +17,11 @@ describe('Multiple requires of same module don\'t affect each other', function ( }) it('foo1.bigBar() == "BAR1"', function () { - assert.equal(foo1.bigBar(), 'BAR1') + assert.strictEqual(foo1.bigBar(), 'BAR1') }) it('foo2.bigBar() == "BAR2"', function () { - assert.equal(foo2.bigBar(), 'BAR2') + assert.strictEqual(foo2.bigBar(), 'BAR2') }) describe('and I change bar1.bar() to return barone', function () { @@ -30,11 +30,11 @@ describe('Multiple requires of same module don\'t affect each other', function ( }) it('foo1.bigBar() == "BARONE"', function () { - assert.equal(foo1.bigBar(), 'BARONE') + assert.strictEqual(foo1.bigBar(), 'BARONE') }) it('foo2.bigBar() == "BAR2"', function () { - assert.equal(foo2.bigBar(), 'BAR2') + assert.strictEqual(foo2.bigBar(), 'BAR2') }) }) }) diff --git a/test/proxyquire-non-object.js b/test/proxyquire-non-object.js index 76895a2..1314805 100644 --- a/test/proxyquire-non-object.js +++ b/test/proxyquire-non-object.js @@ -14,16 +14,16 @@ describe('Given foo requires the boof, foonum and foobool modules and boof is a describe('When I resolve foo with boofber stub as boof.', function () { before(function () { stats.reset() - foo = proxyquire('./samples/foo', {'./boof': boofber}) + foo = proxyquire('./samples/foo', { './boof': boofber }) }) it('foo is required 1 times', function () { - assert.equal(stats.fooRequires(), 1) + assert.strictEqual(stats.fooRequires(), 1) }) describe('foo\'s boof is boofber', function () { it('foo.boof == boofber', function () { - assert.equal(foo.boof, boofber) + assert.strictEqual(foo.boof, boofber) }) }) }) @@ -31,16 +31,16 @@ describe('Given foo requires the boof, foonum and foobool modules and boof is a describe('When I resolve foo with foonumber stub as foonum.', function () { before(function () { stats.reset() - foo = proxyquire('./samples/foo', {'./foonum': foonumber}) + foo = proxyquire('./samples/foo', { './foonum': foonumber }) }) it('foo is required 1 times', function () { - assert.equal(stats.fooRequires(), 1) + assert.strictEqual(stats.fooRequires(), 1) }) describe('foo\'s foonum is foonumber', function () { it('foo.foonum == foonumber', function () { - assert.equal(foo.foonum, foonumber) + assert.strictEqual(foo.foonum, foonumber) }) }) }) @@ -48,16 +48,16 @@ describe('Given foo requires the boof, foonum and foobool modules and boof is a describe('When I resolve foo with fooboolber stub as foobool.', function () { before(function () { stats.reset() - foo = proxyquire('./samples/foo', {'./foobool': fooboolber}) + foo = proxyquire('./samples/foo', { './foobool': fooboolber }) }) it('foo is required 1 times', function () { - assert.equal(stats.fooRequires(), 1) + assert.strictEqual(stats.fooRequires(), 1) }) describe('foo\'s foobool is fooboolber', function () { it('foo.foobool == fooboolber', function () { - assert.equal(foo.foobool, fooboolber) + assert.strictEqual(foo.foobool, fooboolber) }) }) }) @@ -65,16 +65,16 @@ describe('Given foo requires the boof, foonum and foobool modules and boof is a describe('When I resolve foo with ./fooarray stub as fooarray.', function () { before(function () { stats.reset() - foo = proxyquire('./samples/foo', {'./fooarray': fooarray}) + foo = proxyquire('./samples/foo', { './fooarray': fooarray }) }) it('foo is required 1 times', function () { - assert.equal(stats.fooRequires(), 1) + assert.strictEqual(stats.fooRequires(), 1) }) describe('foo\'s fooarray is fooarray', function () { it('foo.fooarray is fooarray', function () { - assert.deepEqual(foo.fooarray, ['x', 'y', 'z']) + assert.deepStrictEqual(foo.fooarray, ['x', 'y', 'z']) }) }) }) @@ -82,16 +82,16 @@ describe('Given foo requires the boof, foonum and foobool modules and boof is a describe('When I resolve foo with ./fooarray stub as empty.', function () { before(function () { stats.reset() - foo = proxyquire('./samples/foo', {'./fooarray': []}) + foo = proxyquire('./samples/foo', { './fooarray': [] }) }) it('foo is required 1 times', function () { - assert.equal(stats.fooRequires(), 1) + assert.strictEqual(stats.fooRequires(), 1) }) describe('foo\'s fooarray is the original', function () { it('foo.fooarray is empty', function () { - assert.deepEqual(foo.fooarray, ['a', 'b', 'c']) + assert.deepStrictEqual(foo.fooarray, ['a', 'b', 'c']) }) }) }) @@ -100,17 +100,17 @@ describe('Given foo requires the boof, foonum and foobool modules and boof is a before(function () { stats.reset() var empty = [] - Object.defineProperty(empty, '@noCallThru', {value: true}) - foo = proxyquire('./samples/foo', {'./fooarray': 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) + assert.strictEqual(stats.fooRequires(), 1) }) describe('foo\'s fooarray is empty', function () { it('foo.fooarray is empty', function () { - assert.deepEqual(foo.fooarray, []) + assert.deepStrictEqual(foo.fooarray, []) }) }) }) diff --git a/test/proxyquire-notexisting.js b/test/proxyquire-notexisting.js index 0a9085d..96f35a3 100644 --- a/test/proxyquire-notexisting.js +++ b/test/proxyquire-notexisting.js @@ -20,7 +20,7 @@ describe('When resolving foo that requires stubbed /not/existing/bar.json with @ var foo = proxyquire(fooPath, { '/not/existing/bar.json': { config: 'bar\'s config', '@noCallThru': true } }) - assert.equal(foo.config, 'bar\'s config') + assert.strictEqual(foo.config, 'bar\'s config') }) }) @@ -30,7 +30,7 @@ describe('When resolving foo that requires stubbed /not/existing/bar.json with n var foo = proxyquire(fooPath, { '/not/existing/bar.json': { config: 'bar\'s config' } }) - assert.equal(foo.config, 'bar\'s config') + assert.strictEqual(foo.config, 'bar\'s config') proxyquire.callThru() }) }) diff --git a/test/proxyquire-relative-paths.js b/test/proxyquire-relative-paths.js index 27fcb4f..942512a 100644 --- a/test/proxyquire-relative-paths.js +++ b/test/proxyquire-relative-paths.js @@ -6,7 +6,7 @@ var proxyquire = require('..') describe('When requiring relative paths, they should be relative to the proxyrequired module', function () { it('should return the correct result', function () { - var result = proxyquire('./samples/relative-paths/a/index.js', {'./util': {c: 'c'}}) - result.should.eql({a: 'a', c: 'c'}) + var result = proxyquire('./samples/relative-paths/a/index.js', { './util': { c: 'c' } }) + result.should.eql({ a: 'a', c: 'c' }) }) }) diff --git a/test/proxyquire-remove.js b/test/proxyquire-remove.js index 0f24216..1925543 100644 --- a/test/proxyquire-remove.js +++ b/test/proxyquire-remove.js @@ -17,6 +17,6 @@ describe('When resolving foo that requires nulled file package', function () { describe('When resolving foo that optionally requires nulled crypto package', function () { it('catches when resolving crypto', function () { var foo = proxyquire(fooPath, { crypto: null }) - assert.equal(foo.bigCrypto(), 'caught') + assert.strictEqual(foo.bigCrypto(), 'caught') }) }) diff --git a/test/proxyquire-sub-dependencies.js b/test/proxyquire-sub-dependencies.js index 94a41dd..10a906b 100644 --- a/test/proxyquire-sub-dependencies.js +++ b/test/proxyquire-sub-dependencies.js @@ -22,10 +22,10 @@ describe('When resolving foo that requires bar and stubbed baz where bar require }) it('does not stub baz in bar', function () { - assert.equal(foo.bar.baz.testexport, 'test') + assert.strictEqual(foo.bar.baz.testexport, 'test') }) it('does not affect a normal baz import', function () { - assert.equal(baz.testexport, 'test') + assert.strictEqual(baz.testexport, 'test') }) }) diff --git a/test/proxyquire.js b/test/proxyquire.js index 621da8b..2886eee 100644 --- a/test/proxyquire.js +++ b/test/proxyquire.js @@ -19,26 +19,26 @@ describe('Given foo requires the bar and path modules and bar.bar() returns "bar }) it('foo is required 2 times', function () { - assert.equal(stats.fooRequires(), 2) + assert.strictEqual(stats.fooRequires(), 2) }) describe('foo\'s bar is unchanged', function () { it('foo.bigBar() == "BAR"', function () { - assert.equal(foo.bigBar(), 'BAR') + assert.strictEqual(foo.bigBar(), 'BAR') }) }) describe('only stubbed modules have overrides in foober', function () { it('foober.bigBar() == "BARBER"', function () { - assert.equal(foober.bigBar(), 'BARBER') + assert.strictEqual(foober.bigBar(), 'BARBER') }) it('foober.bigExt("/folder/test.ext") == ".EXT"', function () { - assert.equal(foober.bigExt(file), '.EXT') + assert.strictEqual(foober.bigExt(file), '.EXT') }) it('foober.bigBas("/folder/test.ext") == "TEST.EXT"', function () { - assert.equal(foober.bigBas(file), 'TEST.EXT') + assert.strictEqual(foober.bigBas(file), 'TEST.EXT') }) }) @@ -49,11 +49,11 @@ describe('Given foo requires the bar and path modules and bar.bar() returns "bar }) it('overrides behavior when module is required inside function call', function () { - assert.equal(foober.bigBar(), 'FRISEUR') + assert.strictEqual(foober.bigBar(), 'FRISEUR') }) it('overrides behavior when module is required on top of file', function () { - assert.equal(foober.bigRab(), 'RABARBER') + assert.strictEqual(foober.bigRab(), 'RABARBER') }) describe('and then delete overrides of stubs after resolve', function () { @@ -63,7 +63,7 @@ describe('Given foo requires the bar and path modules and bar.bar() returns "bar }) it('reverts to original behavior when module is required inside function call', function () { - assert.equal(foober.bigBar(), 'BAR') + assert.strictEqual(foober.bigBar(), 'BAR') }) it('doesn\'t properly revert to original behavior when module is required on top of file ', function () { @@ -85,11 +85,11 @@ describe('Given foo requires the bar and path modules and bar.bar() returns "bar }) it('foo.bigExt(file) == "OVERRIDE /FOLDER/TEST.EXT"', function () { - assert.equal(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT') + assert.strictEqual(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT') }) it('foo.bigBas(file) == "TEST.EXT"', function () { - assert.equal(foo.bigBas(file), 'TEST.EXT') + assert.strictEqual(foo.bigBas(file), 'TEST.EXT') }) }) @@ -103,7 +103,7 @@ describe('Given foo requires the bar and path modules and bar.bar() returns "bar }) it('foo.bigExt(file) == "OVERRIDE /FOLDER/TEST.EXT"', function () { - assert.equal(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT') + assert.strictEqual(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT') }) it('foo.bigBas(file) throws', function () { @@ -127,7 +127,7 @@ describe('Given foo requires the bar and path modules and bar.bar() returns "bar }) it('foo.bigExt(file) == "OVERRIDE /FOLDER/TEST.EXT"', function () { - assert.equal(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT') + assert.strictEqual(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT') }) it('foo.bigBas(file) throws', function () { @@ -145,11 +145,11 @@ describe('Given foo requires the bar and path modules and bar.bar() returns "bar }) it('foo.bigExt(file) == "OVERRIDE /FOLDER/TEST.EXT"', function () { - assert.equal(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT') + assert.strictEqual(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT') }) it('foo.bigBas(file) == "TEST.EXT"', function () { - assert.equal(foo.bigBas(file), 'TEST.EXT') + assert.strictEqual(foo.bigBas(file), 'TEST.EXT') }) }) @@ -165,11 +165,11 @@ describe('Given foo requires the bar and path modules and bar.bar() returns "bar }) it('foo.bigExt(file) == "OVERRIDE /FOLDER/TEST.EXT"', function () { - assert.equal(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT') + assert.strictEqual(foo.bigExt(file), 'OVERRIDE /FOLDER/TEST.EXT') }) it('foo.bigBas(file) == "TEST.EXT"', function () { - assert.equal(foo.bigBas(file), 'TEST.EXT') + assert.strictEqual(foo.bigBas(file), 'TEST.EXT') }) }) }) diff --git a/test/samples/cache/foo.js b/test/samples/cache/foo.js index 4efe8b4..0f19cde 100644 --- a/test/samples/cache/foo.js +++ b/test/samples/cache/foo.js @@ -1,3 +1,3 @@ var bar = require('./bar') -module.exports = {bar: bar} +module.exports = { bar: bar } From 5b88b0798f57767af63c89292faa0357181db8ea Mon Sep 17 00:00:00 2001 From: Ben Drucker Date: Fri, 12 Jul 2019 09:56:05 -0700 Subject: [PATCH 06/10] 2.1.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5fc9e09..bda1e54 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "proxyquire", - "version": "2.1.0", + "version": "2.1.1", "description": "Proxies nodejs require in order to allow overriding dependencies during testing.", "main": "index.js", "scripts": { From 0297d28db7d02475ff1700820f5e25fc47870df2 Mon Sep 17 00:00:00 2001 From: Brian Valerius Date: Thu, 8 Aug 2019 14:21:52 -0500 Subject: [PATCH 07/10] fix: allow non-existent stub path when @noCallThru is set at stub level (#245) --- lib/proxyquire.js | 28 ++++++++++++++++-------- test/proxyquire-notexisting.js | 10 +++++++++ test/samples/notexisting/foo-relative.js | 5 +++++ 3 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 test/samples/notexisting/foo-relative.js diff --git a/lib/proxyquire.js b/lib/proxyquire.js index a169c38..dec0ba7 100644 --- a/lib/proxyquire.js +++ b/lib/proxyquire.js @@ -132,7 +132,7 @@ Proxyquire.prototype.load = function (request, stubs) { // 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) { +Proxyquire.prototype._resolveModule = function (baseModule, pathToResolve, stubs) { try { return resolve.sync(pathToResolve, { basedir: path.dirname(baseModule), @@ -153,9 +153,22 @@ Proxyquire.prototype._resolveModule = function (baseModule, pathToResolve) { // 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 || this._noCallThru) { - return path.resolve(path.dirname(baseModule), pathToResolve) + // best we can. We also need to check if the relative module has @noCallThru. + var moduleNoCallThru + var resolvedPath + if (hasOwnProperty.call(stubs, pathToResolve)) { + // pathToResolve is currently relative on stubs from _withoutCache() call + moduleNoCallThru = hasOwnProperty.call(stubs[pathToResolve], '@noCallThru') ? stubs[pathToResolve]['@noCallThru'] : undefined + resolvedPath = path.resolve(path.dirname(baseModule), pathToResolve) + } else { + resolvedPath = path.resolve(path.dirname(baseModule), pathToResolve) + if (hasOwnProperty.call(stubs, resolvedPath)) { + // after _withoutCache() alters stubs paths to be absolute + moduleNoCallThru = hasOwnProperty.call(stubs[resolvedPath], '@noCallThru') ? stubs[resolvedPath]['@noCallThru'] : undefined + } + } + if (!this._preserveCache || this._noCallThru || moduleNoCallThru) { + return resolvedPath } throw err @@ -167,10 +180,9 @@ Proxyquire.prototype._require = function (module, stubs, path) { assert(typeof path === 'string', 'path must be a string') assert(path, 'missing path') - var resolvedPath = this._resolveModule(module.filename, path) + var resolvedPath = this._resolveModule(module.filename, path, stubs) 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) @@ -202,11 +214,10 @@ Proxyquire.prototype._withoutCache = function (module, stubs, path, func) { // Resolve all stubs to absolute paths. stubs = Object.keys(stubs) .reduce(function (result, stubPath) { - var resolvedStubPath = this._resolveModule(resolvedPath, stubPath) + var resolvedStubPath = this._resolveModule(resolvedPath, stubPath, stubs) result[resolvedStubPath] = stubs[stubPath] return result }.bind(this), {}) - // Override all require extension handlers var restoreExtensionHandlers = this._overrideExtensionHandlers(module, stubs) @@ -287,7 +298,6 @@ Proxyquire.prototype._disableModuleCache = function (path, module) { Proxyquire.prototype._overrideExtensionHandlers = function (module, resolvedStubs) { /* eslint node/no-deprecated-api: [error, {ignoreGlobalItems: ["require.extensions"]}] */ - var originalExtensions = {} var self = this diff --git a/test/proxyquire-notexisting.js b/test/proxyquire-notexisting.js index 96f35a3..23258a4 100644 --- a/test/proxyquire-notexisting.js +++ b/test/proxyquire-notexisting.js @@ -4,6 +4,7 @@ var assert = require('assert') var proxyquire = require('..') var path = require('path') var fooPath = path.join(__dirname, './samples/notexisting/foo.js') +var fooRelativePath = path.join(__dirname, './samples/notexisting/foo-relative.js') describe('When resolving foo that requires stubbed /not/existing/bar.json', function () { it('throws an error', function () { @@ -34,3 +35,12 @@ describe('When resolving foo that requires stubbed /not/existing/bar.json with n proxyquire.callThru() }) }) + +describe('When resolving foo-relative that requires relative stubbed ../not/existing/bar.json with @noCallThru', function () { + it('resolves foo-relative with stubbed bar', function () { + var fooRelative = proxyquire(fooRelativePath, { + '../not/existing/bar.json': { config: 'bar\'s config', '@noCallThru': true } + }) + assert.strictEqual(fooRelative.config, 'bar\'s config') + }) +}) diff --git a/test/samples/notexisting/foo-relative.js b/test/samples/notexisting/foo-relative.js new file mode 100644 index 0000000..a0d2f9f --- /dev/null +++ b/test/samples/notexisting/foo-relative.js @@ -0,0 +1,5 @@ +var bar = require('../not/existing/bar.json') + +module.exports = { + config: bar.config +} From cc9cdb646512b1fc29adc91772ae5c82b6a4b62d Mon Sep 17 00:00:00 2001 From: Ben Drucker Date: Thu, 8 Aug 2019 22:25:55 -0400 Subject: [PATCH 08/10] 2.1.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bda1e54..c9b1614 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "proxyquire", - "version": "2.1.1", + "version": "2.1.2", "description": "Proxies nodejs require in order to allow overriding dependencies during testing.", "main": "index.js", "scripts": { From 6a01bc10d05589dc7c67b42094c4c5c9898e0a65 Mon Sep 17 00:00:00 2001 From: David Sommerich <45643387+dsommerich@users.noreply.github.com> Date: Mon, 12 Aug 2019 23:54:21 +1000 Subject: [PATCH 09/10] fix: throw correct error when simulating MODULE_NOT_FOUND (#246) --- lib/proxyquire.js | 14 +++++--------- test/proxyquire-remove.js | 2 ++ 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/proxyquire.js b/lib/proxyquire.js index dec0ba7..64dca95 100644 --- a/lib/proxyquire.js +++ b/lib/proxyquire.js @@ -154,18 +154,14 @@ Proxyquire.prototype._resolveModule = function (baseModule, pathToResolve, stubs // 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. We also need to check if the relative module has @noCallThru. + var resolvedPath = path.resolve(path.dirname(baseModule), pathToResolve) var moduleNoCallThru - var resolvedPath - if (hasOwnProperty.call(stubs, pathToResolve)) { + if (hasOwnProperty.call(stubs, pathToResolve) && stubs[pathToResolve]) { // pathToResolve is currently relative on stubs from _withoutCache() call moduleNoCallThru = hasOwnProperty.call(stubs[pathToResolve], '@noCallThru') ? stubs[pathToResolve]['@noCallThru'] : undefined - resolvedPath = path.resolve(path.dirname(baseModule), pathToResolve) - } else { - resolvedPath = path.resolve(path.dirname(baseModule), pathToResolve) - if (hasOwnProperty.call(stubs, resolvedPath)) { - // after _withoutCache() alters stubs paths to be absolute - moduleNoCallThru = hasOwnProperty.call(stubs[resolvedPath], '@noCallThru') ? stubs[resolvedPath]['@noCallThru'] : undefined - } + } else if (hasOwnProperty.call(stubs, resolvedPath) && stubs[resolvedPath]) { + // after _withoutCache() alters stubs paths to be absolute + moduleNoCallThru = hasOwnProperty.call(stubs[resolvedPath], '@noCallThru') ? stubs[resolvedPath]['@noCallThru'] : undefined } if (!this._preserveCache || this._noCallThru || moduleNoCallThru) { return resolvedPath diff --git a/test/proxyquire-remove.js b/test/proxyquire-remove.js index 1925543..bdb8dd3 100644 --- a/test/proxyquire-remove.js +++ b/test/proxyquire-remove.js @@ -10,6 +10,8 @@ describe('When resolving foo that requires nulled file package', function () { it('throws an error', function () { assert.throws(function () { proxyquire(fooPath, { path: null }) + }, function (error) { + return error.code === 'MODULE_NOT_FOUND' }) }) }) From aaae2966fac9643225816156e25b2d5db838d108 Mon Sep 17 00:00:00 2001 From: Ben Drucker Date: Mon, 12 Aug 2019 09:54:37 -0400 Subject: [PATCH 10/10] 2.1.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c9b1614..f6b887d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "proxyquire", - "version": "2.1.2", + "version": "2.1.3", "description": "Proxies nodejs require in order to allow overriding dependencies during testing.", "main": "index.js", "scripts": {