{"id":13474156,"url":"https://github.com/thlorenz/proxyquireify","last_synced_at":"2025-04-06T09:09:07.195Z","repository":{"id":7668048,"uuid":"9029977","full_name":"thlorenz/proxyquireify","owner":"thlorenz","description":"browserify \u003e= v2 version of proxyquire. Mocks out browserify's require to allow stubbing out dependencies while testing.","archived":false,"fork":false,"pushed_at":"2016-11-29T08:05:56.000Z","size":112,"stargazers_count":150,"open_issues_count":3,"forks_count":24,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-01T19:05:22.912Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thlorenz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-03-26T12:43:18.000Z","updated_at":"2025-01-03T21:50:34.000Z","dependencies_parsed_at":"2022-09-16T15:01:04.798Z","dependency_job_id":null,"html_url":"https://github.com/thlorenz/proxyquireify","commit_stats":null,"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thlorenz%2Fproxyquireify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thlorenz%2Fproxyquireify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thlorenz%2Fproxyquireify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thlorenz%2Fproxyquireify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thlorenz","download_url":"https://codeload.github.com/thlorenz/proxyquireify/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247457802,"owners_count":20941906,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T16:01:09.930Z","updated_at":"2025-04-06T09:09:07.170Z","avatar_url":"https://github.com/thlorenz.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# proxyquireify [![build status](https://secure.travis-ci.org/thlorenz/proxyquireify.svg?branch=master)](http://travis-ci.org/thlorenz/proxyquireify)\n\nbrowserify `\u003e= v2` version of [proxyquire](https://github.com/thlorenz/proxyquire). \n\nProxies 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](https://github.com/bendrucker/proxyquire-universal).\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n**Table of Contents**  *generated with [DocToc](https://github.com/thlorenz/doctoc)*\n\n- [Features](#features)\n- [Installation](#installation)\n- [Example](#example)\n- [With Other Transforms](#with-other-transforms)\n- [API](#api)\n  - [proxyquire.plugin()](#proxyquireplugin)\n  - [proxyquire.browserify()](#proxyquirebrowserify)\n    - [Deprecation Warning](#deprecation-warning)\n  - [proxyquire(request: String, stubs: Object)](#proxyquirerequest-string-stubs-object)\n    - [Important Magic](#important-magic)\n  - [noCallThru](#nocallthru)\n- [More Examples](#more-examples)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n\n## Features\n\n- **no changes to your code** are necessary\n- non overriden methods of a module behave like the original\n- mocking framework agnostic, if it can stub a function then it works with **proxyquireify**\n- \"use strict\" compliant\n- [automatic injection](https://github.com/thlorenz/proxyquireify#important-magic) of `require` calls to ensure the\n  module you are testing gets bundled \n\n## Installation\n\n    npm install proxyquireify\n\nTo use with browserify `\u003c 5.1` please `npm install proxyquireify@0.5` instead. To run your tests in PhantomJS, you may need to [use a shim](https://github.com/bendrucker/phantom-ownpropertynames).\n\n## Example \n\n**foo.js**:\n\n```js\nvar bar = require('./bar');\n\nmodule.exports = function () {\n  return bar.kinder() + ' ist ' + bar.wunder();\n};\n```\n\n**foo.test.js**:\n\n```js\nvar proxyquire = require('proxyquireify')(require);\n\nvar stubs = { \n  './bar': { \n      wunder: function () { return 'wirklich wunderbar'; }\n    , kinder: function () { return 'schokolade'; }\n  }\n};\n\nvar foo = proxyquire('./src/foo', stubs);\n\nconsole.log(foo()); \n```\n\n**browserify.build.js**:\n\n```js\nvar browserify = require('browserify');\nvar proxyquire = require('proxyquireify');\n\nbrowserify()\n  .plugin(proxyquire.plugin)\n  .require(require.resolve('./foo.test'), { entry: true })\n  .bundle()\n  .pipe(fs.createWriteStream(__dirname + '/bundle.js'));\n```\n\nload it in the browser and see:\n\n    schokolade ist wirklich wunderbar\n\n## With Other Transforms\n\nIf you're transforming your source code to JavaScript, you must apply those transforms before applying the proxyquireify plugin:\n\n```js\nbrowserify()\n  .transform('coffeeify')\n  .plugin(proxyquire.plugin)\n  .require(require.resolve('./test.coffee'), { entry: true })\n  .bundle()\n  .pipe(fs.createWriteStream(__dirname + '/bundle.js'));\n```\n\nproxyquireify needs to parse your code looking for `require` statements. If you `require` anything that's not valid JavaScript that [acorn](https://github.com/marijnh/acorn) can parse (e.g. CoffeeScript, TypeScript), you need to make sure the relevant transform runs before proxyquireify.\n\n## API\n\n### proxyquire.plugin()\n\n**proxyquireify** functions as a browserify plugin and needs to be registered with browserify like so:\n\n```js\nvar browserify = require('browserify');\nvar proxyquire = require('proxyquireify');\n\nbrowserify()\n  .plugin(proxyquire.plugin)\n  .require(require.resolve('./test'), { entry: true })\n  .bundle()\n  .pipe(fs.createWriteStream(__dirname + '/bundle.js'));\n```\n\nAlternatively you can register **proxyquireify** as a plugin from the command line like so:\n\n```sh\nbrowserify -p proxyquireify/plugin test.js \u003e bundle.js\n```\n\n### proxyquire.browserify()\n\n#### Deprecation Warning\n\nThis API to setup **proxyquireify** was used prior to [browserify plugin](https://github.com/substack/node-browserify#bpluginplugin-opts) support.\n\nIt has not been removed yet to make upgrading **proxyquireify** easier for now, but it **will be deprecated in future\nversions**. Please consider using the plugin API (above) instead.\n\n****\n\nTo be used in build script instead of `browserify()`, autmatically adapts browserify to work for tests and injects\nrequire overrides into all modules via a browserify transform.\n\n```js\nproxyquire.browserify()\n  .require(require.resolve('./test'), { entry: true })\n  .bundle()\n  .pipe(fs.createWriteStream(__dirname + '/bundle.js'));\n```\n\n****\n\n### proxyquire(request: String, stubs: Object)\n\n- **request**: path to the module to be tested e.g., `../lib/foo`\n- **stubs**: key/value pairs of the form `{ modulePath: stub, ... }`\n  - module paths are relative to the tested module **not** the test file \n  - therefore specify it exactly as in the require statement inside the tested file\n  - values themselves are key/value pairs of functions/properties and the appropriate override\n\n```js\nvar proxyquire =  require('proxyquireify')(require);\nvar barStub    =  { wunder: function () { 'really wonderful'; } };\n\nvar foo = proxyquire('./foo', { './bar': barStub })\n```\n\n#### Important Magic \n\nIn order for browserify to include the module you are testing in the bundle, **proxyquireify** will inject a\n`require()` call for every module you are proxyquireing. So in the above example `require('./foo')` will be injected at\nthe top of your test file.\n\n### noCallThru\n\nBy default **proxyquireify** calls the function defined on the *original* dependency whenever it is not found on the stub.\n\nIf you prefer a more strict behavior you can prevent *callThru* on a per module or per stub basis.\n\nIf *callThru* is disabled, you can stub out modules that weren't even included in the bundle. **Note**, that unlike in\nproxquire, there is no option to prevent call thru globally.\n\n```js\n// Prevent callThru for path module only\nvar foo = proxyquire('./foo', {\n    path: {\n      extname: function (file) { ... }\n    , '@noCallThru': true\n    }\n  , fs: { readdir: function (..) { .. } }\n});\n\n// Prevent call thru for all contained stubs (path and fs)\nvar foo = proxyquire('./foo', {\n    path: {\n      extname: function (file) { ... }\n    }\n  , fs: { readdir: function (..) { .. } }\n  , '@noCallThru': true\n});\n\n// Prevent call thru for all stubs except path\nvar foo = proxyquire('./foo', {\n    path: {\n      extname: function (file) { ... }\n    , '@noCallThru': false\n    }\n  , fs: { readdir: function (..) { .. } }\n  , '@noCallThru': true\n});\n```\n\n## More Examples\n\n- [foobar](https://github.com/thlorenz/proxyquireify/tree/master/examples/foobar)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthlorenz%2Fproxyquireify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthlorenz%2Fproxyquireify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthlorenz%2Fproxyquireify/lists"}