Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock the Node.js child_process spawn function?

Is there an easy way to mock the Node.js child_process spawn function?

I have code like the following, and would like to test it in a unit test, without having to rely on the actual tool calls:

var output;
var spawn = require('child_process').spawn;
var command = spawn('foo', ['get']);

command.stdout.on('data', function (data) {
    output = data;
});

command.stdout.on('end', function () {
    if (output) {
        callback(null, true);
    }
    else {
        callback(null, false);
    }
});

Is there a (proven and maintained) library that allows me to mock the spawn call and lets me specify the output of the mocked call?

I don't want to rely on the tool or OS to keep the tests simple and isolated. I want to be able to run the tests without having to set up complex test fixtures, which could mean a lot of work (including changing system configuration).

Is there an easy way to do this?

like image 520
nwinkler Avatar asked Nov 10 '14 08:11

nwinkler


People also ask

How do you spawn in node JS?

The spawn function launches a command in a new process and we can use it to pass that command any arguments. For example, here's code to spawn a new process that will execute the pwd command. const { spawn } = require('child_process'); const child = spawn('pwd');

How do you mock in node JS?

In Jest, Node. js modules are automatically mocked in your tests when you place the mock files in a __mocks__ folder that's next to the node_modules folder. For example, if you a file called __mock__/fs. js , then every time the fs module is called in your test, Jest will automatically use the mocks.

What is Child_process in node?

Usually, Node. js allows single-threaded, non-blocking performance but running a single thread in a CPU cannot handle increasing workload hence the child_process module can be used to spawn child processes. The child processes communicate with each other using a built-in messaging system.


2 Answers

For anyone who still has problems with this particular problem and for some reason, the recommendations in other answers don't help, I was able to get it to work with proxyrequire (https://github.com/thlorenz/proxyquire) by replacing the real child_process spawn with an event emitter that I then used in my tests to mock the emission.

var stdout = new events.EventEmitter();
var stderr = new events.EventEmitter();
var spawn = new events.EventEmitter();
spawn.stderr = stderr;
spawn.stdout = stdout;

var child_process = {
  spawn: () => spawn,
  stdout,
  stderr
};

// proxyrequire replaces the child_process require in the file pathToModule
var moduleToTest = proxyquire("./pathToModule/", {
  'child_process': child_process
});

describe('Actual test', function () {
  var response;

  before(function (done) {
    // your regular method call
    moduleToTest.methodToTest()
    .then(data => {
      response = data;
      done();
    }).catch(err => {
      response = err;
      done();
    });

    // emit your expected response
    child_process.stdout.emit("data", "the success message sent");
    // you could easily use the below to test an error
    // child_process.stderr.emit("data", "the error sent");
  });

  it('test your expectation', function () {
    expect(response).to.equal("the success message or whatever your moduleToTest 
      resolves with");
  });
});

Hope this helps...

like image 93
Caleb Mbakwe Avatar answered Oct 27 '22 16:10

Caleb Mbakwe


Came across this and nwinkler's answer put me on the path. Below is a Mocha, Sinon and Typescript example that wraps the spawn in a promise, resolving if the exit code is a zero, and rejecting otherwise, It gathers up STDOUT/STDERR output, and lets you pipe text in through STDIN. Testing for a failure would be just a matter of testing for the exception.

function spawnAsPromise(cmd: string, args: ReadonlyArray<string> | undefined, options: child_process.SpawnOptions | undefined, input: string | undefined) {
    return new Promise((resolve, reject) => {
        // You could separate STDOUT and STDERR if your heart so desires...
        let output: string = '';  
        const child = child_process.spawn(cmd, args, options);
        child.stdout.on('data', (data) => {
            output += data;
        });
        child.stderr.on('data', (data) => {
            output += data;
        });
        child.on('close', (code) => {
            (code === 0) ? resolve(output) : reject(output);
        });
        child.on('error', (err) => {
            reject(err.toString());
        });

        if(input) {            
            child.stdin.write(input);
            child.stdin.end();
        }
    });
}

// ...

describe("SpawnService", () => {
    it("should run successfully", async() => {
        const sandbox = sinon.createSandbox();
        try {
            const CMD = 'foo';
            const ARGS = ['--bar'];
            const OPTS = { cwd: '/var/fubar' };

            const STDIN_TEXT = 'I typed this!';
            const STDERR_TEXT = 'Some diag stuff...';
            const STDOUT_TEXT = 'Some output stuff...';

            const proc = <child_process.ChildProcess> new events.EventEmitter();
            proc.stdin = new stream.Writable();
            proc.stdout = <stream.Readable> new events.EventEmitter();
            proc.stderr = <stream.Readable> new events.EventEmitter();

            // Stub out child process, returning our fake child process
            sandbox.stub(child_process, 'spawn')
                .returns(proc)    
                .calledOnceWith(CMD, ARGS, OPTS);

            // Stub our expectations with any text we are inputing,
            // you can remove these two lines if not piping in data
            sandbox.stub(proc.stdin, "write").calledOnceWith(STDIN_TEXT);
            sandbox.stub(proc.stdin, "end").calledOnce = true;

            // Launch your process here
            const p = spawnAsPromise(CMD, ARGS, OPTS, STDIN_TEXT);

            // Simulate your program's output
            proc.stderr.emit('data', STDERR_TEXT);
            proc.stdout.emit('data', STDOUT_TEXT);

            // Exit your program, 0 = success, !0 = failure
            proc.emit('close', 0);

            // The close should get rid of the process
            const results = await p;
            assert.equal(results, STDERR_TEXT + STDOUT_TEXT);
        } finally {
            sandbox.restore();
        }
    });
});
like image 45
Jason Avatar answered Oct 27 '22 15:10

Jason