injectify-loader

0.1.1 • Public • Published

injectify-loader

Build Status npm version npm downloads

A rewrite of inject-loader which is using babel parser under the hood. It has the following advantages:

API and behaviour are fully compatible with original inject-loader. Here is the copy of it's readme, all thanks go to @plasticine.

💉👾 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.

Installation

npm install injectify-loader --save-dev

or

yarn add injectify-loader --dev

Usage

Documentation: loaders

Use the inject loader by adding injectify-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:

// All require statements are wrapped in an injector
var MyModuleInjector = require('injectify-loader!MyStore')
var MyModule = MyModuleInjector({
  'lib/dispatcher': DispatcherMock,
  'events': EventsMock,
  'lib/handle_action': HandleActionMock
})

Package Sidebar

Install

npm i injectify-loader

Weekly Downloads

1

Version

0.1.1

License

MIT

Last publish

Collaborators

  • vladimir-tikhonov