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

require-inject

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

require-inject

A simple mock injector compatible needing no instrumentation in the libraries being tested

  • 1.3.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
21K
increased by10.92%
Maintainers
1
Weekly downloads
 
Created
Source

require-inject

A simple mock injector compatible needing no instrumentation in the libraries being tested

Example

var requireInject = require('require-inject');

var mymod = requireInject('mymod', {
    'fs' => {
        stat: function (file,cb) {
            switch (file) {
            case 'testfile1': return cb(null,{})
            case 'testfile2': return cb(new Error('ENOENT'))
            }
        }
    }
})

var myglobal = requireInject.installGlobally('myglobal', { … })

Usage

  • var mymod = requireInject( module, mocks )

module is the name of the module you want to require. This is what you'd pass to require to load the module from your script. This means that for relative paths, the path should be relative to your test script, not to the thing you're injecting dependencies into.

mocks is an object with keys that are the names of the modules you want *to mock and values of the mock version of the objects.

requireInject makes it so that when module is required, any of its calls to require for modules inclued in mocks will return the mocked version. It takes care to not impact any other uses of module, any calls to require for it will get a version without mocks.

  • var myglobal = requireInject.installGlobally( module, mocks)

As with requireInject, except that the module and its mocks are left in the require cache and any future requires will end up using them too. This is helpful particularly in the case of things that defer loading.

Keywords

FAQs

Package last updated on 04 Mar 2016

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