thlorenz/proxyquire
thlorenz/proxyquire là dự án mã nguồn mở trên GitHub với 2.7k sao, viết chủ yếu bằng JavaScript. 🔮 Proxies nodejs require in order to allow overriding dependencies during testing.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Snapshot
Cộng tác viên hàng đầu
Xem cộng tác viên hàng đầu
proxyquire 
Proxies nodejs's require in order to make overriding dependencies during testing easy while staying totally unobtrusive.
If you want to stub dependencies for your client side modules, try proxyquireify, a proxyquire for browserify v2 or proxyquire-universal to test in both Node and the browser.
Features
- no changes to your code are necessary
- non overridden methods of a module behave like the original
- mocking framework agnostic, if it can stub a function then it works with proxyquire
- "use strict" compliant
Example
foo.js:
var path = require('path');
module.exports.extnameAllCaps = function (file) {
return path.extname(file).toUpperCase();
};
module.exports.basenameAllCaps = function (file) {
return path.basename(file).toUpperCase();
};
foo.test.js:
var proxyquire = require('proxyquire')
, assert = require('assert')
, pathStub = { };
// when no overrides are specified, path.extname behaves normally
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.strictEqual(foo.extnameAllCaps('file.txt'), 'EXTERMINATE, EXTERMINATE THE FILE.TXT');
// path.basename and all other path module methods still function as before
assert.strictEqual(foo.basenameAllCaps('/a/b/file.txt'), 'FILE.TXT');
You can also replace functions directly:
get.js:
var get = require('simple-get');
var assert = require('assert');
module.exports = function fetch (callback) {
get('https://api/users', callback);
};
get.test.js:
var proxyquire = require('proxyquire').noCallThru();
var assert = require('assert');
var fetch = proxyquire('./get', {
'simple-get': function (url, callback) {
process.nextTick(function () {
callback(null, { statusCode: 200 })
})
}
});
fetch(function (err, res) {
assert(res.statusCode, 200)
});
Table of Contents generated with DocToc
- Usage
- API
- Preventing call thru to original dependency
- 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
- All together, now
- Using proxyquire to simulate the absence of Modules
- Forcing proxyquire to reload modules
- Globally override require
- Caveat
- Globally override require during module initialization
- Why is proxyquire messing with my
requirecache? - Globally override require during module runtime
- Configuring proxyquire by setting stub properties
- Preventing call thru to original dependency
- Backwards Compatibility for proxyquire v0.3.x
- Examples
- More Examples
Usage
Two simple steps to override require in your tests:
- add
var proxyquire = require('proxyquire');to top level of your test file proxyquire(...)the module you want to test and pass along stubs for modules you want to override
API
proxyquire({string} request, {Object} stubs)
- request: path to the module to be tested e.g.,
../lib/foo - stubs: key/value pairs of the form
{ modulePath: stub, ... }- module paths are relative to the tested module not the test file
- therefore specify it exactly as in the require statement inside the tested file
- values themselves are key/value pairs of functions/properties and the appropriate override
Preventing call thru to original dependency
By default proxyquire calls the function defined on the original dependency whenever it is not found on the stub.
If you prefer a more strict behavior you can prevent callThru on a per module or contextual basis. If your stub is a class or class instance rather than a plain object, you should disable callThru to ensure that it is passed through with the correct prototype.
class MockClass {
static '@noCallThru' = true;
}
var foo = proxyquire('./foo', {
'./my-class': MockClass
});
class MockClass {
get '@noCallThru'() {
return true;
}
}
var foo = proxyquire('./foo', {
'./my-class-instance': new MockClass()
});
If callThru is disabled, you can stub out modules that don't even exist on the machine that your tests are running on. While I wouldn't recommend this in general, I have seen cases where it is legitimately useful (e.g., when requiring global environment configs in json format that may not be available on all machines).
Prevent call thru on path stub:
var foo = proxyquire('./foo', {
path: {
extname: function (file) { ... }
, '@noCallThru': true
}
});
Prevent call thru for all future stubs resolved by a proxyquire instance
// 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 resolved by a proxyquire instance
proxyquire.callThru();
Call thru configurations per module override callThru():
Passing @noCallThru: false when configuring modules will override noCallThru():
var foo = proxyquire
.noCallThru()
.load('./foo', {
// no calls to original './bar' methods will be made
'./bar' : { toAtm: function (val) { ... } }
// for 'path' module they will be made
, path: {
extname: function (file) { ... }
, '@noCallThru': false
}
});
All together, now
var proxyquire = require('proxyquire').noCallThru();
// all methods for foo's dependencies will have to be stubbed out since proxyquire will not call through
var foo = proxyquire('./foo', stubs);
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);
Using proxyquire to simulate the absence of Modules
Some libraries may behave differently in the presence or absence of a package, for example:
var cluster;
try {
cluster = require('cluster');
} catch(e) {
// cluster module is not present.
cluster = null
}
if (cluster) {
// Then provide some functionality for a cluster-aware version of Node.js
} else {
// and some alternative for a cluster-unaware version.
}
To exercise the second branch of the if statement, you can make proxyquire pretend the package isn't present by
setting the stub for it to null. This works even if a cluster module is actually present.
var foo = proxyquire('./foo', { cluster: null });
Forcing proxyquire to reload modules
In most situations it is fine to have proxyquire behave exactly like nodejs require, i.e. modules that are loaded once
get pulled from the cache the next time.
For some tests however you need to ensure that the module gets loaded fresh everytime, i.e. if that causes initializing some dependency or some module state.
For this purpose proxyquire exposes the noPreserveCache function.
// ensure we don't get any module from the cache, but to load it fresh every time
var proxyquire = require('proxyquire').noPreserveCache();
var foo1 = proxyquire('./foo', stubs);
var foo2 = proxyquire('./foo', stubs);
var foo3 = require('./foo');
// foo1, foo2 and foo3 are different instances of the same module
assert.notStrictEqual(foo1, foo2);
assert.notStrictEqual(foo1, foo3);
proxyquire.preserveCache allows you to restore the behavior to match nodejs's require again.
proxyquire.preserveCache();
var foo1 = proxyquire('./foo', stubs);
var foo2 = proxyquire('./foo', stubs);
var foo3 = require('./foo');
// foo1, foo2 and foo3 are the same instance
assert.strictEqual(foo1, foo2);
assert.strictEqual(foo1, foo3);
Globally override require
Use the @global property to override every require of a module, even transitively.
Caveat
You should think very hard about alternatives before using this feature. Why, because it's intrusive and as you'll see if you read on it changes the default behavior of module initialization which means that code runs differently during testing than it does normally.
Additionally it makes it harder to reason about how your tests work.
Yeah, we are mocking
fsthree levels down inbar, so that's why we have to set it up when testingfoo
WAAAT???
If you write proper unit tests you should never have a need for this. So here are some techniques to consider:
- test each module in isolation
- make sure your modules are small enough and do only one thing
- stub out dependencies directly instead of stubbing something inside your dependencies
- if you are testing
barandbarcallsfoo.readandfoo.readcallsfs.readFileproceed as follows- do not stub out
fs.readFileglobally - instead stub out
fooso you can control whatfoo.readreturns without ever even hittingfs
- do not stub out
OK, made it past the warnings and still feel like you need this? Read on then but you are on your own now, this is as far as I'll go ;)
Watch out for more warnings below.
Globally override require during module initialization
// foo.js
var bar = require('./bar');
module.exports = function() {
bar();
}
// bar.js
var baz = require('./baz');
module.exports = function() {
baz.method();
}
// baz.js
module.exports = {
method: function() {
console.info('hello');
}
}
// test.js
var bazStub = {
method: function() {
console.info('goodbye');
}
};
var stubs = {
'./baz': Object.assign(bazStub, {'@global': true})
};
var proxyquire = require('proxyquire');
var foo = proxyquire('./foo', stubs);
foo(); // 'goodbye' is printed to stdout
Be aware that when using global overrides any module initialization code will be re-executed for each require.
This is not normally the case since node.js caches the return value of require, however to make global overrides work ,
proxyquire bypasses the module cache. This may cause unexpected behaviour if a module's initialization causes side effects.
As an example consider this module which opens a file during its initialization:
var fs = require('fs')
, C = require('C');
// will get executed twice
var file = fs.openSync('/tmp/foo.txt', 'w');
module.exports = function() {
return new C(file);
};
The file at /tmp/foo.txt could be created and/or truncated more than once.
Why is proxyquire messing with my require cache?
Say you have a module, C, that you wish to stub. You require module A which contains require('B'). Module B in turn
contains require('C'). If module B has already been required elsewhere then when module A receives the cached version
of module B and proxyquire would have no opportunity to inject the stub for C.
Therefore when using the @global flag, proxyquire will bypass the require cache.
Globally override require during module runtime
Say you have a module that looks like this:
module.exports = function() {
var d = require('d');
d.method();
};
The invocation of require('d') will happen at runtime and not when the containing module is requested via require.
If you want to globally override d above, use the @runtimeGlobal property:
var stubs = {
'd': {
method: function(val) {
console.info('hello world');
},
'@runtimeGlobal': true
}
};
This will cause module setup code to be re-executed just like @global, but with the difference that it will happen
every time the module is requested via require at runtime as no module will ever be cached.
This can cause subtle bugs so if you can guarantee that your modules will not vary their require behaviour at runtime,
use @global instead.
Configuring proxyquire by setting stub properties
Even if you want to override a module that exports a function directly, you can still set special properties like @global. You can use a named function or assign your stub function to a variable to add properties:
function foo () {}
proxyquire('./bar', {
foo: Object.assign(foo, {'@global': true})
});
And if your stub is in a separate module where module.exports = foo:
var foostub = require('../stubs/foostub');
proxyquire('bar', {
foo: Object.assign(foostub, {'@global': true})
});
Backwards Compatibility for proxyquire v0.3.x
Compatibility mode with proxyquire v0.3.x has been removed.
You should update your code to use the newer API but if you can't, pin the version of proxyquire in your package.json file to ~0.6 in order to continue using the older style.
Examples
We are testing foo which depends on bar:
// bar.js module
module.exports = {
toAtm: function (val) { return 0.986923267 * val; }
};
// foo.js module
// requires bar which we will stub out in tests
var bar = require('./bar');
[ ... ]
Tests:
// foo-test.js module which is one folder below foo.js (e.g., in ./tests/)
/*
* Option a) Resolve and override in one step:
*/
var foo = proxyquire('../foo', {
'./bar': { toAtm: function (val) { return 0; /* wonder what happens now */ } }
});
// [ .. run some tests .. ]
/*
* Option b) Resolve with empty stub and add overrides later
*/
var barStub = { };
var foo = proxyquire('../foo', { './bar': barStub });
// Add override
barStub.toAtm = function (val) { return 0; /* wonder what happens now */ };
[ .. run some tests .. ]
// Change override
barStub.toAtm = function (val) { return -1 * val; /* or now */ };
[ .. run some tests .. ]
// Resolve foo and override multiple of its dependencies in one step - oh my!
var foo = proxyquire('./foo', {
'./bar' : {
toAtm: function (val) { return 0; /* wonder what happens now */ }
}
, path : {
extname: function (file) { return 'exterminate the name of ' + file; }
}
});
More Examples
For more examples look inside the examples folder or look through the tests
Specific Examples:
- test async APIs synchronously: examples/async.
- using proxyquire with Sinon.JS: examples/sinon.
Repo liên quan
Master programming by recreating your favorite technologies from scratch.
A curated meta-list of curated lists organized by technology domain. The repository acts as a directory pointing to hundreds of specialized awesome lists covering programming languages, platforms, frameworks, and tooling. All content is community-contributed under the CC0 public domain dedication.
Public APIs is a community-curated GitHub repository listing free, publicly accessible APIs across a wide range of categories, with auth type, HTTPS, and CORS noted for each entry. It's a browsable reference, not a library to install.
REA is an MCP server and CLI that gives AI coding agents the ability to reverse engineer binaries, JavaScript apps, .NET assemblies, and websites. It hooks into Hopper, Ghidra, or IDA Pro for native binary work, or runs static JavaScript analysis with just Node.js. Every investigation returns an Evidence bundle that includes what was found, what was not, and the reasoning path. Setup registers REA with supported agents and walks you through connecting an analysis engine. The CLI also works standalone for terminal-first workflows.
Trả lời nhanh
Đọc thêm về thlorenz/proxyquire ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/thlorenz/proxyquire là nguồn chính thức.
thlorenz/proxyquire có bao nhiêu sao?
thlorenz/proxyquire có 2.7k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/thlorenz/proxyquire. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
thlorenz/proxyquire có phải mã nguồn mở không?
Có — thlorenz/proxyquire phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/thlorenz/proxyquire.
thlorenz/proxyquire có tag gì không?
Bản đồng bộ chưa ghi nhận topic GitHub nào cho thlorenz/proxyquire. GitHub topics hiển thị ở thanh bên phải trang repo — đó là nơi đáng kiểm tra nhất.
thlorenz/proxyquire có website riêng không?
TopGit chưa ghi nhận URL trang chủ cho thlorenz/proxyquire. Phần README ở tab phía trên thường có link demo, hoặc xem mô tả GitHub của repo.
thlorenz/proxyquire còn đang phát triển không?
Commit gần nhất trên thlorenz/proxyquire là 1.3 năm trước (theo timestamp GitHub). Repo có 102 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
Đọc đầy đủ README ở tab phía trên.
proxyquire có đáng để bạn bỏ thời gian?
ChatGPT, Claude và Perplexity đều đọc được trang này. Hỏi thử xem họ nghĩ gì về proxyquire.