Skip to content

kkroboth/inject-loader

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

89 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

inject-loader

build status Gemnasium npm version npm downloads

๐Ÿ’‰๐Ÿ‘พ A Webpack loader for injecting code into modules via their dependencies

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 inject-loader! when you use require, this will return a function that can be passed things to inject.

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 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 in the example folder.

License

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

About

๐Ÿ’‰๐Ÿ‘พ A Webpack loader for injecting code into modules via their dependencies.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 91.6%
  • Shell 6.8%
  • Makefile 1.6%