{"id":17454394,"url":"https://github.com/tomitrescak/proxyrequire","last_synced_at":"2025-09-13T23:32:37.036Z","repository":{"id":71787576,"uuid":"82884651","full_name":"tomitrescak/proxyrequire","owner":"tomitrescak","description":"The easiest way to stub/proxy module references across most bundlers","archived":false,"fork":false,"pushed_at":"2018-01-30T02:13:57.000Z","size":8,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-28T12:34:21.569Z","etag":null,"topics":["fusebox","nodejs","tdd"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tomitrescak.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-02-23T04:36:31.000Z","updated_at":"2020-12-21T01:45:34.000Z","dependencies_parsed_at":"2023-06-02T07:45:45.705Z","dependency_job_id":null,"html_url":"https://github.com/tomitrescak/proxyrequire","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomitrescak%2Fproxyrequire","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomitrescak%2Fproxyrequire/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomitrescak%2Fproxyrequire/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomitrescak%2Fproxyrequire/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tomitrescak","download_url":"https://codeload.github.com/tomitrescak/proxyrequire/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232931899,"owners_count":18598665,"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":["fusebox","nodejs","tdd"],"created_at":"2024-10-18T01:31:57.722Z","updated_at":"2025-01-07T20:11:20.433Z","avatar_url":"https://github.com/tomitrescak.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Introduction\n\nEasily stub module references in node.js, FuseBox or webpack\n\n## Stubbing References\n\nFitrst, you need to configure your environment to start stubbing (e.g. see FuseBox below).\nOnce the environment is configured, you can follow notation from well known *proxyquire* package.\n\n```javascript\nimport { proxy } from 'proxyrequire';\n\nconst Stub = proxy(() =\u003e require('$module_path'), {\n  '$stubbed_reference': your_stub\n});\n```\n\nFor example, if I wish to stub the `../container` and '../other' reference in file 'myFile.ts':\n\n```javascript\n// myFile.ts\nimport { Container } from '../container';\nimport Other from '../../other';\n```\n\nIn my test file (sitting in subdirectory `tests`)\n\n```javascript\nimport { proxy } from 'proxyrequire';\nconst Stub = proxy(() =\u003e require('../myFile').Container, {\n  '../container': { Container: () =\u003e \u003cdiv\u003eStubbed Container\u003c/div\u003e },\n  '../../other': { default: { stubbedObject } } // please note the object contruction with 'default'\n});\n```\n\n### FuseBox\n\nUse the package plugin to rewrite all your require references to allow stubbing.\nIn your fusebox config add plugin. Plugin parameter specifies the regular expression\nas filter on files which you want to stub:\n\n```javascript\nconst StubPlugin = require('proxyrequire').FuseBoxStubPlugin(/\\.tsx?/);\n\nfsbx.FuseBox.init({\n  homeDir: \"src\",\n  ...\n  plugins: [\n    StubPlugin, // add this plugin\n    ...\n  ]\n}).devServer(\"...\", {\n  ...\n});\n```\n\nThen, in your entry file, make sure you register global helpers.\nPlease, make sure that the entry file is not processed by the plugin.\n\n```javascript\n// entry file\nrequire('proxyrequire').registerGlobals()\n```\n\nIf you cannot isolate this file, please use the following boilerplate:\n\n```javascript\nglobal.$_stubs_$ = {};\nfunction proxyRequire (require, path) {\n   return global.$_stubs_$[path] || require(path);\n};\nglobal.proxyRequire = proxyRequire;\n``` \n\n## Webpack\n\nImport the webpack loader and chain it after the typescript compilation. \n\n```javascript\nconst StubLoader = require('proxyrequire').WebpackLoader;\n```\n\nMake sure you register global variables in your entry file, same as in FuseBox.\n\n** WARNING** This functionality has not yet been tested.\n\n## Node.JS - Mocha\n\nYou need to register the stubbing environment in your entry file (e.g. in the\nsetup function of `wallaby.js`). For Jest, see below.\n\n```javascript\nrequire('proxyrequire').registerNode();\n```\n\nJust start using `proxy` function straight away.\n\n## Node.JS - Jest\n\nJest uses its own implementation of require, therefore we need to rewrite our sources.\nLuckily, jest gives us tools to do that easily. Following is a configuration for Typescript,\nbut you can easily devise configuration for Javascript.\n\nFirst, create a preprocessor and place it in the root directory:\n\n```javascript\n// preprocessor.js\nconst tsc = require('typescript');\nconst tsConfig = require('./tsconfig.fuse.json');\nconst transform = require('jsx-controls-loader').loader;\nconst stubLoader = require('proxyrequire').webpackStubLoader;\n\n\nmodule.exports = {\n  process(src, path) {\n    if (path.endsWith('.ts') || path.endsWith('.tsx')) {\n      let res = tsc.transpile(\n        path.endsWith('.tsx') ? transform(src): src,\n        tsConfig.compilerOptions,\n        path,\n        []\n      );\n      res = stubLoader(res);\n      res = 'const proxyRequire = require(\"proxyrequire\").proxyRequire\\n' + res;\n      return res;\n    }\n    return src;\n  },\n};\n```\n\nAnd in your Jest config in package.json specify:\n\n```javascript\n// package.json\n\"jest\": {\n    \"transform\": {\n      \".(ts|tsx)\": \"\u003crootDir\u003e/preprocessor.js\"\n    }\n}\n```\n\n\n### Note\n\nNode.js implementation is optimised for Wallaby.js and you can see the source code below.\nPlease PR if you need to support more environments:\n\n```js\nexport function registerNode() {\n  global.$_stubs_$ = {};\n\n  var Module = require('module');\n  var originalRequire = Module.prototype.require;\n\n  Module.prototype.require = function (this: any, path: string) {\n    //do your thing here\n    return global.$_stubs_$[path] || originalRequire.apply(this, arguments);\n  };\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomitrescak%2Fproxyrequire","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomitrescak%2Fproxyrequire","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomitrescak%2Fproxyrequire/lists"}