{"id":23127606,"url":"https://github.com/hlfshell/needle-swap","last_synced_at":"2025-04-04T05:42:15.081Z","repository":{"id":57309741,"uuid":"78658580","full_name":"hlfshell/needle-swap","owner":"hlfshell","description":"Dependency injection for testing in node handled by overloading the require function","archived":false,"fork":false,"pushed_at":"2017-10-05T15:35:27.000Z","size":6,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-11T01:52:12.768Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/hlfshell.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}},"created_at":"2017-01-11T16:42:37.000Z","updated_at":"2025-01-01T20:32:35.000Z","dependencies_parsed_at":"2022-09-08T05:21:32.038Z","dependency_job_id":null,"html_url":"https://github.com/hlfshell/needle-swap","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/hlfshell%2Fneedle-swap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hlfshell%2Fneedle-swap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hlfshell%2Fneedle-swap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hlfshell%2Fneedle-swap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hlfshell","download_url":"https://codeload.github.com/hlfshell/needle-swap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247128739,"owners_count":20888234,"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-12-17T09:14:10.029Z","updated_at":"2025-04-04T05:42:15.066Z","avatar_url":"https://github.com/hlfshell.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# needle-swap\n\nneedle-swap is a dead simple unopinionated method to perform dependency injection into loaded files for testing.\n\n## Install\n```\nnpm install --save-dev needle-swap\n```\n\n## Example usage\nLet's say that I need to replace the *fs* module in a test I am about to run in mocha (this is testing framework agnostic, btw).\n\nIn your target test file, you use fs as such:\n\n*superImportant.js:*\n```\nconst fs = require('fs');\n\nvar mySuperImportantFunction = (chosenFile)=\u003e{\n\treturn fs.readFileSync(chosenFile);\n} \n\nmodule.exports = mySuperImportantFunction\n```\n\nthen, in your test file, you can make use needle-swap to override *fs* so it returns your test data, and not access a remote file.\n\n```\nconst needleswap = require('needle-swap');\nneedleswap({\n\t\"fs\": {\n\t\treadFileSync: (filename)=\u003e{\n\t\t\tif(filename == \"test\") return \"Test data\";\n\t\t\telse return \"No good!\"\n\t\t}\n\t}\n});\n\nit(\"should read the file by the path sent\", ()=\u003e{\n\tmySuperImportantFunction = require('./superImportant.js');\n\n\texpect(mySuperImportantFunction(\"test\")).to.be.equal(\"Test data\");\n});\n```\n\n_Needleswap will return itself so you can chain the cache clearing command if need be._\n\n## Clearing the require cache\n\nIf you are calling needle-swap constantly between tests, you may need to reload routes via require. If you do this, your modules/files are cached for quicker loading. This can cause testing to go awry. If you encounter this, try clearing the cache using the following functions.\n\n### clearCache([item])\n\n```\nneedleswap.clearCache(\"my-module\");\nneedleswap.clearCache([\"fs\", \"async\", \"my-module\"]);\nneedleswap.clearCache(); // This is the same as calling clearEntireCache\n```\n\nSince needleswap returns itself, you can easily do this post declaration of modules to inject.\n\n```\nneedleswap({\n\t\"fs\": \"some fake functions for fs\"\n}).clearCache();\n```\n\nCalling with an individual or an array of names will get resolved and cleared from the cache.\n\n### clearEntireCache()\nThis will erase ALL cached values in the require, vastly slowly dowing your loading of modules but guaranteeing a fresh read of any module or file you require.\n\n\n## Cleanup\n\nAfter you're done, you can call .clear() to clear out all of needleswap's overrides. Since needleswap is switching out the global require function, you'll need to do this even if you destroy the needleswap object or work in an entirely different file - so it's an important step!\n\n```\nneedleswap.clear()\n```\n\n### The pun in the name is terrible\nPoint taken.\n\n\n### Ugh.\n;-)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhlfshell%2Fneedle-swap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhlfshell%2Fneedle-swap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhlfshell%2Fneedle-swap/lists"}