package
A mock for child_process.spawn
npm install mock-spawnGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
An easy-to-use mock for child_process.spawn.
spawn was invoked and the details
of every invocation are available as attributes on the mock for later assertionsvar mockSpawn = require('mock-spawn');
// override child_process.spawn
// this is a simplistic example; you can use a library like `mockery` to
// set a new instance for every test. See examples/complete/test.js
var mySpawn = mockSpawn();
require('child_process').spawn = mySpawn;
// at this point you have mocked child_process.spawn to always exit 0
// and write nothing to stdout or stderr
// let's change the default processing to exit 1 always and write something
// to stdout
mySpawn.setDefault(mySpawn.simple(1 /* exit code */, 'hello world' /* stdout */));
// let's tell the mock to do specific things on sequential calls
// in this case we exit 0 on the first call, 1 on the second call and so on
mySpawn.sequence.add(mySpawn.simple
var mockSpawn = require('mock-spawn');
// basic stuff
var mySpawn = mockSpawn();
require('child_process').spawn = mySpawn;
// we are now testing if our library under test retries spawn commands on error
// when executing the `foo` command
var count = 0;
mySpawn.setStrategy(function (command, args, opts) {
if (command !== 'foo') { return null; } // use default
if (++count < 3) {
return mySpawn.simple(1); //exit 1 immediately
The runner function accepts a single callback that needs to be called with an
exit code and optionally a signal name. If you define a throws property on
the runner object, it will throw that error synchronously to mimic the
behavior of child_process.spawn. It will ignore everything else in this case,
and it will not "run" at all.
CAVEAT: The throws value must be an instanceof Error.
The runner function has access to the following attributes via this
this.stdout - the standard output of the process to which it can writethis.stderr - the standard error of the process to which it can writethis.command - the command for the spawn callthis.args - the args for the spawn callthis.opts - the options object passed to the spawn callthis.emit - the emit method of the underlying EventEmitterThe process "runs" until the runner calls the callback.
The strategy function accepts 3 arguments: the command, args and options passed
to the spawn invocation. It can inspect these to return a customized runner
for just that invocation.
It can also return a falsy value to indicate that the default function should be used.
returns a function that can be plugged into child_process as a replacement
for spawn
returns a runner function that exits with the specified code and writes specific data to the output and error streams
Arguments are:
sets the default processing of all spawn invocations to use the runner function specified
enables the sequence strategy and calls the runner function supplied at the
specific point in the sequence.
add plugs a runner function
for the nth invocation to spawnDo not mix sequence.add and setStrategy calls for a specific run.
sets fn as the strategy that will return runner functions on demand.
Do not mix sequence.add and setStrategy calls for a specific run.
sets obj as a lookup table for whether to exit. If the value is true,
then the runner will emit exit with code null and signal <signal>.
array of mock process objects that you can use to inspect how your library
under test invoked spawn. Every object has the following properties available
command - the commandargs - the command argumentsopts - the options passed to the spawn invocationexitCode - the exit code of the processsignal - the signal delivered to the process (simulated via the runner)BSD. See accompanying LICENSE file.
The following third-party libraries are used by this module:
Pull requests welcome!
child_process.fork and child_process.exec processingprocess.kill