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

proxy-loader

v0.1.0

Published

Injector wrapper for modules. Mock specific require dependencies with ease.

Downloads

446

Readme

Deprecation

This loader is no longer supported. This library's method DOES NOT WORK for ES6 imports!!

See c-dante/mockpack. That library works by taking advantage of Webpack's module loader at runtime to re-instantiate modules with runtime injected imports/requires. I also support ES6 with mockpack, and intend to support Webpack 2.0 if possible.

proxy-loader

I really didn't like existing dependency injecting loaders and mocking when using AMD/CommonJS in a webpack environment.

Using webpack as a bundler with resolve loaders/plugins is great. Unit testing in that environment is less-great. So I wanted to make it as transparent as possible.

Bundle your codebase with dependencies. Override modules to test as needed. Do this on the module level. The flatter the inheritance/dependency tree the better.

Use

Really targeted toward webpack. Have the proxy-loader module installed, set up moduleLoaders if needed.

// Get the factory-wrapped module
var MyModuleFactory = require('proxy!./my/module');

// Create instances of your module with overridden require statements
var myModuleMocked = MyModuleFactory({
  '../services/some/dep': mockDependency,
  '../services/some/other/dep': {
    otherDepFunction: function(){ return true; }
  }
});

// now myModuleMocked has overridden dependencies for '../services/some/dep' and '../services/some/other/dep'

See the example Run the example with:

$> npm install
$> gulp compile
$> node ./bin/main.bundle.js

You can see the webpack.config.js for how easy it is to set up this app.

And let's not forget, gulp for making this happen.

Existing proxy/mock/etc modules:

Isn't this exactly like inject-loader?

Yes. Except webpack makes a new dependency and module for each loader string. That means each of the following are completely new dependencies:

var mockA = require('inject!./myModule');
var mockB = require('inject?lib/something!./myModule');
var mockC = require('inject?-lib/something!./myModule');

I also did not like having to specify what I'll be mocking at require time. For unit tests, I prefer:

var ProxyFactory = require('proxy!./module/under/test');

it('Should test some things', function()
{
  var mock = ProxyFactory({
  // override only some things
  });
  
  // use mock
});

it('Should test some different things', function()
{
  var mock = ProxyFactory({
  // override different things
  });
  
  // use mock
});

Methodology

  1. Use webpack to compile the application together.
  2. For unit testing, wrap modules to mock in proxy-loader. This is similar to inject-loader without being as explicit.
  3. Use the returned proxy module factory to mock any dependencies and return the new module. The benefits of creating a new module for each set of dependency mocks is invaluable for unit testing. 4, Look back on your life an think about how things are better when you can unit test with chai, chai-as-promised, and of course mocha. (honroable mention: sinon.js)