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.

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.
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({...}).
npm install inject-loader
yarn add inject-loader
pnpm add inject-loader

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