Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

inject-loader

plasticine74.4kMIT4.0.1

A Webpack loader for injecting code into modules via their dependencies

webpack, testing, loader, webpack-loader, inject, mock, mocking

readme

💉📦
inject-loader

A Webpack loader for injecting code into modules via their dependencies

build status Gemnasium npm version npm downloads

Why

This is particularly useful for writing tests where mocking things inside your module-under-test is sometimes necessary before execution.

inject-loader was inspired by, and builds upon ideas introduced in jauco/webpack-injectable.

Usage

Documentation: Using loaders

Use the inject loader by adding the inject-loader! inline loader when you use require, this will return a function that can used in test code to modify the injected module.

By default all require statements in an injected module will be altered to be replaced with an injector, though if a replacement it not specified the default values will be used.

Examples

Given some code in a module like this:

// MyStore.js

var Dispatcher = require('lib/dispatcher');
var EventEmitter = require('events').EventEmitter;
var handleAction = require('lib/handle_action');

Dispatcher.register(handleAction, 'MyStore');

You can manipulate it’s dependencies when you come to write tests as follows:

// If no flags are provided when using the loader then
// all require statements will be wrapped in an injector
MyModuleInjector = require('inject-loader!MyStore')
MyModule = MyModuleInjector({
  'lib/dispatcher': DispatcherMock,
  'events': EventsMock,
  'lib/handle_action': HandleActionMock
})

There are a few examples of complete test setups for both Webpack 1, 2, 3 & 4 in the example folder.

License

MIT (http://www.opensource.org/licenses/mit-license.php)

changelog

Change log

All notable changes to this project will be documented in this file. This project adheres to Semantic Versioning.

4.0.0

  • Support Webpack 4.X

3.0.0

  • [feature] Huge refactor & cleanup converting underlying implementation to use Babel Core. #36
    • This should fix a huge number of really annoying edge-cases that were present in the previous implementation, e.g #32
    • Support Sourcemaps! #10
    • Massive thanks to @vladimir-tikhonov for his stellar effots! 🚀👏

2.0.1 — 2015-08-05

  • [fixed] — Injection of Babel generated module code. #11

2.0.0 — 2015-05-30

  • [added] — Support for falling back to base module definition if an alternate is not defined; made this this default behaviour. #7

1.0.0 — 2014-10-09

  • Initial release.