Registry / testing / inject-loader

inject-loader

JSON →
library4.0.1jsnpmunverified

A Webpack loader for injecting mock dependencies into modules under test. Current stable version 4.0.1, maintained steadily. It wraps all require statements in a module so you can substitute them during testing. Unlike proxyquire or rewire, it works as a Webpack inline loader, making it seamless in Webpack-based projects. Supports Webpack 1 through 4. Particularly useful for unit testing where you need to mock internal dependencies of a module before it executes.

npm install inject-loader
INSTALL
IMPORT
SIG · INJECT-LOADER
I
inject-loader
testingjavascriptv4.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

inject-loader
import MyModuleInjector from 'inject-loader!./MyModule';
const inject = require('inject-loader'); const MyModuleInjector = inject('./MyModule');
inject-loader is used as an inline Webpack loader, not a regular module. The correct usage is to prefix the module path with 'inject-loader!' in the require/import statement.
inject-loader (CommonJS)
const MyModuleInjector = require('inject-loader!./MyModule');
const MyModuleInjector = require('inject-loader')( './MyModule' );
When using CommonJS, still use the inline loader syntax in the require path.
injector function
const MyModule = MyModuleInjector({ 'lib/dispatcher': DispatcherMock, 'events': EventsMock, 'lib/handle_action': HandleActionMock });
const MyModule = MyModuleInjector.inject({ 'lib/dispatcher': DispatcherMock });
The loader returns a function that accepts an object mapping dependency paths to mock modules.

Shows how to require a module via inject-loader inline and inject mock dependencies.

// Example test using inject-loader with Jest const MyModuleInjector = require('inject-loader!./MyModule'); const DispatcherMock = { register: jest.fn() }; const EventsMock = { EventEmitter: jest.fn() }; const HandleActionMock = jest.fn(); const MyModule = MyModuleInjector({ 'lib/dispatcher': DispatcherMock, 'events': EventsMock, 'lib/handle_action': HandleActionMock }); // Now MyModule uses the mocked dependencies DispatcherMock.register.mock.calls.length; // 1
Debug
Known issues
gotchaYou must use the inline loader syntax 'inject-loader!./module' in your require/import statements. Using inject-loader as a regular module and calling it as a function will not work.
fix
Use 'require("inject-loader!./module")' instead of 'require("inject-loader").(./module)'.
affects: >=4.0
deprecatedWebpack 1 and 2 support is deprecated. The loader still works but no longer actively tested.
fix
Upgrade to Webpack 3 or 4 for official support.
affects: >=4.0
gotchaThe loader wraps all require statements by default. If you want to exclude certain dependencies, you must provide flags (not documented in readme).
fix
Check loader options or use flags as described in the example folder: require('inject-loader?exclude=events!./module').
affects: >=4.0
breakingIn v4, the loader now returns a single injector function instead of an object with inject method.
fix
Call the result directly: const module = require('inject-loader!./module')({...}) instead of require('inject-loader!./module').inject({...}).
affects: >=4.0
Errors
Common errors & fixes
TypeError: inject_loader is not a function
Using inject-loader as a regular module instead of an inline loader.
fix
Use require('inject-loader!./MyModule') instead of require('inject-loader')(...).
Module not found: Error: Can't resolve 'inject-loader'
inject-loader not installed or not in node_modules.
fix
Run 'npm install --save-dev inject-loader'.
Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
Webpack 4 warns about dynamic requires; inject-loader uses dynamic require internally.
fix
This warning is expected and usually harmless. Suppress with webpack config stats: 'warningsFilter: /Critical dependency/'.
Inject loader only works with CommonJS modules (module.exports/require).
Using ES module syntax (import/export) in the injected module.
fix
Ensure the module under test uses CommonJS require/module.exports, or use a babel transform to convert.
Upgrade
Version history
4.0.1latest on npm
Audit
Dependencies
webpackrequiredpeer dependency required to function as a Webpack loader
Agent activity
11 hits · last 30 days
node
10
Resources
inject-loader — npm install inject-loader · libregistry