Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
browsyquire
Advanced tools
Proxies browserify's require in order to allow overriding dependencies during testing.
IMPORTANT This is a fork of the awesome proxyquireify. All credit is for the original authors.
This fork adds the following features:
Wraps the "require magic" in a function to prevent it from being executed but still allow browserify to include the proxied module in the bundle.
Allows to mock dependencies that are outside of the main require flow (like inside a method that is executed after the mock was created).
Enable noCallThru globally.
It adds a noCallThru
function that can be called to indicate you want all your stubs to behave like if they have the property '@noCallThru': true
on them. You can still override this behavior on each stub if desired by adding the property @noCallThru
and set it to false.
injects a function called mockquire
to the modules that reference it.
This function is just an alias to browsyquire, but is convenient to use
because you don't need to pass the local require, that is done automatically.
So basically if you use mockquire('some-module')
then the following code will
prepend to your code.
var mockquire = require('browsyquire')(require); mockquire.reset(); mockquire.noCallThru();
This will inject the mockquire function and configure it to reset the cache, so calls from other modules don't interfere with the current one. And to not call the original methods of your stub. If you want to use mockquire and still want to call the original methods of your stubs you can do:
mockquire.callThru(); // add this at the top of your testing code
NOTE: It is advisable to always clear the cache with reset
after your tests to
prevent the normal require
calls be confused by the cache created by browsyquire
e.g:
// in mocha
afterEach(function () {
mockquire.reset();
});
npm i browsyquire
var fs = require('fs')
, proxyquire = require('browsyquire')
, browserify = require('browserify')
;
browserify({ debug: true })
.plugin(proxyquire.plugin) // !!do not forget to pass the plugin...
.require(require.resolve('./test'), { entry: true })
.bundle()
.pipe(fs.createWriteStream(__dirname + '/bundle.js'));
And inside your tests:
'use strict';
var stubs = {
'./bar': {
wunder: function () { return 'wirklich wunderbar'; }
, kinder: function () { return 'schokolade'; }
}
};
var foo = mockquire('./src/foo', stubs); // mockquire will be defined... I promise :)
console.log(foo());
// it is a drop in replacement for proxyquireify, so just do:
// It works exactly like the original but with the added behavior
// described above.
var proxyquire = require('browsyquire')(require);
mockquire
method//===> mouth.js
module.exports = {
saySomething: function () {
return 'blah';
}
};
//===> speaker.js
module.exports = {
speak: function () {
return require('./mouth').saySomething();
}
};
//===> some test
describe('a test', function () {
describe('it should not say blah, but foo', function () {
var speaker = mockquire('./speaker', {
'./mouth': {
saySomething: function () {
return 'foo'
}
});
var result = speaker.speak();
expect(result).to.equal('foo'); // not blah! cause overriden by stub
});
});
The mockquire
function has 2 methods
Clears the stub cache. This is done automatically after each mockquire call, but can also be called manually if for any reason there is a need to clear it.
// using mockquire
mockquire.reset(); // clear the stubs cache
// using the old api
var browsyquire = require('browsyquire')(require);
browsyquire.reset();
Configure browsyquire
to assume all stubs have the @noCallThru
property set to true.
// using mockquire
mockquire.noCallThru();
// using the old api
var browsyquire = require('browsyquire')(require);
browsyquire.noCallThru();
Original Readme below.
browserify >= v2
version of proxyquire.
Proxies browserify's require in order to make overriding dependencies during testing easy while staying totally unobstrusive. To run your tests in both Node and the browser, use proxyquire-universal.
Table of Contents generated with DocToc
require
calls to ensure the
module you are testing gets bundlednpm install proxyquireify
To use with browserify < 5.1
please npm install proxyquireify@0.5
instead. To run your tests in PhantomJS, you may need to use a shim.
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
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 for require
statements. If you require
anything that's not valid JavaScript that acorn 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:
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
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'));
../lib/foo
{ modulePath: stub, ... }
var proxyquire = require('proxyquireify')(require);
var barStub = { wunder: function () { 'really wonderful'; } };
var foo = proxyquire('./foo', { './bar': barStub })
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.
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
});
FAQs
Proxies browserify's require in order to allow overriding dependencies during testing.
We found that browsyquire demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.