inject-loader
raw JSON → 4.0.1 verified Sat Apr 25 auth: no javascript
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.
Common errors
error TypeError: inject_loader is not a function ↓
cause Using inject-loader as a regular module instead of an inline loader.
fix
Use require('inject-loader!./MyModule') instead of require('inject-loader')(...).
error Module not found: Error: Can't resolve 'inject-loader' ↓
cause inject-loader not installed or not in node_modules.
fix
Run 'npm install --save-dev inject-loader'.
error Critical dependency: require function is used in a way in which dependencies cannot be statically extracted ↓
cause 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/'.
error Inject loader only works with CommonJS modules (module.exports/require). ↓
cause 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.
Warnings
gotcha You 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)'.
deprecated Webpack 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.
gotcha The 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').
breaking In 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({...}).
Install
npm install inject-loader yarn add inject-loader pnpm add inject-loader Imports
- inject-loader wrong
const inject = require('inject-loader'); const MyModuleInjector = inject('./MyModule');correctimport MyModuleInjector from 'inject-loader!./MyModule'; - inject-loader (CommonJS) wrong
const MyModuleInjector = require('inject-loader')( './MyModule' );correctconst MyModuleInjector = require('inject-loader!./MyModule'); - injector function wrong
const MyModule = MyModuleInjector.inject({ 'lib/dispatcher': DispatcherMock });correctconst MyModule = MyModuleInjector({ 'lib/dispatcher': DispatcherMock, 'events': EventsMock, 'lib/handle_action': HandleActionMock });
Quickstart
// 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