Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@orrisroot/inject-loader

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@orrisroot/inject-loader

A Webpack loader for injecting code into modules via their dependencies

  • 5.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

💉📦 inject-loader

A Webpack loader for injecting code into modules via their dependencies

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.

This library has been forked from inject-loader and renamed to @orrisroot/inject-loader for continued maintenance.

Usage

Documentation: Using loaders

Use the inject loader by adding the @orrisroot/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('@orrisroot/inject-loader!MyStore')
MyModule = MyModuleInjector({
  'lib/dispatcher': DispatcherMock,
  'events': EventsMock,
  'lib/handle_action': HandleActionMock
})

There are a few examples of complete test setups for Webpack 5 in the example folder.

License

The source code is licensed MIT. see LICENSE.

Keywords

FAQs

Package last updated on 21 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc