npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

require-inject

v1.4.4

Published

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

Downloads

84,789

Readme

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 in your tests

  • 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 mymod = requireInject.withEmptyCache(module, mocks)

As with requireInject but your require cache will be cleared before requring the module to have mocks injected into it. This can be useful when your test shares dependencies with the module to be mocked and you need to mock a transitive dependency of one of those dependencies. That is:

Test → A → B

ModuleToTest → A → MockedB

If we we didn't clear the cache then ModuleToTest would get the already cached version of A and the MockedB would never be injected. By clearing the cache first it means that ModuleToTest will get it's own copy of A which will then pick up any mocks we defined.

Previously to achieve this you would need to have provided a mock for A, which, if that isn't what you were testing, could be frustrating busy work.

  • 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 (that is, do async loading).

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

As with requireInject.installGlobally but clear the cache first as with requireInject.withEmptyCache. Because this globally clears the cache it means that any requires after this point will get fresh copies of their required modules, even if you required them previously.