/nock-exec

NodeJS mock module around the child_process.exec function inspired by nock

Primary LanguageJavaScript

Nock-exec is a child_process.exec mocking library for Node.js inspired by nock library.

Usage

In your test you can write:

var nockExec = require('nock-exec');
nockExec('myapp').err('some error').reply(0, 'This command was mocked');

This means that any call in the form

var exec = require('child_process').exec;
exec('myapp', function(error, stdout, stderr) {
   console.log('stdout: ' + stdout);
   console.log('stderr: ' + stderr);
   if (error !== null) {
     console.log('exec error: ' + error);
   }
});

will produce this output:

stdout: This command was mocked
stderr: some error

You can limit the execution of the command one time:

var nockExec = require('nock-exec');
nockExec('myapp').once().err('some error').reply(0, 'This command was mocked');

If you try to call this command again will not work.

You can match based on a regular expression

var nockExec = require('nock-exec');
nockExec('myapp.*').regex().err('some error').reply(0, 'This command was matched using a regex');

To do

arguments

Manage conditional arguments like

var nockExec = require('nock-exec');
nockExec('myapp').args('a b c').reply(0, 'done');

delay

var nockExec = require('nock-exec');
nockExec('myapp').args('a b c').delay(300).reply(0, 'done');

filtering

var nockExec = require('nock-exec');
nockExec('myapp')
    .filteringArgs(function(args) {
       return 'a b c';
    })
    .reply(0, 'done');