Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

browserify >= v2 version of proxyquire. Mocks out browserify's require to allow stubbing out dependencies while testing.

License

NotificationsYou must be signed in to change notification settings

thlorenz/proxyquireify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

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 withproxyquireify
  • "use strict" compliant
  • automatic injection ofrequire calls to ensure themodule you are testing gets bundled

Installation

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.

Example

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

With Other Transforms

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.

API

proxyquire.plugin()

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

proxyquire.browserify()

Deprecation Warning

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'));

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 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})

Important Magic

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.

noCallThru

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});

More Examples

About

browserify >= v2 version of proxyquire. Mocks out browserify's require to allow stubbing out dependencies while testing.

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  

Packages

No packages published

Contributors8


[8]ページ先頭

©2009-2025 Movatter.jp