Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork24
browserify >= v2 version of proxyquire. Mocks out browserify's require to allow stubbing out dependencies while testing.
License
thlorenz/proxyquireify
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
browserify>= v2
version ofproxyquire.
Proxies browserify's require in order to make overriding dependencies during testing easy while stayingtotally unobstrusive. To run your tests in both Node and the browser, useproxyquire-universal.
Table of Contentsgenerated withDocToc
- 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 withproxyquireify
- "use strict" compliant
- automatic injection of
require
calls to ensure themodule you are testing gets bundled
npm install proxyquireify
To use with browserify< 5.1
pleasenpm install proxyquireify@0.5
instead. To run your tests in PhantomJS, you may need touse a shim.
foo.js:
varbar=require('./bar');module.exports=function(){returnbar.kinder()+' ist '+bar.wunder();};
foo.test.js:
varproxyquire=require('proxyquireify')(require);varstubs={'./bar':{wunder:function(){return'wirklich wunderbar';},kinder:function(){return'schokolade';}}};varfoo=proxyquire('./src/foo',stubs);console.log(foo());
browserify.build.js:
varbrowserify=require('browserify');varproxyquire=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
If you're transforming your source code to JavaScript, you must apply those transforms before applying the proxyquireify plugin:
browserify().transform('coffeeify').plugin(proxyquire.plugin).require(require.resolve('./test.coffee'),{entry:true}).bundle().pipe(fs.createWriteStream(__dirname+'/bundle.js'));
proxyquireify needs to parse your code looking forrequire
statements. If yourequire
anything that's not valid JavaScript thatacorn can parse (e.g. CoffeeScript, TypeScript), you need to make sure the relevant transform runs before proxyquireify.
proxyquireify functions as a browserify plugin and needs to be registered with browserify like so:
varbrowserify=require('browserify');varproxyquire=require('proxyquireify');browserify().plugin(proxyquire.plugin).require(require.resolve('./test'),{entry:true}).bundle().pipe(fs.createWriteStream(__dirname+'/bundle.js'));
Alternatively you can registerproxyquireify as a plugin from the command line like so:
browserify -p proxyquireify/plugin test.js> bundle.js
This API to setupproxyquireify was used prior tobrowserify plugin support.
It has not been removed yet to make upgradingproxyquireify easier for now, but itwill be deprecated in futureversions. Please consider using the plugin API (above) instead.
To be used in build script instead ofbrowserify()
, autmatically adapts browserify to work for tests and injectsrequire overrides into all modules via a browserify transform.
proxyquire.browserify().require(require.resolve('./test'),{entry:true}).bundle().pipe(fs.createWriteStream(__dirname+'/bundle.js'));
- 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 modulenot 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
varproxyquire=require('proxyquireify')(require);varbarStub={wunder:function(){'really wonderful';}};varfoo=proxyquire('./foo',{'./bar':barStub})
In order for browserify to include the module you are testing in the bundle,proxyquireify will inject arequire()
call for every module you are proxyquireing. So in the above examplerequire('./foo')
will be injected atthe top of your test file.
By defaultproxyquireify calls the function defined on theoriginal dependency whenever it is not found on the stub.
If you prefer a more strict behavior you can preventcallThru on a per module or per stub basis.
IfcallThru is disabled, you can stub out modules that weren't even included in the bundle.Note, that unlike inproxquire, there is no option to prevent call thru globally.
// Prevent callThru for path module onlyvarfoo=proxyquire('./foo',{path:{extname:function(file){ ...},'@noCallThru':true},fs:{readdir:function(..){..}}});// Prevent call thru for all contained stubs (path and fs)varfoo=proxyquire('./foo',{path:{extname:function(file){ ...}},fs:{readdir:function(..){..}},'@noCallThru':true});// Prevent call thru for all stubs except pathvarfoo=proxyquire('./foo',{path:{extname:function(file){ ...},'@noCallThru':false},fs:{readdir:function(..){..}},'@noCallThru':true});
About
browserify >= v2 version of proxyquire. Mocks out browserify's require to allow stubbing out dependencies while testing.
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors8
Uh oh!
There was an error while loading.Please reload this page.