proxyquire
Advanced tools
Comparing version 1.7.4 to 1.7.5
@@ -6,2 +6,4 @@ 'use strict'; | ||
, Module = require('module') | ||
, resolve = require('resolve') | ||
, dirname = require('path').dirname | ||
, ProxyquireError = require('./proxyquire-error') | ||
@@ -184,3 +186,13 @@ , is = require('./is') | ||
} else { | ||
delete require.cache[Module._resolveFilename(path, module)]; | ||
var id = Module._resolveFilename(path, module); | ||
var stubIds = Object.keys(stubs).map(function (stubPath) { | ||
return resolve.sync(stubPath, { | ||
basedir: dirname(id) | ||
}) | ||
}); | ||
var ids = [id].concat(stubIds); | ||
ids.forEach(function (id) { | ||
delete require.cache[id]; | ||
}); | ||
} | ||
@@ -187,0 +199,0 @@ |
{ | ||
"name": "proxyquire", | ||
"version": "1.7.4", | ||
"version": "1.7.5", | ||
"description": "Proxies nodejs require in order to allow overriding dependencies during testing.", | ||
@@ -33,4 +33,5 @@ "main": "index.js", | ||
"fill-keys": "^1.0.2", | ||
"module-not-found-error": "^1.0.0" | ||
"module-not-found-error": "^1.0.0", | ||
"resolve": "~1.1.7" | ||
} | ||
} |
@@ -56,2 +56,29 @@ # proxyquire [![Build Status](https://secure.travis-ci.org/thlorenz/proxyquire.svg)](http://travis-ci.org/thlorenz/proxyquire) | ||
You can also replace functions directly: | ||
```js | ||
var get = require('simple-get'); | ||
var assert = require('assert'); | ||
module.exports = function fetch (callback) { | ||
get('https://api/users', callback); | ||
}; | ||
``` | ||
```js | ||
var proxyquire = require('proxyquire'); | ||
var fetch = proxyquire('./get', { | ||
'simple-get': function (url, callback) { | ||
process.nextTick(function () { | ||
callback(null, fakeResponse) | ||
}) | ||
} | ||
}); | ||
fetch(function (err, res) { | ||
assert(res.statusCode, 200) | ||
}); | ||
``` | ||
<!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
@@ -58,0 +85,0 @@ <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> |
@@ -84,3 +84,22 @@ /*jshint asi:true*/ | ||
}); | ||
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' }; | ||
assert.equal(foo.bar.f.g(), 'a') | ||
assert.equal(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.equal(undefined, require.cache[require.resolve('./samples/cache/foo')]); | ||
assert.equal(undefined, require.cache[require.resolve('./samples/cache/bar')]); | ||
}); | ||
}); | ||
}); |
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
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
60532
53
1273
486
3
+ Addedresolve@~1.1.7
+ Addedresolve@1.1.7(transitive)