{"id":15553247,"url":"https://github.com/bahmutov/stub-spawn-once","last_synced_at":"2025-08-31T00:31:18.464Z","repository":{"id":57373017,"uuid":"96703740","full_name":"bahmutov/stub-spawn-once","owner":"bahmutov","description":"Stubs child_process.spawn for a single command; cleans up afterwards. Perfect for testing.","archived":false,"fork":false,"pushed_at":"2019-12-02T19:38:11.000Z","size":18,"stargazers_count":3,"open_issues_count":3,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-05-01T23:52:42.394Z","etag":null,"topics":["child-process","node","nodejs","spawn","stub","test","testing","util"],"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/bahmutov.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-07-09T18:37:36.000Z","updated_at":"2022-03-28T23:01:11.000Z","dependencies_parsed_at":"2022-08-29T14:42:32.018Z","dependency_job_id":null,"html_url":"https://github.com/bahmutov/stub-spawn-once","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fstub-spawn-once","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fstub-spawn-once/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fstub-spawn-once/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fstub-spawn-once/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bahmutov","download_url":"https://codeload.github.com/bahmutov/stub-spawn-once/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231539812,"owners_count":18392341,"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":["child-process","node","nodejs","spawn","stub","test","testing","util"],"created_at":"2024-10-02T14:27:41.791Z","updated_at":"2024-12-27T21:06:32.539Z","avatar_url":"https://github.com/bahmutov.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# stub-spawn-once\n\n\u003e Stubs child_process.spawn for a single command; cleans up afterwards. Perfect for testing.\n\n[![NPM][npm-icon] ][npm-url]\n\n[![Build status][ci-image] ][ci-url]\n[![semantic-release][semantic-image] ][semantic-url]\n[![js-standard-style][standard-image]][standard-url]\n\nRead [Node mocking examples](https://github.com/bahmutov/node-mock-examples) and [mocked-env](https://github.com/bahmutov/mocked-env)\n\n## Why\n\nMocking [child_process.spawn][spawn] is hard. See for yourself - the stubbing\napi for your tests is hard in [mock-spawn](https://github.com/gotwarlost/mock-spawn#common-cases)\nor [spawn-mock](https://github.com/TylorS/spawn-mock#api). I wanted something\nmuch simpler: just specify exit code and `stdout` and `stderr` for a single\nexecution. This module does this\n\n```js\nconst execa = require('execa') // or any module\nconst { stubSpawnOnce } = require('.')\nstubSpawnOnce(\n  '/bin/sh -c echo \"hello\"',\n  0, // exit code\n  'hi from stub!', // stdout\n  'and some error output' // stderr\n)\nexeca\n  .shell('echo \"hello\"')\n  .then(console.log)\n  /*\n    output:\n    {\n      stdout: 'hi from stub!',\n      stderr: 'and some error output',\n      code: 0,\n      failed: false,\n      killed: false,\n      signal: null,\n      cmd: '/bin/sh -c echo \"hello\"',\n      timedOut: false\n    }\n  */\n  .then(() =\u003e {\n    // call command again - the stub is gone\n    return execa.shell('echo \"hello\"')\n  })\n  .then(console.log)\n  /*\n    output:\n    {\n      stdout: 'hello',\n      stderr: '',\n      code: 0,\n      failed: false,\n      killed: false,\n      signal: null,\n      cmd: '/bin/sh -c echo \"hello\"',\n      timedOut: false\n    }\n  */\n```\n\n***hint** `exit code` argument is optional and you can omit it (then 0 will be\nreturned)\n\n```js\nconst { stubSpawnShellOnce } = require('.')\nstubSpawnShellOnce('my command', 'hi there', 'error output string')\n```\n\n[spawn]: http://devdocs.io/node/child_process#child_process_child_process_spawn_command_args_options\n\n## Install\n\nRequires [Node](https://nodejs.org/en/) version 6 or above.\n\n```sh\nnpm install --save-dev stub-spawn-once\n```\n\n## Use\n\nExamples from Mocha unit tests. Common information\n\n* only the given command is stubbed,\n  other spawn commands are *unaffected* by the stub.\n* a stub will be removed after it runs once.\n\n### stubSpawnOnce\n\n```js\nconst { stubSpawnOnce } = require('stub-spawn-once')\nconst execa = require('execa')\nit('prints mock output', () =\u003e {\n  const cmd = 'echo \"hello\"'\n  // output \"foo\" instead of \"hello\"\n  stubSpawnOnce(`/bin/sh -c ${cmd}`, 0, 'foo', 'bar')\n  return execa.shell(cmd)\n    .then(result =\u003e {\n      // result.code = 0\n      // result.stdout = \"foo\"\n      // result.stderr = \"bar\"\n    })\n})\n```\n\n### stubSpawnShellOnce\n\n```js\nconst { stubSpawnShellOnce } = require('stub-spawn-once')\nconst execa = require('execa')\nit('prints mock output', () =\u003e {\n  const cmd = 'echo \"hello\"'\n  // output \"foo\" instead of \"hello\"\n  stubSpawnShellOnce(cmd, 0, 'foo', 'bar')\n  return execa.shell(cmd)\n    .then(result =\u003e {\n      // result.code = 0\n      // result.stdout = \"foo\"\n      // result.stderr = \"bar\"\n    })\n})\n```\n\n## Bonus\n\nAs a bonus, this module also mocks [child_process.execFile][execFile] allowing\nyou easy testing.\n\n```js\nconst {stubSpawnOnce} = require('stub-spawn-once')\nstubSpawnOnce('echo \"hello\"', null, 'foo', 'bar')\nconst cp = require('child_process')\ncp.exec('echo \"hello\"', (code, out, errors) =\u003e {\n  // code is null\n  // out is \"foo\"\n  // errors is \"bar\"\n})\n```\n\nYou can use alias `stubExecOnce` to `stubSpawnOnce`\n\n```js\nconst {stubExecOnce} = require('stub-spawn-once')\nstubExecOnce('echo \"hi\"', \"bye\")\n```\n\nYou can pass desired error instance\n\n```js\nconst error = new Error('my error')\nstubExecOnce('invalid command', error)\ncp.exec('invalid command', err =\u003e {\n  // err === error\n})\n```\n\n[execFile]: https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback\n\n### Small print\n\nAuthor: Gleb Bahmutov \u0026lt;gleb.bahmutov@gmail.com\u0026gt; \u0026copy; 2017\n\n* [@bahmutov](https://twitter.com/bahmutov)\n* [glebbahmutov.com](https://glebbahmutov.com)\n* [blog](https://glebbahmutov.com/blog)\n\nLicense: MIT - do anything with the code, but don't blame me if it does not work.\n\nSupport: if you find any problems with this module, email / tweet /\n[open issue](https://github.com/bahmutov/stub-spawn-once/issues) on Github\n\n## MIT License\n\nCopyright (c) 2017 Gleb Bahmutov \u0026lt;gleb.bahmutov@gmail.com\u0026gt;\n\nPermission is hereby granted, free of charge, to any person\nobtaining a copy of this software and associated documentation\nfiles (the \"Software\"), to deal in the Software without\nrestriction, including without limitation the rights to use,\ncopy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the\nSoftware is furnished to do so, subject to the following\nconditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES\nOF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT\nHOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\nWHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\nFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\nOTHER DEALINGS IN THE SOFTWARE.\n\n[npm-icon]: https://nodei.co/npm/stub-spawn-once.svg?downloads=true\n[npm-url]: https://npmjs.org/package/stub-spawn-once\n[ci-image]: https://travis-ci.org/bahmutov/stub-spawn-once.svg?branch=master\n[ci-url]: https://travis-ci.org/bahmutov/stub-spawn-once\n[semantic-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg\n[semantic-url]: https://github.com/semantic-release/semantic-release\n[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg\n[standard-url]: http://standardjs.com/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbahmutov%2Fstub-spawn-once","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbahmutov%2Fstub-spawn-once","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbahmutov%2Fstub-spawn-once/lists"}