Proxies Node.js require to allow easy overriding of dependencies during testing without modifying source code.
Proxyquire is a Node.js library that proxies the `require` function to allow easy stubbing and mocking of dependencies during unit testing. It solves the problem of isolating modules by letting developers override specific dependencies without altering the original source code, ensuring tests are focused and reliable.
Node.js developers and testers who need to write isolated unit tests for modules with external dependencies, particularly those working in test-driven development (TDD) or behavior-driven development (BDD) environments.
Developers choose Proxyquire because it requires no changes to production code, works with any mocking framework, and provides fine-grained control over dependency behavior while keeping non-overridden methods intact, making it a flexible and unobtrusive testing tool.
🔮 Proxies nodejs require in order to allow overriding dependencies during testing.
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Proxyquire works without modifying the original module, as emphasized in the README's 'no changes to your code' feature, making it non-invasive.
It is agnostic to mocking libraries, compatible with any tool that can stub functions, allowing integration with Sinon.JS or others.
Features like @noCallThru and cache management (noPreserveCache) provide precise control over dependency behavior and module state in tests.
Allows overriding dependencies across the module graph with @global, useful for complex stubbing scenarios, though with caveats.
The README warns that using @global can cause module initialization code to run multiple times, leading to side effects and harder-to-debug tests.
Designed for CommonJS require, it does not natively handle ES modules, limiting its use in modern Node.js projects adopting import/export syntax.
Advanced features like @runtimeGlobal and cache configuration require careful setup, adding verbosity and potential for errors in test code.