GithubHelp home page GithubHelp logo

proxy-loader's Introduction

Deprecation

This loader is no longer supported.

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)

proxy-loader's People

Contributors

c-dante avatar le0nik avatar

Stargazers

Jidé avatar Laszlo avatar Piotr Jankowski avatar Andrew McDowell avatar

Watchers

James Cloos avatar

Forkers

vrk-kpa

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.