You're Invited: Meet the Socket team at BSidesSF and RSAC - April 27 - May 1.RSVP
Socket
Sign inDemoInstall
Socket

injectr

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

injectr

Finally, a solution to node.js dependency injection

0.5.1
latest
Source
npm
Version published
Weekly downloads
1K
42.31%
Maintainers
1
Weekly downloads
 
Created
Source

injectr

Finally, a solution to node.js dependency injection

Install it

npm install injectr. Boom.

Use it

var injectr = require('injectr');
var myScript = injectr('../lib/myScript.js', {
    fs : mockFs,
    crypto : mockCrypto
});

Now when you require('fs') or require('crypto') in myScript.js, what you get is mockFs or mockCrypto.

Treat injectr like require for your tests, with a second argument to pass in your mocks.

Paths are now relative to the current file, just like require. Please update your tests if you are upgrading from v0.4 or below.

Context

injectr gives you access to the context of the injectr'd file via an optional third argument. Provide an object, and injectr will modify it as necessary and use that as the context.

var myScript = injectr('../lib/myScript.js', {}, {
    Date : mockDate,
    setTimeout : mockSetTimeout
});

As of version 0.4, injectr doesn't create a full node.js context for you to use. Instead, it isolates your script in its own sandbox, allowing you to include mocks of only the bits that your script needs.

CoffeeScript

injectr compiles any *.coffee files for you, so you can test your CoffeeScript too. The default settings can be changed by overwriting the injectr.onload function. It takes the filename and file contents as arguments, and returns the compiled script.

Share it

injectr is under the MIT License. Fork it. Modify it. Pass it around.

Keywords

dependency injection

FAQs

Package last updated on 12 Apr 2014

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