proxyquire
Advanced tools
Comparing version 0.3.5 to 0.4.0
@@ -5,3 +5,3 @@ "use strict"; | ||
, stats = require('./samples/stats') | ||
, proxyquire = require('../../proxyquire') | ||
, proxyquire = require('../..') | ||
, file = '/some/path/test.ext' | ||
@@ -15,5 +15,5 @@ , foo | ||
foo = proxyquire('./samples/foo', __dirname, { }); | ||
fooCut = proxyquire('./samples/foo', __dirname, { './bar': cutBarStub }); | ||
fooWild = proxyquire('./samples/foo', __dirname, { './bar': wildBarStub }); | ||
foo = proxyquire('./samples/foo', { }); | ||
fooCut = proxyquire('./samples/foo', { './bar': cutBarStub }); | ||
fooWild = proxyquire('./samples/foo', { './bar': wildBarStub }); | ||
@@ -55,4 +55,3 @@ assert.equal(stats.fooRequires(), 3); | ||
// not turned off | ||
foo = proxyquire | ||
.resolve('./samples/foo', __dirname, { | ||
foo = proxyquire('./samples/foo', { | ||
path: { | ||
@@ -67,4 +66,3 @@ extname: function (file) { return 'Exterminate, exterminate the ' + file; } | ||
// turned off | ||
foo = proxyquire | ||
.resolve('./samples/foo', __dirname, { | ||
foo = proxyquire('./samples/foo', { | ||
path: { | ||
@@ -84,3 +82,3 @@ extname: function (file) { return 'Exterminate, exterminate the ' + file; } | ||
.noCallThru() | ||
.resolve('./samples/foo', __dirname, { | ||
.load('./samples/foo', { | ||
path: { | ||
@@ -97,3 +95,3 @@ extname: function (file) { return 'Exterminate, exterminate the ' + file; } | ||
.noCallThru() | ||
.resolve('./samples/foo', __dirname, { | ||
.load('./samples/foo', { | ||
path: { | ||
@@ -110,4 +108,4 @@ extname: function (file) { return 'Exterminate, exterminate the ' + file; } | ||
foo = proxyquire | ||
.noCallThru(false) | ||
.resolve('./samples/foo', __dirname, { | ||
.callThru() | ||
.load('./samples/foo', { | ||
path: { | ||
@@ -123,4 +121,4 @@ extname: function (file) { return 'Exterminate, exterminate the ' + file; } | ||
foo = proxyquire | ||
.noCallThru(false) | ||
.resolve('./samples/foo', __dirname, { | ||
.callThru() | ||
.load('./samples/foo', { | ||
path: { | ||
@@ -127,0 +125,0 @@ extname: function (file) { return 'Exterminate, exterminate the ' + file; } |
@@ -8,3 +8,3 @@ "use strict"; | ||
var proxyquire = require('../../proxyquire') | ||
var proxyquire = require('../..') | ||
, assert = require('assert') | ||
@@ -16,3 +16,3 @@ , readdirError = new Error('some error') | ||
var foo = proxyquire('./foo', __dirname, { fs: fsStub }); | ||
var foo = proxyquire('./foo', { fs: fsStub }); | ||
@@ -19,0 +19,0 @@ /* |
@@ -0,4 +1,5 @@ | ||
'use strict'; | ||
require('../example-utils').listModuleAndTests(__dirname + '/foo.js', __filename); | ||
var proxyquire = require('../../proxyquire') | ||
var proxyquire = require('../..') | ||
, assert = require('assert') | ||
@@ -9,7 +10,7 @@ , foo | ||
// no overrides yet, so path.extname behaves normally | ||
foo = proxyquire('./foo', __dirname, {}); | ||
foo = proxyquire('./foo', {}); | ||
assert.equal(foo.extnameAllCaps('file.txt'), '.TXT'); | ||
// override path.extname | ||
foo = proxyquire('./foo', __dirname, { | ||
foo = proxyquire('./foo', { | ||
path: { extname: function (file) { return 'Exterminate, exterminate the ' + file; } } | ||
@@ -16,0 +17,0 @@ }); |
@@ -5,3 +5,3 @@ "use strict"; | ||
var proxyquire = require('../../proxyquire') | ||
var proxyquire = require('../..') | ||
, assert = require('assert') | ||
@@ -11,3 +11,3 @@ , pathStub = { }; | ||
// when not overridden, path.extname behaves normally | ||
var foo = proxyquire('./foo', __dirname, { 'path': pathStub }); | ||
var foo = proxyquire('./foo', { 'path': pathStub }); | ||
assert.equal(foo.extnameAllCaps('file.txt'), '.TXT'); | ||
@@ -14,0 +14,0 @@ |
@@ -11,3 +11,3 @@ "use strict"; | ||
var proxyquire = require('../../proxyquire') | ||
var proxyquire = require('../..') | ||
, sinon = require('sinon') | ||
@@ -27,3 +27,3 @@ , assert = require('assert') | ||
extnameStub = sinon.stub(path, 'extname'); | ||
foo = proxyquire('./foo', __dirname, { path: { extname: extnameStub } } ); | ||
foo = proxyquire('./foo', { path: { extname: extnameStub } } ); | ||
@@ -48,3 +48,3 @@ extnameStub.withArgs(file).returns('.markdown'); | ||
readdirStub = sinon.stub(fs, 'readdir'); | ||
foo = proxyquire('./foo', __dirname, { fs: { readdir: readdirStub } } ); | ||
foo = proxyquire('./foo', { fs: { readdir: readdirStub } } ); | ||
@@ -74,3 +74,3 @@ readdirStub.withArgs('../simple').yields(null, [ 'file1', 'file2' ]); | ||
readdirStub = sinon.stub(fs, 'readdir'); | ||
foo = proxyquire('./foo', __dirname, { fs: { readdir: readdirStub } } ); | ||
foo = proxyquire('./foo', { fs: { readdir: readdirStub } } ); | ||
@@ -101,3 +101,3 @@ readdirError = new Error('some error'); | ||
readdirSpy = sinon.spy(fs, 'readdir'); | ||
foo = proxyquire('./foo', __dirname, { fs: { readdir: readdirSpy } } ); | ||
foo = proxyquire('./foo', { fs: { readdir: readdirSpy } } ); | ||
}) | ||
@@ -104,0 +104,0 @@ |
{ | ||
"name": "proxyquire", | ||
"version": "0.3.5", | ||
"version": "0.4.0", | ||
"description": "Proxies nodejs require in order to allow overriding dependencies during testing.", | ||
"main": "proxyquire.js", | ||
"main": "index.js", | ||
"scripts": { | ||
@@ -7,0 +7,0 @@ "test": "mocha" |
@@ -36,3 +36,3 @@ # proxyquire [![Build Status](https://secure.travis-ci.org/thlorenz/proxyquire.png)](http://travis-ci.org/thlorenz/proxyquire) | ||
// when no overrides are specified, path.extname behaves normally | ||
var foo = proxyquire('./foo', __dirname, { 'path': pathStub }); | ||
var foo = proxyquire('./foo', { 'path': pathStub }); | ||
assert.equal(foo.extnameAllCaps('file.txt'), '.TXT'); | ||
@@ -54,9 +54,10 @@ | ||
- [API](#api) | ||
- [Resolve module to be tested and configure stubs](#resolve-module-to-be-tested-and-configure-stubs) | ||
- [Preventing call thru to original dependency](#preventing-call-thru-to-original-dependency) | ||
- [Changing the TmpDir](#changing-the-tmpdir) | ||
- [Examples](#examples) | ||
- [Preventing call thru to original dependency](#preventing-call-thru-to-original-dependency) | ||
- [Prevent call thru for all future stubs resolved by a proxyquire instance](#prevent-call-thru-for-all-future-stubs-resolved-by-a-proxyquire-instance) | ||
- [Re-enable call thru for all future stubs resolved by a proxyquire instance](#re-enable-call-thru-for-all-future-stubs-resolved-by-a-proxyquire-instance) | ||
- [All together, now](#all-together-now) | ||
- [Examples](#examples) | ||
- [Backwards Compatibility for proxyquire v0.3.x](#backwards-compatibility-for-proxyquire-v03x) | ||
- [More Examples](#more-examples) | ||
# Usage | ||
@@ -71,8 +72,5 @@ | ||
## Resolve module to be tested and configure stubs | ||
***proxyquire({string} request, {Object} stubs)*** | ||
***proxyquire({string} mdl, {string} test__dirname, {Object} stubs)*** | ||
- **mdl**: path to the module to be tested e.g., `../lib/foo` | ||
- **test__dirname**: the `__dirname` of the module containing the tests | ||
- **request**: path to the module to be tested e.g., `../lib/foo` | ||
- **stubs**: key/value pairs of the form `{ modulePath: stub, ... }` | ||
@@ -83,10 +81,2 @@ - module paths are relative to the tested module **not** the test file | ||
### Alternative: | ||
***proxyquire.resolve({string} mdl, {string} test__dirname, {Object} stubs)*** | ||
You should only use ***resolve*** to enable a fluent api, like in the last example | ||
[below](#preventing-call-thru-to-original-dependency). | ||
## Preventing call thru to original dependency | ||
@@ -96,3 +86,3 @@ | ||
If you prefer a more strict behavior you can prevent *callThru* on a per module or global basis. | ||
If you prefer a more strict behavior you can prevent *callThru* on a per module or contextual basis. | ||
@@ -106,5 +96,5 @@ If *callThru* is disabled, you can stub out modules that don't even exist on the machine that your tests are running on. | ||
```javascript | ||
var foo = proxyquire('./foo', __dirname, { | ||
path: { | ||
extname: function (file) { ... } | ||
var foo = proxyquire('./foo', { | ||
path: { | ||
extname: function (file) { ... } | ||
, '@noCallThru': true | ||
@@ -115,12 +105,16 @@ } | ||
**Prevent call thru for all future stubs:** | ||
### Prevent call thru for all future stubs resolved by a proxyquire instance | ||
```javascript | ||
proxyquire.noCallThru(); | ||
// all stubs resolved by proxyquireStrict will not call through by default | ||
var proxyquireStrict = require('proxyquire').noCallThru(); | ||
// all stubs resolved by proxyquireNonStrict will call through by default | ||
var proxyquireNonStrict = require('proxyquire'); | ||
``` | ||
**Re-enable call thru for all future stubs:** | ||
### Re-enable call thru for all future stubs resolved by a proxyquire instance | ||
```javascript | ||
proxyquire.noCallThru(false); | ||
proxyquire.callThru(); | ||
``` | ||
@@ -133,3 +127,3 @@ | ||
.noCallThru() | ||
.resolve('./foo', __dirname, { | ||
.load('./foo', { | ||
@@ -147,10 +141,16 @@ // no calls to original './bar' methods will be made | ||
## Changing the TmpDir | ||
### All together, now | ||
***proxyquire.tmpDir({string} tmpdir)*** | ||
```javascript | ||
var proxyquire = require('proxyquire').noCallThru(); | ||
In order to hook into your code, proxyquire writes some intermediate files into the tmp directory. | ||
// all methods for foo's dependencies will have to be stubbed out since proxyquire will not call through | ||
var foo = proxyquire('./foo', stubs); | ||
By default it will use the *TMPDIR* of your environment, but this method allows you to override it. | ||
proxyquire.callThru(); | ||
// only some methods for foo's dependencies will have to be stubbed out here since proxyquire will now call through | ||
var foo2 = proxyquire('./foo', stubs); | ||
``` | ||
## Examples | ||
@@ -181,3 +181,3 @@ | ||
*/ | ||
var foo = proxyquire('../foo', __dirname, { | ||
var foo = proxyquire('../foo', { | ||
'./bar': { toAtm: function (val) { return 0; /* wonder what happens now */ } } | ||
@@ -193,3 +193,3 @@ }); | ||
var foo = proxyquire('../foo', __dirname, { './bar': barStub }); | ||
var foo = proxyquire('../foo', { './bar': barStub }); | ||
@@ -207,3 +207,3 @@ // Add override | ||
// Resolve foo and override multiple of its dependencies in one step - oh my! | ||
var foo = proxyquire('./foo', __dirname, { | ||
var foo = proxyquire('./foo', { | ||
'./bar' : { | ||
@@ -218,11 +218,22 @@ toAtm: function (val) { return 0; /* wonder what happens now */ } | ||
# Backwards Compatibility for proxyquire v0.3.x | ||
To upgrade your project from v0.3.x to v0.4.x, a nifty compat function has been included. | ||
Simply do a global find and replace for `require('proxyquire')` and change them to `require('proxyquire').compat()`. | ||
This returns an object that wraps the result of `proxyquire()` that provides exactly the same API as v0.3.x. | ||
If your test scripts relied on the fact that v0.3.x stored `noCallThru` in the module scope, you can use | ||
`require('proxyquire').compat(true)` to use a global compat object, instead. | ||
# More Examples | ||
For more examples look inside the [examples folder](./proxyquire/tree/master/examples/) or | ||
look through the [tests](./proxyquire/blob/master/test/proxyquire.js) | ||
For more examples look inside the [examples folder](https://github.com/thlorenz/proxyquire/tree/master/examples/) or | ||
look through the [tests](https://github.com/thlorenz/proxyquire/blob/master/test/proxyquire.js) | ||
**Specific Examples:** | ||
- test async APIs synchronously: [examples/async](./proxyquire/tree/master/examples/async). | ||
- using proxyquire with [Sinon.JS](http://sinonjs.org/): [examples/sinon](./proxyquire/tree/master/examples/sinon). | ||
- test async APIs synchronously: [examples/async](https://github.com/thlorenz/proxyquire/tree/master/examples/async). | ||
- using proxyquire with [Sinon.JS](http://sinonjs.org/): [examples/sinon](https://github.com/thlorenz/proxyquire/tree/master/examples/sinon). | ||
@@ -6,8 +6,36 @@ /*jshint asi:true*/ | ||
var assert = require('assert') | ||
, proxyquire = require('./../proxyquire'); | ||
, realFoo = require('./samples/foo'); | ||
var stubs = { | ||
path:{ | ||
extname:function () {}, | ||
basename:function () {} | ||
} | ||
}; | ||
describe('api', function () { | ||
it('proxyquire function is the same as proxyquire.resolve function', function () { | ||
assert.equal(proxyquire, proxyquire.resolve) | ||
}) | ||
}) | ||
describe('default export', function () { | ||
var proxyquire = require('..') | ||
it('proxyquire can load', function () { | ||
var proxiedFoo = proxyquire.load('./samples/foo', stubs); | ||
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); | ||
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); | ||
assert.equal(typeof proxiedFoo, 'object'); | ||
assert.notStrictEqual(realFoo, proxiedFoo); | ||
}); | ||
}); | ||
}); |
@@ -6,3 +6,3 @@ /*jshint asi:true*/ | ||
var assert = require('assert') | ||
, proxyquire = require('./../proxyquire') | ||
, proxyquire = require('..') | ||
; | ||
@@ -14,3 +14,3 @@ | ||
; | ||
function throws(action, regex) { | ||
@@ -22,48 +22,26 @@ assert.throws(action, function (err) { | ||
describe('when I pass no module', function () { | ||
describe('when I pass no request', function () { | ||
function act () { | ||
proxyquire(undefined, __dirname); | ||
proxyquire(null, {}); | ||
} | ||
it('throws an exception explaining that module needs to be passed', function () { | ||
throws(act, /missing argument: "mdl"/i); | ||
it('throws an exception explaining that a request path must be provided', function () { | ||
throws(act, /missing argument: "request"/i); | ||
}) | ||
}) | ||
describe('when I pass an object as module', function () { | ||
describe('when I pass an object as a request', function () { | ||
function act () { | ||
proxyquire({ }, __dirname, { './bar': bar }); | ||
proxyquire({ }, { './bar': bar }); | ||
} | ||
it('throws an exception explaining that module needs to be a string', function () { | ||
throws(act, /invalid argument: "mdl".+needs to be a string/i); | ||
}) | ||
}) | ||
describe('when I pass no test__dirname', function () { | ||
function act () { | ||
proxyquire('module'); | ||
} | ||
it('throws an exception explaining that resolve without stubs makes no sense', function () { | ||
throws(act, /missing argument: "__dirname" of test file/i); | ||
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); | ||
}) | ||
}) | ||
describe('when I pass an object as test__dirname', function () { | ||
function act () { | ||
proxyquire('./samples/foo', { }, { './bar': bar }); | ||
} | ||
it('throws an exception explaining that test__dirname needs to be a string', function () { | ||
throws(act, /invalid argument: "__dirname" of test file.+needs to be a string/i); | ||
}) | ||
}) | ||
describe('when I pass no stubs', function () { | ||
function act () { | ||
proxyquire('./samples/foo', __dirname); | ||
proxyquire('./samples/foo'); | ||
} | ||
@@ -74,3 +52,3 @@ | ||
}) | ||
}) | ||
@@ -80,3 +58,3 @@ | ||
function act () { | ||
proxyquire('./samples/foo', __dirname, 'stubs'); | ||
proxyquire('./samples/foo', 'stubs'); | ||
} | ||
@@ -83,0 +61,0 @@ |
@@ -6,3 +6,3 @@ /*jshint asi:true*/ | ||
var assert = require('assert') | ||
, proxyquire = require('./../proxyquire') | ||
, proxyquire = require('..') | ||
; | ||
@@ -19,4 +19,4 @@ | ||
before(function () { | ||
foo1 = proxyquire('./samples/foo', __dirname, { './bar': bar1 }); | ||
foo2 = proxyquire('./samples/foo', __dirname, { './bar': bar2 }); | ||
foo1 = proxyquire('./samples/foo', { './bar': bar1 }); | ||
foo2 = proxyquire('./samples/foo', { './bar': bar2 }); | ||
}) | ||
@@ -23,0 +23,0 @@ |
@@ -14,3 +14,3 @@ 'use strict'; | ||
assert.throws(function () { | ||
proxyquire(fooPath, __dirname, { | ||
proxyquire(fooPath, { | ||
'/not/existing/bar.json': { config: 'bar\'s config' } | ||
@@ -26,3 +26,3 @@ }) | ||
it('resolves foo with stubbed bar', function () { | ||
foo = proxyquire(fooPath, __dirname, { | ||
foo = proxyquire(fooPath, { | ||
'/not/existing/bar.json': { config: 'bar\'s config', '@noCallThru': true } | ||
@@ -29,0 +29,0 @@ }) |
@@ -6,4 +6,4 @@ /*jshint asi:true*/ | ||
var assert = require('assert') | ||
, proxyquire = require('./../proxyquire') | ||
, stats = require('./samples/stats') | ||
, proxyquire = require('..') | ||
, stats = require('./samples/stats') | ||
; | ||
@@ -15,10 +15,10 @@ | ||
, foober | ||
, barber = { bar: function () { return 'barber'; } } | ||
, 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', __dirname, { './bar': { /* no overrides */ } }); | ||
foober = proxyquire('./samples/foo', __dirname, { './bar': barber }); | ||
foo = proxyquire('./samples/foo', { './bar':{ /* no overrides */ } }); | ||
foober = proxyquire('./samples/foo', { './bar':barber }); | ||
}) | ||
@@ -28,3 +28,3 @@ | ||
assert.equal(stats.fooRequires(), 2); | ||
}) | ||
}) | ||
@@ -50,3 +50,3 @@ describe('foo\'s bar is unchanged', function () { | ||
}) | ||
}) | ||
@@ -63,7 +63,7 @@ | ||
assert.equal(foober.bigBar(), 'FRISEUR'); | ||
}) | ||
}) | ||
it('overrides behavior when module is required on top of file', function () { | ||
assert.equal(foober.bigRab(), 'RABARBER'); | ||
}) | ||
}) | ||
@@ -85,3 +85,3 @@ | ||
}) | ||
}) | ||
@@ -96,7 +96,7 @@ }) | ||
before(function () { | ||
foo = proxyquire('./samples/foo', __dirname, { | ||
path: { | ||
extname: function (file) { return 'override ' + file; } | ||
} | ||
}); | ||
foo = proxyquire('./samples/foo', { | ||
path:{ | ||
extname:function (file) { return 'override ' + file; } | ||
} | ||
}); | ||
}) | ||
@@ -115,8 +115,7 @@ | ||
before(function () { | ||
foo = proxyquire('./samples/foo', __dirname, { | ||
path: { | ||
extname: function (file) { return 'override ' + file; } | ||
, '@noCallThru': true | ||
} | ||
}); | ||
foo = proxyquire('./samples/foo', { | ||
path:{ | ||
extname:function (file) { return 'override ' + file; }, '@noCallThru':true | ||
} | ||
}); | ||
}) | ||
@@ -131,8 +130,9 @@ | ||
}) | ||
}) | ||
describe('and callThru was turned off globally', function () { | ||
var $proxyquire; | ||
before(function () { | ||
proxyquire.noCallThru(); | ||
$proxyquire = proxyquire.noCallThru(); | ||
}) | ||
@@ -142,7 +142,7 @@ | ||
before(function () { | ||
foo = proxyquire('./samples/foo', __dirname, { | ||
path: { | ||
extname: function (file) { return 'override ' + file; } | ||
} | ||
}); | ||
foo = $proxyquire('./samples/foo', { | ||
path:{ | ||
extname:function (file) { return 'override ' + file; } | ||
} | ||
}); | ||
}) | ||
@@ -161,8 +161,7 @@ | ||
before(function () { | ||
foo = proxyquire('./samples/foo', __dirname, { | ||
path: { | ||
extname: function (file) { return 'override ' + file; } | ||
, '@noCallThru': false | ||
} | ||
}); | ||
foo = $proxyquire('./samples/foo', { | ||
path:{ | ||
extname:function (file) { return 'override ' + file; }, '@noCallThru':false | ||
} | ||
}); | ||
}) | ||
@@ -181,8 +180,8 @@ | ||
before(function () { | ||
foo = proxyquire | ||
.noCallThru(false) | ||
.resolve('./samples/foo', __dirname, { | ||
path: { | ||
extname: function (file) { return 'override ' + file; } | ||
} | ||
foo = $proxyquire | ||
.callThru() | ||
.load('./samples/foo', { | ||
path:{ | ||
extname:function (file) { return 'override ' + file; } | ||
} | ||
}); | ||
@@ -189,0 +188,0 @@ }) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
52760
46
1252
227
7