inject-loader4.0.1 • Public • Published
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.
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.
Given some code in a module like this:
// MyStore.jsvar Dispatcher = ;var EventEmitter = EventEmitter;var handleAction = ;Dispatcher;
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 injectorMyModuleInjector =MyModule =
There are a few examples of complete test setups for both Webpack 1, 2, 3 & 4 in the