stub-spawn-once
v2.3.0
Published
Stubs child_process.spawn for a single command; cleans up afterwards. Perfect for testing.
Downloads
2,149
Maintainers
Readme
stub-spawn-once
Stubs child_process.spawn for a single command; cleans up afterwards. Perfect for testing.
Why
Mocking child_process.spawn is hard. See for yourself - the stubbing
api for your tests is hard in mock-spawn
or spawn-mock. I wanted something
much simpler: just specify exit code and stdout
and stderr
for a single
execution. This module does this
const execa = require('execa') // or any module
const { stubSpawnOnce } = require('.')
stubSpawnOnce(
'/bin/sh -c echo "hello"',
0, // exit code
'hi from stub!', // stdout
'and some error output' // stderr
)
execa
.shell('echo "hello"')
.then(console.log)
/*
output:
{
stdout: 'hi from stub!',
stderr: 'and some error output',
code: 0,
failed: false,
killed: false,
signal: null,
cmd: '/bin/sh -c echo "hello"',
timedOut: false
}
*/
.then(() => {
// call command again - the stub is gone
return execa.shell('echo "hello"')
})
.then(console.log)
/*
output:
{
stdout: 'hello',
stderr: '',
code: 0,
failed: false,
killed: false,
signal: null,
cmd: '/bin/sh -c echo "hello"',
timedOut: false
}
*/
*hint exit code
argument is optional and you can omit it (then 0 will be
returned)
const { stubSpawnShellOnce } = require('.')
stubSpawnShellOnce('my command', 'hi there', 'error output string')
Install
Requires Node version 6 or above.
npm install --save-dev stub-spawn-once
Use
Examples from Mocha unit tests. Common information
- only the given command is stubbed, other spawn commands are unaffected by the stub.
- a stub will be removed after it runs once.
stubSpawnOnce
const { stubSpawnOnce } = require('stub-spawn-once')
const execa = require('execa')
it('prints mock output', () => {
const cmd = 'echo "hello"'
// output "foo" instead of "hello"
stubSpawnOnce(`/bin/sh -c ${cmd}`, 0, 'foo', 'bar')
return execa.shell(cmd)
.then(result => {
// result.code = 0
// result.stdout = "foo"
// result.stderr = "bar"
})
})
stubSpawnShellOnce
const { stubSpawnShellOnce } = require('stub-spawn-once')
const execa = require('execa')
it('prints mock output', () => {
const cmd = 'echo "hello"'
// output "foo" instead of "hello"
stubSpawnShellOnce(cmd, 0, 'foo', 'bar')
return execa.shell(cmd)
.then(result => {
// result.code = 0
// result.stdout = "foo"
// result.stderr = "bar"
})
})
Bonus
As a bonus, this module also mocks child_process.execFile allowing you easy testing.
const {stubSpawnOnce} = require('stub-spawn-once')
stubSpawnOnce('echo "hello"', 0, 'foo', 'bar')
const cp = require('child_process')
cp.exec('echo "hello"', (code, out, errors) => {
// code is 0
// out is "foo"
// errors is "bar"
})
You can use alias stubExecOnce
to stubSpawnOnce
const {stubExecOnce} = require('stub-spawn-once')
stubExecOnce('echo "hi"', "bye")
Small print
Author: Gleb Bahmutov <[email protected]> © 2017
License: MIT - do anything with the code, but don't blame me if it does not work.
Support: if you find any problems with this module, email / tweet / open issue on Github
MIT License
Copyright (c) 2017 Gleb Bahmutov <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.