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

clay-proxyquireify

v1.0.1

Published

Proxies browserify's require in order to allow overriding dependencies during testing.

Downloads

3

Readme

proxyquireify build status

browserify >= v2 version of proxyquire.

Proxies browserify's require in order to make overriding dependencies during testing easy while staying totally unobstrusive.

Table of Contents generated with DocToc

Features

  • no changes to your code are necessary
  • non overriden methods of a module behave like the original
  • mocking framework agnostic, if it can stub a function then it works with proxyquireify
  • "use strict" compliant
  • automatic injection of require calls to ensure the module you are testing gets bundled

Installation

npm install proxyquireify

To use with browserify < 5.1 please npm install [email protected] instead.

Example

foo.js:

var bar = require('./bar');

module.exports = function () {
  return bar.kinder() + ' ist ' + bar.wunder();
};

foo.test.js:

var proxyquire = require('proxyquireify')(require);

var stubs = { 
  './bar': { 
      wunder: function () { return 'wirklich wunderbar'; }
    , kinder: function () { return 'schokolade'; }
  }
};

var foo = proxyquire('./src/foo', stubs);

console.log(foo()); 

browserify.build.js:

var browserify = require('browserify');
var proxyquire = require('proxyquireify');

browserify()
  .plugin(proxyquire.plugin)
  .require(require.resolve('./foo.test'), { entry: true })
  .bundle()
  .pipe(fs.createWriteStream(__dirname + '/bundle.js'));

load it in the browser and see:

schokolade ist wirklich wunderbar

API

proxyquire.plugin()

proxyquireify functions as a browserify plugin and needs to be registered with browserify like so:

var browserify = require('browserify');
var proxyquire = require('proxyquireify');

browserify()
  .plugin(proxyquire.plugin)
  .require(require.resolve('./test'), { entry: true })
  .bundle()
  .pipe(fs.createWriteStream(__dirname + '/bundle.js'));

Alternatively you can register proxyquireify as a plugin from the command line like so:

browserify -p proxyquireify/plugin test.js > bundle.js

proxyquire.browserify()

Deprecation Warning

This API to setup proxyquireify was used prior to browserify plugin support.

It has not been removed yet to make upgrading proxyquireify easier for now, but it will be deprecated in future versions. Please consider using the plugin API (above) instead.


To be used in build script instead of browserify(), autmatically adapts browserify to work for tests and injects require overrides into all modules via a browserify transform.

proxyquire.browserify()
  .require(require.resolve('./test'), { entry: true })
  .bundle()
  .pipe(fs.createWriteStream(__dirname + '/bundle.js'));

proxyquire(request: String, stubs: Object)

  • request: path to the module to be tested e.g., ../lib/foo
  • stubs: key/value pairs of the form { modulePath: stub, ... }
    • module paths are relative to the tested module not the test file
    • therefore specify it exactly as in the require statement inside the tested file
    • values themselves are key/value pairs of functions/properties and the appropriate override
var proxyquire =  require('proxyquireify')(require);
var barStub    =  { wunder: function () { 'really wonderful'; } };

var foo = proxyquire('./foo', { './bar': barStub })

Important Magic

In order for browserify to include the module you are testing in the bundle, proxyquireify will inject a require() call for every module you are proxyquireing. So in the above example require('./foo') will be injected at the top of your test file.

noCallThru

By default proxyquireify calls the function defined on the original dependency whenever it is not found on the stub.

If you prefer a more strict behavior you can prevent callThru on a per module or per stub basis.

If callThru is disabled, you can stub out modules that weren't even included in the bundle. Note, that unlike in proxquire, there is no option to prevent call thru globally.

// Prevent callThru for path module only
var foo = proxyquire('./foo', {
    path: {
      extname: function (file) { ... }
    , '@noCallThru': true
    }
  , fs: { readdir: function (..) { .. } }
});

// Prevent call thru for all contained stubs (path and fs)
var foo = proxyquire('./foo', {
    path: {
      extname: function (file) { ... }
    }
  , fs: { readdir: function (..) { .. } }
  , '@noCallThru': true
});

// Prevent call thru for all stubs except path
var foo = proxyquire('./foo', {
    path: {
      extname: function (file) { ... }
    , '@noCallThru': false
    }
  , fs: { readdir: function (..) { .. } }
  , '@noCallThru': true
});

More Examples